- Updates to bot responses
- Added BotResources.py - Updates to saving and loading of config file - Updated how you call the save config function - Allowed saving of one variable
This commit is contained in:
102
main.py
102
main.py
@@ -1,9 +1,7 @@
|
||||
import configparser
|
||||
import os
|
||||
import time
|
||||
import bot
|
||||
import sound
|
||||
from os.path import exists
|
||||
from BotResources import check_if_config_exists, write_config_file, read_config_file
|
||||
|
||||
# Jorn
|
||||
#token = 'OTE1MDY0OTk2OTk0NjMzNzI5.YaWKsA.Y9yaCGg_VXRL_qQVbs05vo7gSAc'
|
||||
@@ -24,110 +22,18 @@ class BotDeviceNotFound(Exception):
|
||||
#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()
|
||||
write_config_file(init=True)
|
||||
|
||||
config = read_config_file()
|
||||
|
||||
print('Starting Bot...')
|
||||
|
||||
discord_bot_client = bot.Bot(config['Bot Token'], config['Device ID'], config['Device Name'])
|
||||
discord_bot_client = bot.Bot(config['Bot Token'], config['Device ID'], config['Device Name'],
|
||||
config['Mention Group'], config['Channel ID'])
|
||||
|
||||
print(f"Verifying audio device:\t{config['Device Name']}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user