- Added BotResources.py - Updates to saving and loading of config file - Updated how you call the save config function - Allowed saving of one variable
131 lines
3.8 KiB
Python
131 lines
3.8 KiB
Python
import sound
|
|
import configparser
|
|
from os.path import exists
|
|
|
|
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']),
|
|
'Mention Group': str(config['Bot_Info']['Mention_Group']),
|
|
'Channel ID': int(config['Bot_Info']['Channel_ID'])
|
|
}
|
|
return config_return
|
|
|
|
|
|
def write_config_file(**kwargs):
|
|
config = configparser.SafeConfigParser()
|
|
|
|
if not kwargs['init'] and exists('./config.ini'):
|
|
config.read('./config.ini')
|
|
|
|
if not config.has_section('Bot_Info'):
|
|
config.add_section('Bot_Info')
|
|
|
|
if not config.has_section('Device'):
|
|
config.add_section('Device')
|
|
|
|
if kwargs['token']:
|
|
config['Bot_Info']['Token'] = kwargs['token']
|
|
elif kwargs['init']:
|
|
config['Bot_Info']['Token'] = get_user_token()
|
|
|
|
if kwargs['device_id'] or kwargs['device_name']:
|
|
config['Device']['ID'] = kwargs['device_id']
|
|
config['Device']['Name'] = kwargs['device_name']
|
|
elif kwargs['init']:
|
|
config['Device']['ID'], config['Device']['Name'] = get_user_device_selection()
|
|
|
|
if kwargs['mention_group']:
|
|
config['Bot_Info']['Mention_Group'] = kwargs['mention_group']
|
|
elif kwargs['init']:
|
|
config['Bot_Info']['Mention_Group'] = get_user_mention_group()
|
|
|
|
if kwargs['channel_id']:
|
|
config['Bot_Info']['Channel_ID'] = kwargs['channel_id']
|
|
elif kwargs['init']:
|
|
config['Bot_Info']['Channel_ID'] = str(get_user_mention_channel_id())
|
|
|
|
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...')
|
|
token = None
|
|
continue
|
|
|
|
|
|
def get_user_mention_group():
|
|
mention_group = None
|
|
while not mention_group:
|
|
mention_group = str(input(f"Please enter the name of the group you would like to mention:\t"))
|
|
return mention_group
|
|
|
|
|
|
def get_user_mention_channel_id():
|
|
channel_id = None
|
|
while not channel_id:
|
|
channel_id = int(input(f"Please enter the channel ID of the the default channel you would like messages to be sent in"))
|
|
if len(str(channel_id)) == len('757379843792044102'):
|
|
return channel_id
|
|
else:
|
|
print("Length error in ID, please try again")
|
|
channel_id = None
|
|
continue |