radiobot/main.py
2022-03-23 21:55:30 -04:00

76 lines
2.2 KiB
Python

import os
import discord
from discord.ext import commands
import asyncio
from subprocess import Popen
bot_version = os.environ['version']
print('radiobot ' + bot_version + ' starting')
login_token = os.environ['token']
voice_channel_id = os.environ['channel_voice']
text_channel_id = os.environ['channel_text']
# 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
@bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
# get channels
voice_channel = bot.get_channel(voice_channel_id)
text_channel = bot.get_channel(text_channel_id)
if not voice_channel:
print("No voice channel " + voice_channel_id + " found!")
if not text_channel:
print("No text channel " + text_channel_id + " found!")
#voice_client = await bot.join_voice_channel(v_channel)
@bot.event
async def on_message(message):
# don't respond to ourselves
if message.author == bot.user:
return
print ('<' + message.author.nick + '> ' + message.content)
if message.content == '!version':
await message.channel.send('] radiobot ' + bot_version + ' - python: ' + os.environ['PYTHON_VERSION'])
@bot.event
async def on_voice_state_update(member, before, after):
if before.channel is None and after.channel is not None:
if after.channel.id == [voice_channel_id]:
clients_before = len(before.channel.members)
# If nobody in the channel based on before, invoke join the channel
if clients_before == 0:
voiceChannel = await after.channel.connect()
# if channel members > 0, leave the channel
if clients_before == 1:
print("gg")
await voiceChannel.disconnect()
# Start the bot with multiprocess compatiblity
if __name__ == "__main__":
try:
bot.loop.run_until_complete(bot.start(login_token))
finally:
bot.loop.close()