Major update to UI and bot code. Added config file.
It is now modular and can be used on any machine. Still TODO- - 420 Timer - SDR built into bot
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,2 +1,4 @@
|
||||
/.idea/
|
||||
/__pycache__/
|
||||
/__pycache__/
|
||||
/venv/
|
||||
config.ini
|
||||
104
bot.py
104
bot.py
@@ -1,64 +1,74 @@
|
||||
import discord
|
||||
import sound
|
||||
import asyncio
|
||||
import datetime
|
||||
from discord.ext import commands
|
||||
|
||||
|
||||
DEVICE_NAME = "VoiceMeeter Output"
|
||||
BOT_TOKEN = 'OTE1MDY0OTk2OTk0NjMzNzI5.YaWKsA.Y9yaCGg_VXRL_qQVbs05vo7gSAc'
|
||||
# For Alex's use
|
||||
#async def bg_timer():
|
||||
# await bot.wait_until_ready()
|
||||
# channel = bot.get_channel(id=757379843792044102)
|
||||
# while not bot.is_closed():
|
||||
# guild = channel.guild
|
||||
# role_id = discord.utils.get(guild.roles, name='Superadmins').id
|
||||
# willie_time = ("04:20:00", "00:16:00")
|
||||
# print(guild.roles)
|
||||
# time_now = datetime.datetime.now()
|
||||
# time_now_str = f"{time_now.strftime('%H:%M')}:00"
|
||||
# print(role_id)
|
||||
# if 1: #time_now_str in willie_time:
|
||||
# await channel.send(f"<@{role_id}> hello")
|
||||
# print("yes")
|
||||
#
|
||||
# await asyncio.sleep(30)
|
||||
|
||||
|
||||
bot = commands.Bot(command_prefix='>')
|
||||
class Bot(commands.Bot):
|
||||
def __init__(self, bot_token, device_id, device_name, command_prefix='>!'):
|
||||
commands.Bot.__init__(self, command_prefix=commands.when_mentioned_or(command_prefix))
|
||||
self.DEVICE_ID = int(device_id)
|
||||
self.DEVICE_NAME = str(device_name)
|
||||
self.BOT_TOKEN = str(bot_token)
|
||||
self.Devices_List = sound.query_devices().items()
|
||||
|
||||
async def bg_timer():
|
||||
await bot.wait_until_ready()
|
||||
channel = bot.get_channel(id=757379843792044102)
|
||||
while not bot.is_closed():
|
||||
guild = channel.guild
|
||||
role_id = discord.utils.get(guild.roles, name='Superadmins').id
|
||||
willie_time = ("04:20:00", "00:16:00")
|
||||
print(guild.roles)
|
||||
time_now = datetime.datetime.now()
|
||||
time_now_str = f"{time_now.strftime('%H:%M')}:00"
|
||||
print(role_id)
|
||||
if 1: #time_now_str in willie_time:
|
||||
await channel.send(f"<@{role_id}> hello")
|
||||
print("yes")
|
||||
self.add_commands()
|
||||
|
||||
await asyncio.sleep(30)
|
||||
def start_bot(self):
|
||||
self.run(self.BOT_TOKEN)
|
||||
|
||||
@bot.command()
|
||||
async def ping(ctx):
|
||||
await ctx.send('pong')
|
||||
def add_commands(self):
|
||||
@self.command()
|
||||
async def ping(ctx):
|
||||
await ctx.send('pong')
|
||||
|
||||
@bot.command()
|
||||
async def join(ctx):
|
||||
await bot.wait_until_ready()
|
||||
discord.opus.load_opus('./opus/libopus.dll')
|
||||
@self.command()
|
||||
async def join(ctx):
|
||||
await self.wait_until_ready()
|
||||
discord.opus.load_opus('./opus/libopus.dll')
|
||||
|
||||
if discord.opus.is_loaded():
|
||||
stream = sound.PCMStream()
|
||||
channel = ctx.author.voice.channel
|
||||
for device, index in sound.query_devices().items():
|
||||
print(index, device)
|
||||
if DEVICE_NAME in device:
|
||||
stream.change_device(index)
|
||||
break
|
||||
if discord.opus.is_loaded():
|
||||
stream = sound.PCMStream()
|
||||
channel = ctx.author.voice.channel
|
||||
|
||||
voice_connection = await channel.connect()
|
||||
voice_connection.play(discord.PCMAudio(stream))
|
||||
stream.change_device(self.DEVICE_ID)
|
||||
|
||||
else:
|
||||
await ctx.send("Opus won't load")
|
||||
voice_connection = await channel.connect()
|
||||
voice_connection.play(discord.PCMAudio(stream))
|
||||
|
||||
@bot.command()
|
||||
async def leave(ctx):
|
||||
await ctx.voice_client.disconnect()
|
||||
else:
|
||||
await ctx.send("Opus won't load")
|
||||
|
||||
@self.command()
|
||||
async def leave(ctx):
|
||||
await ctx.voice_client.disconnect()
|
||||
|
||||
#Enable below for 420 timer
|
||||
#bot.loop.create_task(bg_timer())
|
||||
bot.run(BOT_TOKEN)
|
||||
def check_device(self):
|
||||
for device, index in self.Devices_List:
|
||||
if int(index) == self.DEVICE_ID and str(device) == self.DEVICE_NAME:
|
||||
return True
|
||||
|
||||
# Testing fetch
|
||||
for device, index in self.Devices_List:
|
||||
if str(device) == self.DEVICE_NAME:
|
||||
self.DEVICE_ID = int(index)
|
||||
return True
|
||||
|
||||
else:
|
||||
return False
|
||||
|
||||
156
main.py
Normal file
156
main.py
Normal file
@@ -0,0 +1,156 @@
|
||||
import configparser
|
||||
import signal
|
||||
import sys
|
||||
import os
|
||||
import time
|
||||
import bot
|
||||
import sound
|
||||
from os.path import exists
|
||||
|
||||
# Jorn
|
||||
#token = 'OTE1MDY0OTk2OTk0NjMzNzI5.YaWKsA.Y9yaCGg_VXRL_qQVbs05vo7gSAc'
|
||||
|
||||
# Greada
|
||||
#token = 'NzU2MzI3MjcxNTk3NDczODYz.X2QOqQ.LVLj2b-RXQzPmhNuBC1eGFMcYls'
|
||||
|
||||
#name = "VoiceMeeter Output"
|
||||
|
||||
|
||||
class BotDeviceNotFound(Exception):
|
||||
def __init__(self, device):
|
||||
print(f"Unable to find the device: {device}")
|
||||
try:
|
||||
os.remove('./config.ini')
|
||||
except OSError:
|
||||
print("Config file not found, restarting.")
|
||||
#os.execv(__file__, sys.argv)
|
||||
|
||||
|
||||
|
||||
|
||||
def check_if_config_exists():
|
||||
if exists('./config.ini'):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def read_config_file():
|
||||
config = configparser.ConfigParser()
|
||||
config.read('./config.ini')
|
||||
|
||||
config_return = {
|
||||
'Bot Token': config['Bot_Info']['Token'],
|
||||
'Device ID': int(config['Device']['ID']),
|
||||
'Device Name': str(config['Device']['Name'])
|
||||
}
|
||||
return config_return
|
||||
|
||||
|
||||
def write_config_file(token='', device_id=0, device_name=''):
|
||||
config = configparser.ConfigParser()
|
||||
|
||||
config.add_section('Bot_Info')
|
||||
config.add_section('Device')
|
||||
|
||||
if token == '' and device_id == 0:
|
||||
device_id, device_name = get_user_device_selection()
|
||||
print(device_id)
|
||||
print(device_name)
|
||||
|
||||
token = get_user_token()
|
||||
|
||||
config['Bot_Info']['Token'] = token
|
||||
config['Device']['ID'] = str(device_id)
|
||||
config['Device']['Name'] = str(device_name)
|
||||
|
||||
with open('./config.ini', 'w') as config_file:
|
||||
config.write(config_file)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def get_device_list():
|
||||
return sound.query_devices().items()
|
||||
|
||||
|
||||
def get_user_device_selection():
|
||||
device_list = get_device_list()
|
||||
org_device_list = []
|
||||
for device, dev_id in device_list:
|
||||
print(f"{dev_id + 1}\t-\t{device}")
|
||||
org_device_list.append((dev_id, device))
|
||||
selected_id = None
|
||||
while not selected_id:
|
||||
|
||||
try:
|
||||
selected_id = int(input(f"Please select the input device from above:\t")) - 1
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
continue
|
||||
|
||||
if selected_id and not selected_id + 1 > int(len(device_list)):
|
||||
continue
|
||||
elif selected_id > int(len(device_list)):
|
||||
print("Out of range, try again...")
|
||||
selected_id = None
|
||||
continue
|
||||
else:
|
||||
selected_id = None
|
||||
print("Internal error, try again")
|
||||
continue
|
||||
|
||||
for dev_dict in org_device_list:
|
||||
if dev_dict[0] == selected_id:
|
||||
selected_id = dev_dict
|
||||
|
||||
return selected_id
|
||||
|
||||
|
||||
def get_user_token():
|
||||
token = None
|
||||
while not token:
|
||||
token = str(input(f"Please enter your Discord bot API token now:\t"))
|
||||
if len(token) == 59:
|
||||
return token
|
||||
else:
|
||||
print('Length error in token, please try again...')
|
||||
continue
|
||||
|
||||
|
||||
def main():
|
||||
print('Checking config file...')
|
||||
if not check_if_config_exists():
|
||||
print("No config file exists, please enter this information now")
|
||||
write_config_file()
|
||||
|
||||
config = read_config_file()
|
||||
|
||||
print('Starting Bot...')
|
||||
|
||||
discord_bot_client = bot.Bot(config['Bot Token'], config['Device ID'], config['Device Name'])
|
||||
|
||||
print(f"Verifying audio device:\t{config['Device Name']}")
|
||||
|
||||
if not discord_bot_client.check_device():
|
||||
raise BotDeviceNotFound(config['Device Name'])
|
||||
|
||||
print("Bot started!")
|
||||
|
||||
discord_bot_client.start_bot()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
#main()
|
||||
#atexit.register()
|
||||
try:
|
||||
print('Starting...')
|
||||
while True:
|
||||
try:
|
||||
main()
|
||||
except BotDeviceNotFound:
|
||||
print("Restarting...")
|
||||
time.sleep(2)
|
||||
except KeyboardInterrupt:
|
||||
print("Exiting...")
|
||||
Reference in New Issue
Block a user