Initial commit

This commit is contained in:
Roman 2023-06-06 17:01:17 -04:00
commit eaee2970b6
2 changed files with 90 additions and 0 deletions

16
Dockerfile Normal file
View file

@ -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"]

74
main.py Normal file
View file

@ -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"))