commit 29eae47855b209d73193d16eabb6d022a608dc6d Author: Roy Olav Purser Date: Sat May 25 16:58:06 2024 +0200 V1 diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..eb6dd29 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,15 @@ +kind: pipeline +name: default + +steps: +- name: docker + image: plugins/docker + settings: + username: + from_secret: docker_username + password: + from_secret: docker_password + repo: roypur/stream-discord-bot + tags: + - latest + - ${DRONE_BUILD_FINISHED} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2aaf911 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3.12-alpine AS venv +RUN ["mkdir", "-p", "/app"] +COPY ["bot.py", "/app/bot.py"] +COPY ["start.sh", "/app/start.sh"] +COPY ["install.sh", "/app/install.sh"] +RUN ["sh", "/app/install.sh"] + +FROM scratch +COPY --from=venv / / +USER 1455:1455 +ENTRYPOINT ["/app/start.sh"] diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..c915e0a --- /dev/null +++ b/bot.py @@ -0,0 +1,49 @@ +import os +from urllib.parse import urlparse, parse_qs +import discord + +DISCORD_TOKEN = os.getenv("DISCORD_TOKEN") +DISCORD_CHANNEL = int(os.getenv("DISCORD_CHANNEL")) +DISCORD_GUILD = int(os.getenv("DISCORD_GUILD")) + + +class CustomClient(discord.Client): + async def on_ready(self): + print(f"{self.user} has connected to Discord!") + + async def on_message(self, message: discord.message.Message): + if ( + message.channel.id == DISCORD_CHANNEL + and message.guild.id == DISCORD_GUILD + and message.author.id != self.user.id + ): + url = urlparse(message.content) + if "youtu" in url.hostname: + if ( + url.path.strip("/").startswith("shorts") + and len(splitted := url.path.split("/")) >= 2 + ): + video_id = splitted[len(splitted) - 1] + + await message.reply( + content=f"https://stream.purser.it/{video_id}?provider=youtube&direct=true" + ) + elif video := parse_qs(url.query).get("v"): + if isinstance(video, str): + await message.reply( + content=f"https://stream.purser.it/{video}?provider=youtube&direct=true" + ) + elif isinstance(video, list): + await message.reply( + content=f"https://stream.purser.it/{video[0]}?provider=youtube&direct=true" + ) + else: + video_id = url.path.strip("/") + await message.reply( + content=f"https://stream.purser.it/{video_id}?provider=youtube&direct=true" + ) + + +intents = discord.Intents.default() +intents.message_content = True +CustomClient(intents=intents).run(DISCORD_TOKEN) diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..d736945 --- /dev/null +++ b/install.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env sh + +python3 -m venv /app/venv +source /app/venv/bin/activate + +pip3 install --upgrade pip +pip3 install --upgrade wheel +pip3 install --upgrade 'discord.py' diff --git a/start.sh b/start.sh new file mode 100644 index 0000000..6bfa827 --- /dev/null +++ b/start.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env sh +source /app/venv/bin/activate +exec /app/bot.py