commit eaee2970b60f6b7aac39401b38280f3785134c70 Author: Roman Date: Tue Jun 6 17:01:17 2023 -0400 Initial commit diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8a4042c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM python:3 + +# Install dependencies +RUN pip3 install python-dotenv +RUN pip3 install discord.py +RUN pip3 install pynacl +RUN pip3 install youtube_dl +RUN pip3 install git+https://github.com/Cupcakus/pafy + +WORKDIR /usr/src/app +COPY . . + +RUN apt update +RUN apt install -y ffmpeg + +CMD ["python3", "main.py"] diff --git a/main.py b/main.py new file mode 100644 index 0000000..334016f --- /dev/null +++ b/main.py @@ -0,0 +1,74 @@ +import os +from dotenv import load_dotenv +from functools import partial + +import discord +import pafy + +class Client(discord.Client): + def __init__(self): + intents = discord.Intents().default() + intents.members = True + super().__init__(intents=intents) + + + async def on_ready(self): + print("Logged on as {}".format(self.user)) + + + async def on_message(self, message: discord.Message): + if message.author == self.user: + return + + if message.channel.type is not discord.ChannelType.private: + return + + url = message.content + v_id = url.split("=")[1] + user = message.channel.recipient.id + + # Download file if it doesn't exist already + if not os.path.exists(url): + pafy.new(url).getbestaudio().download(filepath=v_id) + + for chan in self.get_all_channels(): + # Check if Channel is Voice Channel + if type(chan) is not discord.VoiceChannel: + continue + + # Join the voice channel the user who DM'd Sergei is in + if user not in [i.id for i in chan.members]: + continue + + # Try to create a new voice client; grab existing one if available + try: + v_client: discord.VoiceClient = await chan.connect() + except discord.errors.ClientException: + for client in self.voice_clients: + if client.channel.id != chan.id: + continue + + v_client = client + await v_client.move_to(chan) + v_client.stop() + + self.play(v_client, v_id) + + + @staticmethod + def play(vc: discord.VoiceClient, v_id: str, err=None): + if err is not None: + print(err) + return + + if vc.is_playing(): + return + + audio = discord.FFmpegOpusAudio(v_id) + vc.play(audio, after=partial(Client.play, vc, v_id)) + + +if __name__ == "__main__": + load_dotenv() + Client().run(os.getenv("DISCORD_TOK")) +