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
token=OT..
channel=0
name=radiobot
channel_text=123
channel_voice=456
url=http://example.net
passes=2
bitrate=48000

View file

@ -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
COPY . /app

76
main.py
View file

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