151 lines
4.5 KiB
Python
151 lines
4.5 KiB
Python
import sound
|
|
import configparser
|
|
from os.path import exists
|
|
|
|
def check_if_config_exists():
|
|
if exists('./config.ini'):
|
|
config = configparser.SafeConfigParser()
|
|
config.read('./config.ini')
|
|
if config.has_section('Bot_Info') and config.has_section('Device'):
|
|
return True
|
|
else:
|
|
return False
|
|
else:
|
|
return False
|
|
|
|
|
|
def read_config_file():
|
|
config = configparser.ConfigParser()
|
|
config.read('./config.ini')
|
|
|
|
config_return = {
|
|
'Token': config['Bot_Info']['Token'],
|
|
'Device ID': int(config['Device']['ID']),
|
|
'Device Name': str(config['Device']['Name']),
|
|
}
|
|
if config.has_option('Bot_Info', 'Mention_Group'): #'Mention Group' in config['Bot_Info'].items():
|
|
config_return['Mention Group'] = str(config['Bot_Info']['Mention_Group'])
|
|
|
|
if 'Channel_ID' in config['Bot_Info'].keys():
|
|
config_return['Channel ID'] = int(config['Bot_Info']['Channel_ID'])
|
|
|
|
print("Found config options:")
|
|
for key in config_return.keys():
|
|
print(f"\t{key} : {config_return[key]}")
|
|
|
|
return config_return
|
|
|
|
|
|
def write_config_file(**kwargs):
|
|
config = configparser.SafeConfigParser()
|
|
|
|
if 'init' not in kwargs.keys() 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 'token' in kwargs.keys():
|
|
config['Bot_Info']['Token'] = str(kwargs['token'])
|
|
elif 'init' in kwargs.keys():
|
|
config['Bot_Info']['Token'] = str(get_user_token())
|
|
|
|
if 'device_id' in kwargs.keys() or 'device_name' in kwargs.keys():
|
|
config['Device']['ID'] = str(kwargs['device_id'])
|
|
config['Device']['Name'] = str(kwargs['device_name'])
|
|
elif 'init' in kwargs.keys():
|
|
device_id, device_name = get_user_device_selection()
|
|
config['Device']['ID'] = str(device_id)
|
|
config['Device']['Name'] = str(device_name)
|
|
|
|
if 'mention_group' in kwargs.keys():
|
|
config['Bot_Info']['Mention_Group'] = str(kwargs['mention_group'])
|
|
elif 'init' in kwargs.keys():
|
|
config['Bot_Info']['Mention_Group'] = str(get_user_mention_group())
|
|
|
|
if 'channel_id' in kwargs.keys():
|
|
config['Bot_Info']['Channel_ID'] = str(kwargs['channel_id'])
|
|
elif 'init' in kwargs.keys():
|
|
config['Bot_Info']['Channel_ID'] = str(get_user_mention_channel_id())
|
|
|
|
try:
|
|
with open('./config.ini', 'w') as config_file:
|
|
config.write(config_file)
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
print("Config Saved")
|
|
|
|
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:\t"))
|
|
if len(str(channel_id)) == len('757379843792044102'):
|
|
return channel_id
|
|
else:
|
|
print("Length error in ID, please try again")
|
|
channel_id = None
|
|
continue |