async def on_message(message: discord.Message): """ Perform lambda commands. """ args = utils.split(message.content) # Check if the command is a lambda command and is not disabled (in the blacklist) if args[0] in lambdas.data and args[0] not in lambda_config.data[ "blacklist"]: def arg(i, default=0): if len(args) > i: return args[i] else: return default code_globals.update( dict(arg=arg, args=args, message=message, client=client, author=message.author, server=message.server, channel=message.channel)) python_code = lambdas.data[args[0]] # Create an async function so that we can await it using the result of eval python_code = "async def lambda_session():\n " + "\n ".join( line for line in python_code.split("\n")) try: exec(python_code, code_globals) except SyntaxError as e: if plugins.is_owner(message.author): await client.say(message, "```" + utils.format_syntax_error(e) + "```") else: logging.warning( "An exception occurred when parsing lambda command:" "\n{}".format(utils.format_syntax_error(e))) return True # Execute the command try: await eval("lambda_session()", code_globals) except AssertionError as e: # Send assertion errors to the core module raise AssertionError(e) except Exception as e: if plugins.is_owner(message.author): await client.say(message, "```" + utils.format_exception(e) + "```") else: logging.warning( "An exception occurred when parsing lambda command:" "\n{}".format(utils.format_exception(e))) return True
async def unlink(message: discord.Message, member: discord.Member = Annotate.Self): """ Unlink your osu! account or unlink the member specified (**Owner only**). """ # The message author is allowed to unlink himself # If a member is specified and the member is not the owner, set member to the author if not plugins.is_owner(message.author): member = message.author # The member might not be linked to any profile assert member.id in osu_config.data[ "profiles"], "No osu! profile assigned to **{}**!".format(member.name) # Unlink the given member (usually the message author) del osu_config.data["profiles"][member.id] osu_config.save() await client.say(message, "Unlinked **{}'s** osu! profile.".format(member.name))
async def execute_command(command: plugins.Command, message: discord.Message, *args, **kwargs): """ Execute a command and send any AttributeError exceptions. """ app_info = await client.application_info() try: await command.function(message, *args, **kwargs) except AssertionError as e: await client.say( message, str(e) or command.error or plugins.format_help(command, message.server)) except: traceback.print_exc() if plugins.is_owner(message.author) and config.owner_error: await client.say(message, utils.format_code(traceback.format_exc())) else: await client.say( message, "An error occurred while executing this command. If the error persists, " "please send a PM to {}.".format(app_info.owner))