155 lines
3.7 KiB
Python
155 lines
3.7 KiB
Python
import configparser
|
|
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...")
|