radiobot/main.py

76 lines
2.2 KiB
Python
Raw Normal View History

2022-03-23 19:45:22 -04:00
import os
2022-03-23 19:27:00 -04:00
import discord
2022-03-23 21:13:39 -04:00
from discord.ext import commands
import asyncio
from subprocess import Popen
2022-03-23 19:27:00 -04:00
2022-03-23 19:45:22 -04:00
bot_version = os.environ['version']
print('radiobot ' + bot_version + ' starting')
2022-03-23 19:27:00 -04:00
2022-03-23 21:13:39 -04:00
login_token = os.environ['token']
voice_channel_id = os.environ['channel_voice']
2022-03-23 21:22:35 -04:00
text_channel_id = os.environ['channel_text']
2022-03-23 19:56:13 -04:00
2022-03-23 21:13:39 -04:00
# Configure the bot
2022-03-23 21:22:35 -04:00
description = '''Radiobot'''
2022-03-23 21:13:39 -04:00
bot = commands.Bot(command_prefix='!', description=description)
2022-03-23 19:59:21 -04:00
2022-03-23 21:13:39 -04:00
# Initialize opus
2022-03-23 21:23:56 -04:00
#if not discord.opus.is_loaded():
# discord.opus.load_opus('opus')
2022-03-23 21:13:39 -04:00
# Initialize some global variables
voice_client = None
text_channel = None
2022-03-23 19:27:00 -04:00
isPlaying = False
2022-03-23 21:13:39 -04:00
@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)
2022-03-23 21:22:35 -04:00
if not voice_channel:
print("No voice channel " + voice_channel_id + " found!")
2022-03-23 21:13:39 -04:00
if not text_channel:
2022-03-23 21:22:35 -04:00
print("No text channel " + text_channel_id + " found!")
2022-03-23 21:13:39 -04:00
#voice_client = await bot.join_voice_channel(v_channel)
@bot.event
2022-03-23 21:55:30 -04:00
async def on_message(message):
2022-03-23 21:13:39 -04:00
# don't respond to ourselves
2022-03-23 21:55:30 -04:00
if message.author == bot.user:
2022-03-23 21:13:39 -04:00
return
2022-03-23 19:27:00 -04:00
2022-03-23 21:55:30 -04:00
print ('<' + message.author.nick + '> ' + message.content)
2022-03-23 19:27:00 -04:00
2022-03-23 21:55:30 -04:00
if message.content == '!version':
await message.channel.send('] radiobot ' + bot_version + ' - python: ' + os.environ['PYTHON_VERSION'])
2022-03-23 21:13:39 -04:00
@bot.event
async def on_voice_state_update(member, before, after):
2022-03-23 21:55:30 -04:00
if before.channel is None and after.channel is not None:
if after.channel.id == [voice_channel_id]:
clients_before = len(before.channel.members)
2022-03-23 19:27:00 -04:00
2022-03-23 21:55:30 -04:00
# If nobody in the channel based on before, invoke join the channel
if clients_before == 0:
voiceChannel = await after.channel.connect()
2022-03-23 19:27:00 -04:00
2022-03-23 21:55:30 -04:00
# if channel members > 0, leave the channel
if clients_before == 1:
print("gg")
await voiceChannel.disconnect()
2022-03-23 19:27:00 -04:00
2022-03-23 21:55:30 -04:00
# Start the bot with multiprocess compatiblity
if __name__ == "__main__":
try:
bot.loop.run_until_complete(bot.start(login_token))
finally:
bot.loop.close()
2022-03-23 19:27:00 -04:00