async def on_ready(self): if not self.loaded: Logging.BOT_LOG_CHANNEL = self.get_channel(Configuration.get_var("log_channel")) Emoji.initialize(self) Logging.info("Connected to discord!") Logging.info("Establishing database connections") await Tortoise.init( db_url="sqlite://db.sqlite3", modules={"models": ["Utils.Models"]} ) await Tortoise.generate_schemas() Logging.info("Database connected") if await NewGameTest.filter().first() is None: to_insert = list() for test in await GameTest.all().prefetch_related("game"): to_insert.append(NewGameTest(game=test.game, message=test.message, end=test.end, status=test.status, feedback=test.feedback)) await NewGameTest.bulk_create(to_insert) Logging.info("Loading cogs") for cog in ["GameTesting"]: try: self.load_extension("Cogs." + cog) except Exception as e: await Utils.handle_exception(f"Failed to load cog {cog}", self, e) Logging.info("Cogs loaded") await Logging.bot_log("GameDjinnie ready to go!") self.loaded = True
def main(): Logging.create_instance("PME.log") Authenticator.create_instance() Database.UserDB.create_instance() Database.DataDB.create_instance() UserComponent.create_instance() MessageParser.create_instance() #tornado config app = tornado.web.Application([ (r"/ws", WS_Handler), (r"/new_user", NewUserHandler), ]) app.listen(8022) tornado.ioloop.IOLoop.instance().start()
async def remove_codes(self, ctx): if len(ctx.message.attachments) is not 1: await ctx.send( "Please send the txt file with codes with the command") else: try: codes = ( await ctx.message.attachments[0].read()).decode().splitlines() except Exception as ex: await ctx.send( "Something went wrong reading that file, please make sure the file is valid and try again" ) Logging.error(ex) else: await GameCode.filter(code__in=codes).delete() await ctx.send(f"Successfully deleted those codes!")
def __init__(self, dbManager, tableName, filters): self.logger = Logging.defaults(__name__); self.dbManager = dbManager; self.tableName = tableName; self.filters = filters;
def __init__(self, dbName): self.logger = Logging.defaults(__name__); self.dbName = dbName; self.setup();
def on_close(self): Logging.instance().log("SERVER: Websocket connection closed.") UserComponent.instance().del_user(self)
def on_message(self, message): user = UserComponent.instance().find(self) if(user): MessageParser.instance().parse(message,user) else: Logging.instance().log("SERVER: rx'd message from unknown client location!!") # this is bad.
def open(self): Logging.instance().log("SERVER: Websocket connection opened.") user = User(self) UserComponent.instance().add_user(self,user)
def parse(self,raw_message,user_obj): jObj = None try: jObj = json.loads(raw_message) except Exception, err: Logging.instance().log("PARSER: Invalid json object with message (%s)" % (raw_message))
def __init__(self): MessageParser.__INST__ = self Logging.instance().log("PARSER: messageparser initialized.")
param = list(ctx.command.params.values())[min(len(ctx.args) + len(ctx.kwargs), len(ctx.command.params))] bot.help_command.context = ctx await ctx.send( f"{Emoji.get_chat_emoji('NO')} You are missing a required command argument: `{param._name}`\n{Emoji.get_chat_emoji('WRENCH')} Command usage: `{bot.help_command.get_command_signature(ctx.command)}`") elif isinstance(error, commands.BadArgument): param = list(ctx.command.params.values())[min(len(ctx.args) + len(ctx.kwargs), len(ctx.command.params))] bot.help_command.context = ctx await ctx.send( f"{Emoji.get_chat_emoji('NO')} Failed to parse the ``{param._name}`` param: ``{error}``\n{Emoji.get_chat_emoji('WRENCH')} Command usage: `{bot.help_command.get_command_signature(ctx.command)}`") elif isinstance(error, commands.CommandNotFound): return else: await Utils.handle_exception("Command execution failed", bot, error.original if hasattr(error, "original") else error, ctx=ctx) # notify caller e = Emoji.get_chat_emoji('BUG') if ctx.channel.permissions_for(ctx.me).send_messages: await ctx.send(f"{e} Something went wrong while executing that command {e}") if __name__ == '__main__': Logging.init() Configuration.load() Logging.info("Did someone summon the GameDjinnie?") bot = GameJinnie(command_prefix=Configuration.get_var("prefix"), case_insensitive=True) bot.run(Configuration.get_var("token")) Logging.info("GameDjinnie shutdown")
def Train(): train_data, valid_data = DataLoader.Load_Divided_Data(train_data_file) n_train, n_valid = train_data.shape[0], valid_data.shape[0] print('*DATA-LOADING: FINISHED') """ Prepare folders and files for saving information of the training process """ MAKE_FOLDERS_FOR_SAVING() if train_cfg.LOGGING: model_description='LR=%f, 1-epoch=%d, batch=%d, n-train=%d, n-validation=%d'%\ (train_cfg.LEARNING_RATE,train_cfg.N_ITER,train_cfg.BATCH_SIZE, n_train, n_valid) Logging.CREATE_LOG(log_training, Model.MODEL_DESCRIPTION, msg=' Training: ' + model_description + '\n' + params_text) Logging.CREATE_LOG(log_validating, Model.MODEL_DESCRIPTION, msg=' Validating: ' + model_description + '\n' + params_text) if train_cfg.SUMMARY: train_sum_writer = tf.summary.FileWriter(summary_dir + '/train') valid_sum_writer = tf.summary.FileWriter(summary_dir + '/validation') mse_summary = tf.Summary( value=[tf.Summary.Value(tag='MSE', simple_value=None)]) """ Prepare hyper-parameters """ min_valid = 10.0 train_lr = train_cfg.LEARNING_RATE """ ======================================================================= """ sess_cfg = tf.ConfigProto() sess_cfg = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) # sess_cfg.gpu_options.allow_growth = True # sess_cfg.gpu_options.per_process_gpu_memory_fraction = Configs.GPU_MEM_FRACTION with tf.Session(graph=graph, config=sess_cfg) as sess: tf.global_variables_initializer().run() # checkpoint_saver.restore(sess, save_dir+'/training-checkpoint') print('*PREPARATION is finished. TRAINING is starting now...') start_time = datetime.now() for iEpoch in range(0, train_cfg.N_EPOCHS): if (iEpoch + 1) % train_cfg.DECAY_STEP == 0: if train_lr > 1e-12: train_lr = train_lr * train_cfg.DECAY_RATE """ Training Epoch starts """ iter_loss, iter_mse = 0, 0 epoch_data = TrainOps.Train_Data(train_data, n_train, n_train) if AUGMENTATION: epoch_data = TrainOps.Numpy_Augment(epoch_data) for iBatch in range(0, n_train, train_cfg.BATCH_SIZE): train_batch = epoch_data[iBatch:iBatch + train_cfg.BATCH_SIZE] if train_cfg.SUMMARY: merged_summary = tf.summary.merge_all() __, loss, err, summary = sess.run( [train_step, train_cost, train_mse, merged_summary], feed_dict={ img_seq: train_batch, lr: train_lr }) else: __, loss, err = sess.run( [train_step, train_cost, train_mse], feed_dict={ img_seq: train_batch, lr: train_lr }) iter_mse += err iter_loss += loss # print('Successful Training Iter:', iEpoch+1, loss, err) iter_mse /= train_cfg.N_ITER iter_loss /= train_cfg.N_ITER """ Epoch finishs """ if iEpoch == 0 or (iEpoch + 1) % train_cfg.LOG_STEP == 0: text = ' -Epoch %5d: Loss=%f; MSE=%f\n'%(iEpoch+1, iter_loss, iter_mse) + \ ' Time passed: %s'%(datetime.now() - start_time) if train_cfg.VERBOSE: print(text) if train_cfg.LOGGING: Logging.WRITE_LOG(log_training, msg=text) if train_cfg.SAVING: checkpoint_saver.save(sess, save_dir + '/training-checkpoint') if train_cfg.SUMMARY: mse_summary.value[0].simple_value = iter_mse train_sum_writer.add_summary(mse_summary, iEpoch + 1) train_sum_writer.add_summary(summary, iEpoch + 1) """ Validation starts """ if iEpoch == 0 or (iEpoch + 1) % train_cfg.VALIDATE_STEP == 0: valid_cost, valid_mse = Validating(sess, valid_data, n_valid) text = ' -Epoch %5d: Cost=%f; MSE=%f' % (iEpoch + 1, valid_cost, valid_mse) if train_cfg.LOGGING: Logging.WRITE_LOG(log_validating, msg=text) if train_cfg.SAVING: model_saver.save(sess, save_dir_best + '/model', global_step=(iEpoch + 1)) """ Validation finishs """ end_time = datetime.now() if train_cfg.SUMMARY: train_sum_writer.close() valid_sum_writer.close() print(' - TOTAL TRAINING TIME:', end_time - start_time) """ ======================================================================= """
async def handle_exception(exception_type, bot, exception, event=None, message=None, ctx=None, *args, **kwargs): embed = Embed(colour=Colour(0xff0000), timestamp=datetime.utcfromtimestamp(time.time())) # something went wrong and it might have been in on_command_error, make sure we log to the log file first lines = [ "\n===========================================EXCEPTION CAUGHT, DUMPING ALL AVAILABLE INFO===========================================", f"Type: {exception_type}" ] arg_info = "" for arg in list(args): arg_info += extract_info(arg) + "\n" if arg_info == "": arg_info = "No arguments" kwarg_info = "" for name, arg in kwargs.items(): kwarg_info += "{}: {}\n".format(name, extract_info(arg)) if kwarg_info == "": kwarg_info = "No keyword arguments" lines.append("======================Exception======================") lines.append(f"{str(exception)} ({type(exception)})") lines.append("======================ARG INFO======================") lines.append(arg_info) lines.append("======================KWARG INFO======================") lines.append(kwarg_info) lines.append("======================STACKTRACE======================") tb = "".join(traceback.format_tb(exception.__traceback__)) lines.append(tb) if message is None and event is not None and hasattr(event, "message"): message = event.message if message is None and ctx is not None: message = ctx.message if message is not None and hasattr(message, "content"): lines.append( "======================ORIGINAL MESSAGE======================") lines.append(message.content) if message.content is None or message.content == "": content = "<no content>" else: content = message.content embed.add_field(name="Original message", value=trim_message(content, 1000), inline=False) lines.append( "======================ORIGINAL MESSAGE (DETAILED)======================" ) lines.append(extract_info(message)) if event is not None: lines.append("======================EVENT NAME======================") lines.append(event) embed.add_field(name="Event", value=event) if ctx is not None: lines.append( "======================COMMAND INFO======================") lines.append(f"Command: {ctx.command.name}") embed.add_field(name="Command", value=ctx.command.name) channel_name = 'Private Message' if isinstance( ctx.channel, PrivateChannel) else f"{ctx.channel.name} (`{ctx.channel.id}`)" lines.append(f"Channel: {channel_name}") embed.add_field(name="Channel", value=channel_name, inline=False) sender = f"{str(ctx.author)} (`{ctx.author.id}`)" lines.append(f"Sender: {sender}") embed.add_field(name="Sender", value=sender, inline=False) lines.append( "===========================================DATA DUMP COMPLETE===========================================" ) Logging.error("\n".join(lines)) for t in [ConnectionClosed, ClientOSError, ServerDisconnectedError]: if isinstance(exception, t): return # nice embed for info on discord embed.set_author(name=exception_type) embed.add_field(name="Exception", value=f"{str(exception)} (`{type(exception)}`)", inline=False) if len(tb) < 1024: embed.add_field(name="Traceback", value=tb) else: embed.add_field(name="Traceback", value="stacktrace too long, see logs") try: await Logging.bot_log(embed=embed) except Exception as ex: Logging.error( f"Failed to log to botlog, either Discord broke or something is seriously wrong!\n{ex}" ) Logging.error(traceback.format_exc())