def __init__(self): if not os.path.isfile("config/config.ini"): if not os.path.isfile("config/config.ini.example"): log.critical("There is no \"config.ini.example\" file in the \"config\" folder! Please go to the github repo and download it and then put it in the \"config\" folder!") os._exit(1) else: shutil.copy("config/config.ini.example", "config/config.ini") log.warning("Created the \"config.ini\" file in the config folder! Please edit the config and then run the bot again!") os._exit(1) self.config_file = "config/config.ini" config = configparser.ConfigParser(interpolation=None) config.read(self.config_file, encoding="utf-8") sections = {"Credentials", "Bot"}.difference(config.sections()) if sections: log.critical("Could not load a section in the config file, please obtain a new config file from the github repo if regenerating the config doesn't work!") os._exit(1) self._token = config.get("Credentials", "Token", fallback=Defaults.token) self.owner_id = config.get("Bot", "Owner_ID", fallback=Defaults.owner_id) self.command_prefix = config.get("Bot", "Command_Prefix", fallback=Defaults.prefix) self.check()
def language_recognition_save_to_file(suffix=""): for clean_file_path in glob.glob( join(config.test_data_cleaned_text_dir(suffix="-UK-keyword2"), '*.txt')): result_file_path = join( config.language_understanding_result_dir(suffix=suffix), basename(clean_file_path)) if exists(result_file_path): log.info(f'already exists... skipping:\t {result_file_path}') continue content = open(clean_file_path).read() long, short = analyse_text(content) if long.get('error'): log.warning(f'error occurred... skipping:\t {result_file_path}') continue long = str(long) short = str(short) open(result_file_path, 'w+').write(long) open( join(config.language_understanding_dir(), 'result' + suffix + '.txt'), 'a').write(long + '\n') log.info("File written: %s" % result_file_path) log.info("Cleaned Text: %s" % short)
async def blacklist(ctx, id: str, *, reason: str): """Blacklist an user""" await bot.send_typing(ctx.message.channel) user = discord.utils.get(list(bot.get_all_members()), id=id) if user is None: await bot.say("Could not find a user with an id of `{}`".format(id)) return if getblacklistuser(id) != None: await bot.say("`{}` is already blacklisted".format(user)) return blacklistuser(id, user.name, user.discriminator, reason) await bot.say("Successfully blacklisted `{}`".format(user)) try: await bot.send_message( user, "You have been blacklisted by `{}` - Reason `{}`".format( ctx.message.author, reason)) except: log.warning("Cannot send message to `{}`".format(user)) fields = { "ID": id, "Name": user.name, "Discriminator": user.discriminator, "Reason": reason, "Time": i.strftime('%H:%M:%S %d/%m/%Y') } embed = make_list_embed(fields) embed.title = ":warning: Blacklist :warning:" embed.color = 0xFF0000 await logger.embed_mod_log(ctx.message.server, embed)
def synthesize_monster(self) -> []: self.check_eliminate() temp = [] for direct in self.direct_states: temp.extend(self.direct_states[direct]) if len(temp) == 0: return [] result = {} for state in temp: key = (state.ref_id, state.order) if key not in result: result[key] = state else: result[key].indexes.extend(state.indexes) final_index = [] remove_indexes = None for (ref_id, order) in result: if order == OrderType.A: log.warning('up the top order %s %s' % (ref_id, result[(ref_id, order)])) continue value = result[(ref_id, order)] indexes = set(value.indexes) target_index, target_level = self.get_synthesize_index_level(indexes) log.debug('synthesize unit: %s %s %s target_index %s' % (ref_id, order, indexes, target_index)) final_index.append(target_index) monster = Monster(target_index, ref_id, order.up(), level=target_level) self.grid[target_index] = monster self.record_soldier(ref_id, order, target_level) if remove_indexes is None: remove_indexes = indexes else: remove_indexes = indexes.union(remove_indexes) log.info('synthesize:%s remove:%s' % (final_index, remove_indexes)) if remove_indexes is None: return [] result = [] for index in remove_indexes: if index not in final_index: result.append(index) log.debug('remove=%s' % result) return result
def test_swap(self): grid = Grid() grid.init_generate_grid() grid.simple_show() result = grid.swap_by_strategy(StrategyType.HIGH_ORDER_FIRST) if len(result) == 0: log.warning('no swap') return for i in result: log.info('swap %s' % i) grid.swap_and_eliminate(result) grid.show()
def main_loop(self, loop, strategy_type): self.init_generate_grid() i = 0 for i in range(loop): log.info('loop %s' % i) self.table_show() cells = self.swap_by_strategy(strategy_type) if len(cells) == 0: log.warning('can\'t find any swap plan') break log.info('best plan %s' % str(cells)) self.swap_and_eliminate(cells) log.warning('complete loop: %s' % i) self.show()
def clean_text_save_to_file(): for text_file_path in glob.glob(join(config.test_data_text_dir(), '*.txt')): clean_file_path = join(config.test_data_cleaned_text_dir(), basename(text_file_path)) if exists(clean_file_path): continue content = open(text_file_path).read() text = clean_up_text(content) if not text: log.warning("Failed to clean up \"%s\" (File %s)" % (content, text_file_path)) continue open(clean_file_path, 'w+').write(text) log.info("File written: %s" % text_file_path) log.info("Cleaned Text: %s" % text)
async def unblacklist(ctx, id: str): """Unblacklist an user""" user = getblacklistuser(id) if user is None: await bot.say( "No blacklisted user can be found with an id of `{}`".format(id)) return try: unblacklistuser(id) except: await bot.say( "No blacklisted user can be found with an id of `{}`".format(id)) await bot.say("Successfully unblacklisted `{}#{}`".format( user.get("name"), user.get("discrim"))) try: await bot.send_message( discord.User(id=id), "You have been unblacklisted by `{}`".format(ctx.message.author)) except: log.warning("Cannot send message to `{}`".format(id))
def convert_audio_files(): if not which("ffmpeg"): log.warning("Couldn't find ffmpeg") error_files = [] for original_audio_file_path in glob.glob( os.path.join(AUDIO_DIR(), '*.wav')): try: for speed in []: _convert_audio(original_audio_file_path, speed) for speed in []: _convert_audio_with_normalisation(original_audio_file_path, speed) _convert_audio_with_noise_injection(original_audio_file_path, 0.01) _convert_audio_with_noise_injection(original_audio_file_path, 0.03) _convert_audio_with_noise_injection(original_audio_file_path, 0.05) _convert_audio_with_noise_injection(original_audio_file_path, 0.07) _convert_audio_with_noise_injection(original_audio_file_path, 0.09) _convert_audio_with_noise_injection(original_audio_file_path, 0.1) #_convert_audio_with_random_speed(original_audio_file_path, random.choice(np.arange(0.7, 1.3, 0.1))) except: error_files.append(original_audio_file_path) log.warning(f'Could not convert {original_audio_file_path}') if len(error_files) != 0: log.warning(f'Could not convert {error_files}')
def speech_to_text(audio_file_path): data = None cnt = 0 # if data is None HTTP Request failed, # The cause is probably because there is a limit of how many Request can be sent, # but since it looks random we'll just try again when it happens... while data is None and cnt < 5: data = _send_request(audio_file_path) cnt += 1 if data is None: log.warning(f'try number {cnt} failed for {audio_file_path}') time.sleep(0.5) if data is None: log.warning(f'{audio_file_path} had an HTTP Request failure') return if data.get('RecognitionStatus') != 'Success': logwarning = f'{audio_file_path} has an empty transcript' if data.get('RecognitionStatus') is not None: logwarning += ', RecognitionStatus = ' + data.get( 'RecognitionStatus') log.warning(logwarning) return transcript = data.get('NBest')[0].get('Display') log.info("Transcript: %s" % transcript) return transcript
async def shutdown(ctx): """Stop the bot""" await bot.say("Shutting down...") log.warning("{} has stopped the bot!".format(ctx.message.author)) await _shutdown_bot()
async def restart(ctx): """Restart the bot""" await bot.say("Restarting...") log.warning("{} has restarted the bot!".format(ctx.message.author)) await _restart_bot()
def up(self): if self.value == self.A.value: log.warning('want to up than A') return self return OrderType(self.value + 1)