async def on_command_error(ctx: DiscordContext, exc): if isinstance(exc, CommandOnCooldown): await send_error_msg( ctx, f"You're memeing too fast! Please wait {int(exc.retry_after)} seconds." ) elif isinstance(exc, DisabledCommand): await send_error_msg(ctx, f"`{ctx.command}` is disabled here") elif isinstance(exc, MissingPermissions): await send_error_msg( ctx, f"You need the following permissions to use this command: `{', '.join(exc.missing_perms)}`" ) elif isinstance(exc, BotMissingPermissions): await send_error_msg( ctx, f"The bot needs the following permissions for this command: `{', '.join(exc.missing_perms)}`" ) elif isinstance(exc, CheckFailure): return elif isinstance(exc, MissingRequiredArgument) and ctx.can_respond: await ctx.send_help(ctx.command) elif isinstance(exc, CommandInvokeError): log_exc(exc, ctx) await send_error_msg(ctx, "error :cry:") elif isinstance(exc, CommandError): if settings.DEBUG: log_exc(exc, ctx) await send_error_msg(ctx, str(exc) or "error :cry:")
def found_terminator(S): # First clear the pending data out cmd = S.next_cmd S.next_cmd = "" # Next clear out stray \r's cmd = string.replace(cmd, "\r", "") # Then dispatch it try: S.conn_obj.comm_readline(cmd) except: util.log_exc()
def found_terminator(S): # First clear the pending data out cmd = S.next_cmd S.next_cmd = '' # Next clear out stray \r's cmd = string.replace(cmd, '\r', '') # Then dispatch it try: S.conn_obj.comm_readline(cmd) except: util.log_exc()
def close(S): if S.conn_obj: # Tell our softcode friend about it del cmap[S.conn_obj.__name__] try: S.conn_obj.comm_close() except: util.log_exc() S.conn_obj = None # Really do the close. AC.close(S)
def __init__(S, conn, peer, klass): AC.__init__(S, conn) S.next_cmd = '' S.terminator = '\n' try: # Build the softcode object to handle this connection S.conn_obj = klass(peer) cmap[S.conn_obj.__name__] = S S.conn_obj.start() except: S.conn_obj = None util.log_exc() S.close() raise
def __init__(S, conn, peer, klass): AC.__init__(S, conn) S.next_cmd = "" S.terminator = "\n" try: # Build the softcode object to handle this connection S.conn_obj = klass(peer) cmap[S.conn_obj.__name__] = S S.conn_obj.start() except: S.conn_obj = None util.log_exc() S.close() raise
def refresh(path): # Remember the old class in case of errors try: oldclass = getclass(path) except: oldclass = None # Try to load the new version # .. clear cache try: del ccache[path] except: pass try: # .. and refetch (this might raise compile errors etc) newclass = getclass(path) # Successfully loaded. Now update any cached object instances based # on it # Note! We can't let any exceptions out of here once we # upgrade an object! be careful.. for i in ocache.keys(): if i[:len(path) + 1] == path + '%': # Instance of class if not newclass._class: # ugh. There are instances without a class now. help! raise LoadError, path + ": instances exist in cache, but new code doesn't allow them" # Set the new class, then upgrade o = ocache[i] o.__class__ = newclass._class if hasattr(o, 'loadhook'): try: o.loadhook() except: util.log_exc() except: # Help! Help! Something went wrong. Restore what we can. # .. restore cache if oldclass: ccache[path] = oldclass raise
def run(): try: main = db.soft_sys.main() main.init() except: util.log_exc() sys.exit(1) # don't sync.. s_next = 0.0 t_last = time() # t = now # t_last = start of last tick # next = scheduled start of next tick # s_next = scheduled time of next cache sync try: while 1: try: main.tick() except: util.log_exc() # Calculate next tick time next = t_last + tick_t # Wait for next tick, doing I/O t = time() while t < next: comm.idle(next - t) t = time() # Don't use t; we don't want drift here t_last = next # Check for cache sync if t > s_next: s_next = t + sync_t db.sync() except: db.sync() raise
def refresh(path): # Remember the old class in case of errors try: oldclass = getclass(path) except: oldclass = None # Try to load the new version # .. clear cache try: del ccache[path] except: pass try: # .. and refetch (this might raise compile errors etc) newclass = getclass(path) # Successfully loaded. Now update any cached object instances based # on it # Note! We can't let any exceptions out of here once we # upgrade an object! be careful.. for i in ocache.keys(): if i[:len(path)+1] == path + '%': # Instance of class if not newclass._class: # ugh. There are instances without a class now. help! raise LoadError, path + ": instances exist in cache, but new code doesn't allow them" # Set the new class, then upgrade o = ocache[i] o.__class__ = newclass._class if hasattr(o, 'loadhook'): try: o.loadhook() except: util.log_exc() except: # Help! Help! Something went wrong. Restore what we can. # .. restore cache if oldclass: ccache[path] = oldclass raise
def generate(cls, image_pools, queue_id, template=None, saveme=True): if template is None: templ = MemeTemplate.next(image_pools, queue_id) else: templ = template source_files = {} prev_slot_id = None source_file = None for slot in templ.memetemplateslot_set.order_by('slot_order').all(): if slot.slot_order == prev_slot_id: source_files[slot.slot_order] = source_file.name continue # pick source file that hasn't been used attempts = 0 while True: source_file = MemeSourceImage.next(image_pools, queue_id) if source_file.name not in source_files.values(): break else: attempts += 1 if attempts > 5: raise NotEnoughImages( 'Not enough source images for template ' + templ.name) source_files[slot.slot_order] = source_file.name prev_slot_id = slot.slot_order meem = Meem(template_link=templ, source_images=json.dumps(source_files)) if saveme: succ = False while not succ: try: meem.save() succ = True except IntegrityError as exc: log_exc(exc) meem.number += 1 return meem
def handle_error(S, *info): util.log_exc(info)
def sync(): for i in ocache.keys(): try: ocache[i].save() except: util.log_exc()
d['__name__'] = path d['__instance__'] = inst except IOError, e: if e[0] != ENOENT: raise raise NoSuchObject, path # Construct the new instance with given dict i = ocache[path] = new.instance(c._class, d) # Call loadhook if it's there to upgrade the object if hasattr(i, 'loadhook'): try: i.loadhook() except: util.log_exc() # might as well continue on errors? return i # Permanently destroy an instance def destroy(obj): if isinstance(obj, Object): klass = obj.__class__.__name__ inst = obj.__instance__ elif isinstance(obj, lazy): klass = obj._class inst = obj._inst else: raise RuntimeError, "can only destroy Object instances"
# Make sure the data is sane (this can go wrong if things are renamed) d['__name__'] = path d['__instance__'] = inst except IOError, e: if e[0] != ENOENT: raise raise NoSuchObject, path # Construct the new instance with given dict i = ocache[path] = new.instance(c._class, d) # Call loadhook if it's there to upgrade the object if hasattr(i, 'loadhook'): try: i.loadhook() except: util.log_exc() # might as well continue on errors? return i # Permanently destroy an instance def destroy(obj): if isinstance(obj, Object): klass = obj.__class__.__name__ inst = obj.__instance__ elif isinstance(obj, lazy): klass = obj._class inst = obj._inst else: raise RuntimeError, "can only destroy Object instances"