radiobot/main.py

104 lines
3.1 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-27 22:38:03 -04:00
isConnected = False
2022-03-23 19:27:00 -04:00
2022-03-23 21:13:39 -04:00
@bot.event
async def on_ready():
2022-03-27 22:38:03 -04:00
print('[INFO] Logged in as ' + bot.user.name + ' #' + str(bot.user.id))
2022-03-23 21:13:39 -04:00
# get channels
2022-03-27 22:38:03 -04:00
voice_channel = await bot.fetch_channel(voice_channel_id)
debug_channel = await bot.fetch_channel(text_channel_id)
if not voice_channel:
print("[WARN] No voice channel " + voice_channel_id + " found!")
if not debug_channel:
print("[WARN] No text channel " + text_channel_id + " found!")
await debug_channel.send('] ready.')
2022-03-23 21:13:39 -04:00
@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
if not str(message.channel.id) == text_channel_id:
return
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):
"""
Starts events when a user changes their voice state.
Such as connecting, disconnecting and moving between channels.
:type member: discord.Member
:type before: discord.VoiceState
:type after: discord.VoiceState
:param member: The member that changed their voice state.
:param before: The member as they were before the change.
:param after: The member as they are after the change.
:return:
"""
2022-03-27 22:38:03 -04:00
global isConnected
2022-03-27 23:23:10 -04:00
if member.bot:
#print("[INFO] self event detection")
return
2022-03-27 21:39:37 -04:00
2022-03-27 22:38:03 -04:00
voice_channel = await bot.fetch_channel(voice_channel_id)
member_ids = len(voice_channel.voice_states.keys())
debug_channel = await bot.fetch_channel(text_channel_id)
2022-03-27 23:23:10 -04:00
await debug_channel.send('] voice #' + voice_channel_id + ' member count: ' + str(member_ids))
2022-03-27 22:38:03 -04:00
2022-03-27 23:23:10 -04:00
if member_ids == 1 and isConnected == False:
isConnected = True
2022-03-27 22:38:03 -04:00
await debug_channel.send('] connecting to #' + voice_channel_id)
voiceChannel = await voice_channel.connect()
2022-03-27 23:29:02 -04:00
player = voice_channel.play(discord.FFmpegPCMAudio(os.environ['url'])
while not player.is_done():
await asyncio.sleep(1)
2022-03-27 23:23:10 -04:00
return
2022-03-23 19:27:00 -04:00
2022-03-27 23:23:10 -04:00
if member_ids == 1 and isConnected == True:
2022-03-27 22:38:03 -04:00
isConnected = False
2022-03-27 23:23:10 -04:00
await debug_channel.send('] disconnecting from #' + voice_channel_id)
for x in bot.voice_clients:
await x.disconnect()
return
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()