From d88d344e4e773607a6823608575cf845284896dc Mon Sep 17 00:00:00 2001 From: Ethan Ruszanowski Date: Thu, 9 Nov 2023 14:49:10 -0500 Subject: [PATCH] First working bot. --- .gitignore | 3 +++ .idea/.gitignore | 8 ++++++++ Dockerfile | 10 ++++++++++ bot.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ requiremtns.txt | 10 ++++++++++ 5 files changed, 82 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 Dockerfile create mode 100644 bot.py create mode 100644 requiremtns.txt diff --git a/.gitignore b/.gitignore index 68bc17f..e307134 100644 --- a/.gitignore +++ b/.gitignore @@ -152,6 +152,9 @@ dmypy.json # Cython debug symbols cython_debug/ +compose.yml +.idea/ + # PyCharm # JetBrains specific template is maintained in a separate JetBrains.gitignore that can # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3421d9c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM python:3.11.6 + +WORKDIR /usr/src/units + +COPY requiremtns.txt . +RUN pip install -r requirements.com + +COPY bot.py . + +CMD ["python", "bot.py"] diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..7992776 --- /dev/null +++ b/bot.py @@ -0,0 +1,51 @@ +import os +from urllib.parse import urlparse, parse_qs +from contextlib import suppress +import discord +from discord.ext import commands +import re + +intents = discord.Intents.default() +intents.members = True +intents.message_content = True + +bot = commands.Bot(command_prefix='?', intents=intents) + +PIPED_URL = os.getenv('PIPED_URL') +DISCORD_TOKEN = os.getenv('DISCORD_TOKEN') + + +@bot.event +async def on_ready(): + print("Ready!") + + +@bot.event +async def on_message(message): + regex = re.compile( + r'https://(music\.)?(www\.)?youtu(be)?\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&/=]*)') + + result = regex.search(message.content) + if result is not None: + youtube_url = result.group(0) + video_id = await get_youtube_id(youtube_url) + + if video_id is not None: + await message.reply(f"I think you meant {PIPED_URL}{video_id}.", mention_author=False) + + +async def get_youtube_id(url: str, ignore_playlist=True) -> str: + query = urlparse(url) + if query.hostname == 'youtu.be': return query.path[1:] + if query.hostname in {'www.youtube.com', 'youtube.com', 'music.youtube.com'}: + if not ignore_playlist: + # use case: get playlist id not current video in playlist + with suppress(KeyError): + return parse_qs(query.query)['list'][0] + if query.path == '/watch': return parse_qs(query.query)['v'][0] + if query.path[:7] == '/watch/': return query.path.split('/')[1] + if query.path[:7] == '/embed/': return query.path.split('/')[2] + if query.path[:3] == '/v/': return query.path.split('/')[2] + + +bot.run(DISCORD_TOKEN) diff --git a/requiremtns.txt b/requiremtns.txt new file mode 100644 index 0000000..69a3e12 --- /dev/null +++ b/requiremtns.txt @@ -0,0 +1,10 @@ +aiohttp==3.8.6 +aiosignal==1.3.1 +async-timeout==4.0.3 +attrs==23.1.0 +charset-normalizer==3.3.2 +discord.py==2.3.2 +frozenlist==1.4.0 +idna==3.4 +multidict==6.0.4 +yarl==1.9.2