diff --git a/.github/workflows/flake8.yaml b/.github/workflows/flake8.yaml index 04556c2..539cdac 100644 --- a/.github/workflows/flake8.yaml +++ b/.github/workflows/flake8.yaml @@ -3,15 +3,15 @@ name: Check Syntax on: [push] jobs: - build: + flake8: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 - - name: Set up Python 3.8 - uses: actions/setup-python@v1 - with: - python-version: 3.8 - - name: Install dependencies - run: python -m pip install discord.py[voice] flake8 - - name: Run flake8 - run: flake8 --ignore=E302,E501 + - uses: actions/checkout@v3 + - name: Set up Python 3.9 + uses: actions/setup-python@v4 + with: + python-version: 3.9 + - name: Install dependencies + run: python -m pip install discord.py python-dotenv flake8 + - name: Run flake8 + run: flake8 --ignore=E261,E302,E305,E501 diff --git a/constant.py b/constant.py index 1209cab..c0d8127 100644 --- a/constant.py +++ b/constant.py @@ -6,4 +6,28 @@ TOKEN = getenv('DISCORD_BOT_TOKEN') API_URL = getenv('API_URL') GAS_URL = getenv('GAS_URL') +GUILD_LINKS_ID = int(getenv('GUILD_LINKS_ID', 870910390706532382)) LOG_CHANNEL_ID = int(getenv('LOG_CHANNEL_ID', 1068570419528990821)) + +CHANNEL_TAG_TABLE: dict = { + '872967889928486912': '人物', + '872894495430164610': 'イラスト', + '872841453636841524': '音楽', + '872940515547578378': '動画', +} + +DOMAINS_TABLE: dict = { + 'discord.com': 'discord', + 'ptb.discord.com': 'discord', + 'canary.discord.com': 'discord', + 'twitter.com': 'twitter', + 'open.spotify.com': 'spotify', + 'youtube.com': 'youtube', + 'www.youtube.com': 'youtube', + 'youtu.be': 'youtube', + 'pixiv.net': 'pixiv', + 'www.pixiv.net': 'pixiv', + 'soundcloud.com': 'soundcloud', + 'nicovideo.jp': 'nicovideo', + 'www.nicovideo.jp': 'nicovideo', +} diff --git a/extensions/gkb.py b/extensions/gkb.py index 7684d1b..b3f04ab 100644 --- a/extensions/gkb.py +++ b/extensions/gkb.py @@ -85,6 +85,7 @@ async def global_dictionary(self, interaction: discord.Interaction): domain='', domain_id='', user_id=f'discord_user_{interaction.user.id}', + ephemeral=False, ) await interaction.response.send_modal(modal) @@ -95,6 +96,7 @@ async def guild_dictionary(self, interaction: discord.Interaction): domain='discord', domain_id=f'discord_guild_{interaction.guild.id}', user_id=f'discord_user_{interaction.user.id}', + ephemeral=False, ) await interaction.response.send_modal(modal) @@ -104,6 +106,7 @@ async def private_dictionary(self, interaction: discord.Interaction): domain='discord', domain_id=f'discord_user_{interaction.user.id}', user_id=f'discord_user_{interaction.user.id}', + ephemeral=False, ) await interaction.response.send_modal(modal) diff --git a/extensions/url_handle.py b/extensions/url_handle.py index 94c6b2d..d547acf 100644 --- a/extensions/url_handle.py +++ b/extensions/url_handle.py @@ -1,21 +1,15 @@ from discord import Message from discord.ext import commands -from utils import Text2URL +from utils import get_urls_from_text from utils import save_url from utils import hashing from utils.dpyexcept import excepter - -GUILD_LINKS_ID = 870910390706532382 +from constant import GUILD_LINKS_ID +from constant import CHANNEL_TAG_TABLE class UrlHandleCog(commands.Cog): def __init__(self, bot): self.bot = bot - self.channels = { - '872967889928486912': '人物', - '872894495430164610': 'イラスト', - '872841453636841524': '音楽', - '872940515547578378': '動画', - } @commands.Cog.listener() @excepter @@ -25,21 +19,18 @@ async def on_message(self, message: Message): if message.guild.id != GUILD_LINKS_ID: return provider = hashing(str(message.author.id)) - text2url = Text2URL(message.content) - if len(text2url.urls) == 0: + urls = get_urls_from_text(message.content) + if len(urls) == 0: return - save_count = 0 - for url, data in text2url.urls.items(): + channel_tag = CHANNEL_TAG_TABLE.get(str(message.channel.id)) + is_saved = False + for url, data in urls.items(): if data.get('site') == 'discord': continue - await save_url( - provider, - url, - data, - {data.get('site'), self.channels.get(str(message.channel.id))} - {None}, - ) - save_count += 1 - if save_count > 0: + tags = {data.get('site'), channel_tag} - {None} + await save_url(provider, url, data, tags) + is_saved = True + if is_saved: await message.add_reaction('💾') async def setup(bot: commands.Bot) -> None: diff --git a/utils/__init__.py b/utils/__init__.py index 0db4a2c..e0579ca 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -1,42 +1,26 @@ import re from hashlib import sha256 -from utils.gas_client import GoogleAppScriptClient +from utils import gas_client +from constant import GAS_URL +from constant import DOMAINS_TABLE +REGEXP_URL = r'https?:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+' +REGEXP_URL_TWITTER = r'(https?:\/\/twitter\.com\/)([-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)' +PATTERN_TWITTER = re.compile(REGEXP_URL_TWITTER) -class Text2URL(): - def __init__(self, text): - self.text: str = text - self.pattern = r'https?:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+' - self.domains: dict = { - 'discord.com': 'discord', - 'ptb.discord.com': 'discord', - 'canary.discord.com': 'discord', - 'twitter.com': 'twitter', - 'open.spotify.com': 'spotify', - 'youtube.com': 'youtube', - 'www.youtube.com': 'youtube', - 'youtu.be': 'youtube', - 'pixiv.net': 'pixiv', - 'www.pixiv.net': 'pixiv', - 'soundcloud.com': 'soundcloud', - 'nicovideo.jp': 'nicovideo', - 'www.nicovideo.jp': 'nicovideo', +def get_urls_from_text(text: str): + urls: dict = {} + for url in re.findall(REGEXP_URL, text): + url = url.replace('http://', 'https://') + domain = url.split('://')[1].split('/')[0] + site = DOMAINS_TABLE.get(domain, 'other') + urls[url] = { + 'domain': domain, + 'site': site, } - self.urls: dict = {} - for url in re.findall(self.pattern, self.text): - url = url.replace('http://', 'https://') - domain = url.split('://')[1].split('/')[0] - site = self.domains.get(domain, 'other') - self.urls[url] = { - 'domain': domain, - 'site': site, - } - if site == 'twitter': - pattern = re.compile( - r'(https?:\/\/twitter\.com\/)([-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)' - ) - self.urls[url] |= {'twitter_id': pattern.match(url).group(2).split('/')[0]} - + if site == 'twitter': + urls[url] |= {'twitter_id': PATTERN_TWITTER.match(url).group(2).split('/')[0]} + return urls async def save_url(provider, url, data, tags): payload = { @@ -44,7 +28,7 @@ async def save_url(provider, url, data, tags): 'url': url, 'tags': ', '.join(tags), } - await GoogleAppScriptClient('all').post(payload) + await gas_client.post(GAS_URL, 'all', payload) for tag in tags: payload = { 'url': url, @@ -52,8 +36,7 @@ async def save_url(provider, url, data, tags): } if tag == 'twitter': payload |= {'twitter_id': data.get('twitter_id')} - await GoogleAppScriptClient(tag).post(payload) - + await gas_client.post(GAS_URL, tag, payload) def hashing(text: str): return sha256(text.encode()).hexdigest() diff --git a/utils/gas_client.py b/utils/gas_client.py index 6b56727..0a3f318 100644 --- a/utils/gas_client.py +++ b/utils/gas_client.py @@ -1,18 +1,12 @@ import aiohttp -import os +async def get(url, sheet_name) -> dict: + async with aiohttp.ClientSession() as session: + async with session.get(url, params={'sheet', sheet_name}) as resp: + return await resp.json() -class GoogleAppScriptClient(): - def __init__(self): - self.url = os.getenv('GAS_URL') - - async def get(self, sheet_name) -> dict: - async with aiohttp.ClientSession() as session: - async with session.get(self.url, params={'sheet', sheet_name}) as resp: - return await resp.json() - - async def post(self, sheet_name, data: dict) -> int: - payload = {'sheet': sheet_name} | data - async with aiohttp.ClientSession() as session: - async with session.post(self.url, json=payload) as resp: - return resp.status +async def post(url, sheet_name, data: dict) -> int: + payload = {'sheet': sheet_name} | data + async with aiohttp.ClientSession() as session: + async with session.post(url, json=payload) as resp: + return resp.status