refactor with bot.event decorators

This commit is contained in:
deflax 2022-03-23 21:13:39 -04:00
parent 5c9f95f281
commit b469ae4e9b
3 changed files with 55 additions and 29 deletions

View file

@ -1,7 +1,7 @@
version=0.1 version=0.1
token=OT.. token=OT..
channel=0 channel_text=123
name=radiobot channel_voice=456
url=http://example.net url=http://example.net
passes=2 passes=2
bitrate=48000 bitrate=48000

View file

@ -4,7 +4,7 @@ RUN apt-get update
RUN apt-get -yq install ffmpeg build-essential RUN apt-get -yq install ffmpeg build-essential
RUN pip --no-cache-dir install \ RUN pip --no-cache-dir install \
discord.py \ discord.py[voice] \
pynacl pynacl
WORKDIR /app WORKDIR /app

62
main.py
View file

@ -1,42 +1,68 @@
import os import os
import discord import discord
from discord.ext.commands import Bot from discord.ext import commands
import asyncio
from subprocess import Popen
bot_version = os.environ['version'] bot_version = os.environ['version']
print('radiobot ' + bot_version + ' starting') 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 isPlaying = False
class Radio(discord.Client):
async def on_ready(self): @bot.event
print('Logged on as', self.user) async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
async def on_message(self, message): # 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)
@bot.event
async def on_message(ctx):
# don't respond to ourselves # don't respond to ourselves
if message.author == self.user: if ctx.message.author == ctx.user:
return return
print ('<' + message.author.nick + '> ' + message.content) print ('<' + ctx.message.author.nick + '> ' + ctx.message.content)
if message.content == '!version': if ctx.message.content == '!version':
await message.channel.send('] radiobot ' + bot_version) await ctx.message.channel.send('] radiobot ' + bot_version + ' - python: ' + os.environ['PYTHON_VERSION'])
@client.event @bot.event
async def on_voice_state_update(self, member, before, after): async def on_voice_state_update(member, before, after):
clients_before = len(before.channel.members) clients_before = len(ctx.before.channel.members)
# If nobody in the channel based on before, invoke join the channel # If nobody in the channel based on before, invoke join the channel
if clients_before == 0: if clients_before == 0:
self.voiceChannel = await after.channel.connect() voiceChannel = await after.channel.connect()
# if after join channel members > 0, join the channel # if after join channel members > 0, join the channel
if clients_before == 1: if clients_before == 1:
print("gg") print("gg")
await self.voiceChannel.disconnect() await voiceChannel.disconnect()
bot.run(bot_token) # Get token for this shit
client = Radio()
client.run(bot_token) # Get tokeun for this shit