async def on_command_error(self, context, exception): if isinstance(exception, commands.BadUnionArgument): msg = "Could not find the specified " + str([c.__name__ for c in exception.converters]) await context.trigger_typing() await context.send(embed=embeds.error(msg)) elif isinstance(exception, commands.BadArgument): await context.trigger_typing() await context.send( embed=embeds.error(str(exception)) ) elif isinstance(exception, commands.CommandNotFound): print("CommandNotFound: " + str(exception)) elif isinstance(exception, commands.MissingRequiredArgument): await context.send_help(context.command) elif isinstance(exception, commands.CommandOnCooldown): await context.send( embed=embeds.error(f"This command is on cooldown. Try again in {exception.retry_after:.2f}s.")) else: print("Unexpected exception: " + str(exception))
async def source(self, ctx: SlashContext, command: str = None): source_url = "https://github.com/RealCyGuy/Bronze-Medalist" if command is None: embed = discord.Embed(title="Bronze Medalist's Source Code", description=source_url + "\n\n" + "To get source for a specific command, use " "`/source {command}`", colour=Colours.BRONZE) await ctx.send(embed=embed) return not_found = embeds.error("Command not found!") obj = self.bot.get_command(command) if obj is None: return await ctx.send(embed=not_found) src = obj.callback.__code__ lines, file_start = inspect.getsourcelines(src) sourcecode = inspect.getsource(src).replace("```", "") branch = "master" if obj.callback.__module__.startswith("discord"): location = obj.callback.__module__.replace(".", "/") + ".py" source_url = "https://github.com/Rapptz/discord.py" elif obj.callback.__module__.startswith("jishaku"): location = obj.callback.__module__.replace(".", "/") + ".py" source_url = "https://github.com/Gorialis/jishaku" else: location = os.path.relpath(src.co_filename).replace("\\", "/") branch = "prod" if obj.callback.__module__.startswith( "cogs.events") or obj.callback.__module__.startswith( "jishaku"): prefix = self.bot.command_prefix else: prefix = "/" embed = discord.Embed(title=f"Source code of {prefix}{command}.", colour=Colours.BRONZE) sourcecode = sourcecode.splitlines(True) for index, line in enumerate(sourcecode): if line.startswith(" " * 4): sourcecode[index] = line[4:] sourcecode = "".join(sourcecode) file_end = file_start + len(lines) - 1 if len(sourcecode) > 1900: embed.description = "{}/blob/{}/{}#L{}-L{}".format( source_url, branch, location, file_start, file_end) else: embed.description = "<{}/blob/{}/{}#L{}-L{}>\n```py\n{}```".format( source_url, branch, location, file_start, file_end, sourcecode) await ctx.send(embed=embed)
async def send_error_message(self, error): command = self.context.kwargs.get("command") command_names = set() for cmd in self.context.bot.walk_commands(): if not cmd.hidden: command_names.add(cmd.qualified_name) closest = get_close_matches(command, command_names, 2) if not closest: closest = get_close_matches(command, command_names, 1, 0) closest = "` or `".join(closest) await self.get_destination().send(embed=embeds.error( f"Command `{command}` not found. Did you mean `{closest}`?"))
async def paste(self, ctx): """ Pastes the copied text. ~ {prefix}paste """ clipboard = await self.bot.get_clipboard() copied = clipboard["copied"] if str(ctx.author.id) in copied: embed = discord.Embed(description=copied[str(ctx.author.id)], colour=discord.Colour.gold()) embed.set_author( name=f"{ctx.author.name}#{ctx.author.discriminator}", icon_url=ctx.author.avatar_url) else: embed = embeds.error( "You don't have anything copied to clipboard.") embed.set_footer(text="Use the help command for more info.") await ctx.send(embed=embed)
async def clear(self, ctx): """ Clear your clipboard! ~ {prefix}clear """ clipboard = await self.bot.get_clipboard() copied = clipboard["copied"] try: copied.pop(str(ctx.author.id)) await self.bot.clipboard.find_one_and_update( {"_id": "clipboard"}, {"$set": { "copied": copied }}, upsert=True, ) await ctx.send(embed=embeds.success("Cleared clipboard.")) except KeyError: await ctx.send(embed=embeds.error("Nothing in clipboard."))
async def feedback(self, ctx, *, feedback): """ Send feedback about the bot. ~ {prefix}feedback this bot is very good. {prefix}feedback I have a command idea... """ url = os.environ.get("FEEDBACK_WEBHOOK", None) if url: webhook = Webhook.from_url(url, adapter=RequestsWebhookAdapter()) embed = discord.Embed(description=feedback, colour=discord.Colour.teal()) embed.set_author( name=f"{ctx.author.name}#{ctx.author.discriminator}", icon_url=ctx.author.avatar_url) embed.set_footer(text=f"User id: {ctx.author.id}") webhook.send(embed=embed) await ctx.send(embed=embeds.success("Sent the feedback!")) else: await ctx.send(embed=embeds.error("This command is disabled."))
async def prefix(self, ctx, new_prefix=None): """ View and change the prefix. ~ {prefix}prefix {prefix}prefix ? {prefix}prefix thisisthenewprefix ~ Use without param to view the prefix. """ if new_prefix is not None: if ctx.guild is None: await ctx.send(embed=embeds.error( "You can only change prefixes in a server.")) else: if ctx.author.permissions_in(ctx.channel).manage_guild: user_id = self.bot.user.id if new_prefix not in [f'<@{user_id}>', f'<@!{user_id}>']: if len(new_prefix) < 20: config = await self.bot.get_config() prefixes = config["prefixes"] prefixes[str(ctx.guild.id)] = new_prefix await self.bot.config.find_one_and_update( {"_id": "config"}, {"$set": { "prefixes": prefixes }}, upsert=True, ) embed = embeds.success( f"Changed prefix to `{new_prefix}`.") embed.set_footer(text="You can always mention me!") await ctx.send(embed=embed) else: await ctx.send(embed=embeds.error( "Prefix must be under 20 characters.")) else: await ctx.send( embed=embeds.error("Mentions are already usable.")) else: await ctx.send(embed=embeds.error( "You need the `manage_guild` permission to do this.")) else: embed = discord.Embed(title=f"Command Prefixes", colour=discord.Colour.dark_magenta()) default_prefix = os.environ.get("DEFAULT_PREFIX", "c!") embed.add_field(name="Default Prefix", value=code(default_prefix), inline=True) config = await self.bot.get_config() prefixes = config["prefixes"] if ctx.guild is not None: server_prefix = prefixes.get(str(ctx.guild.id), default_prefix) embed.add_field(name="Server Prefix", value=code(server_prefix), inline=True) dm_prefixes = [code(default_prefix)] for guild in self.bot.guilds: if str(guild.id) in prefixes and ctx.author in guild.members: dm_prefixes.append(code(prefixes[str(guild.id)])) embed.add_field(name=ctx.author.name + "'s DM prefixes.", value=", ".join(dm_prefixes)) embed.set_footer(text="You can always mention me!") await ctx.send(embed=embed)