From b469ae4e9b21ed0b72e1b257737b704d128b8483 Mon Sep 17 00:00:00 2001 From: deflax Date: Wed, 23 Mar 2022 21:13:39 -0400 Subject: [PATCH] refactor with bot.event decorators --- .env.dist | 4 +-- Dockerfile | 4 +-- main.py | 76 ++++++++++++++++++++++++++++++++++++------------------ 3 files changed, 55 insertions(+), 29 deletions(-) diff --git a/.env.dist b/.env.dist index ec956dd..4bcfd53 100644 --- a/.env.dist +++ b/.env.dist @@ -1,7 +1,7 @@ version=0.1 token=OT.. -channel=0 -name=radiobot +channel_text=123 +channel_voice=456 url=http://example.net passes=2 bitrate=48000 diff --git a/Dockerfile b/Dockerfile index 327bd6c..63bf6d7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,9 +4,9 @@ RUN apt-get update RUN apt-get -yq install ffmpeg build-essential RUN pip --no-cache-dir install \ - discord.py \ + discord.py[voice] \ pynacl WORKDIR /app -COPY . /app \ No newline at end of file +COPY . /app diff --git a/main.py b/main.py index a8e3b06..22277bd 100644 --- a/main.py +++ b/main.py @@ -1,42 +1,68 @@ import os import discord -from discord.ext.commands import Bot +from discord.ext import commands +import asyncio +from subprocess import Popen bot_version = os.environ['version'] print('radiobot ' + bot_version + ' starting') -bot_token = os.environ['token'] +login_token = os.environ['token'] +voice_channel_id = os.environ['channel_voice'] +text_channel_id = config['channel_test'] -client = Bot(command_prefix="!") +# Configure the bot +description = '''Radiobot.''' +bot = commands.Bot(command_prefix='!', description=description) +# Initialize opus +if not discord.opus.is_loaded(): + discord.opus.load_opus('opus') + +# Initialize some global variables +voice_client = None +text_channel = None isPlaying = False -class Radio(discord.Client): - async def on_ready(self): - print('Logged on as', self.user) +@bot.event +async def on_ready(): + print('Logged in as') + print(bot.user.name) + print(bot.user.id) + print('------') - async def on_message(self, message): - # don't respond to ourselves - if message.author == self.user: - return + # get channels + voice_channel = bot.get_channel(voice_channel_id) + text_channel = bot.get_channel(text_channel_id) + if not v_channel: + print("No voice channel with that ID found!") + if not text_channel: + print("No text channel with that ID found!") + #voice_client = await bot.join_voice_channel(v_channel) - print ('<' + message.author.nick + '> ' + message.content) +@bot.event +async def on_message(ctx): + # don't respond to ourselves + if ctx.message.author == ctx.user: + return - if message.content == '!version': - await message.channel.send('] radiobot ' + bot_version) + print ('<' + ctx.message.author.nick + '> ' + ctx.message.content) - @client.event - async def on_voice_state_update(self, member, before, after): - clients_before = len(before.channel.members) + if ctx.message.content == '!version': + await ctx.message.channel.send('] radiobot ' + bot_version + ' - python: ' + os.environ['PYTHON_VERSION']) - # If nobody in the channel based on before, invoke join the channel - if clients_before == 0: - self.voiceChannel = await after.channel.connect() +@bot.event +async def on_voice_state_update(member, before, after): + clients_before = len(ctx.before.channel.members) - # if after join channel members > 0, join the channel - if clients_before == 1: - print("gg") - await self.voiceChannel.disconnect() + # If nobody in the channel based on before, invoke join the channel + if clients_before == 0: + voiceChannel = await after.channel.connect() + + # if after join channel members > 0, join the channel + if clients_before == 1: + print("gg") + await voiceChannel.disconnect() + +bot.run(bot_token) # Get token for this shit -client = Radio() -client.run(bot_token) # Get tokeun for this shit