async def _edit(self, ctx, command, *, new_content): """ {command_prefix}cc edit [command] [new_content] {help} """ command = command.lower() settings = await SettingsDB.get_instance().get_guild_settings( ctx.guild.id) if not settings.customcommands: await ctx.send( get_str(ctx, "cmd-customcommand-raw-no-cc").format( "`{}customcom add`".format( get_server_prefixes(ctx.bot, ctx.guild)))) elif command in settings.customcommands: settings.customcommands[command] = new_content await SettingsDB.get_instance().set_guild_settings(settings) await ctx.send(get_str(ctx, "cmd-customcommand-success-edit")) else: await ctx.send( get_str(ctx, "cmd-customcommand-failed-edit").format( "`{}customcom add`".format( get_server_prefixes(ctx.bot, ctx.guild))))
async def _delete(self, ctx, command): """ {command_prefix}cc delete [command] {help} """ command = command.lower() settings = await SettingsDB.get_instance().get_guild_settings( ctx.guild.id) if not settings.customcommands: await ctx.send( get_str(ctx, "cmd-customcommand-raw-no-cc").format( "`{}customcom add`".format( get_server_prefixes(ctx.bot, ctx.guild)))) elif command in settings.customcommands: settings.customcommands.pop(command, None) await SettingsDB.get_instance().set_guild_settings(settings) await ctx.send(get_str(ctx, "cmd-customcommand-deleted")) else: await ctx.send( get_str(ctx, "cmd-customcommand-failed-edit").format( "`{}customcom add`".format( get_server_prefixes(ctx.bot, ctx.guild))))
async def poll(self, ctx, *, question): """ {command_prefix}poll [question] {help} """ messages = [ctx.message ] # a list of messages to delete when we're all done question = format_mentions(question) answers = [] for i in range(1, 11): messages.append(await ctx.send( get_str(ctx, "cmd-poll-init").format("`{}cancel`".format( get_server_prefixes(ctx.bot, ctx.guild))))) try: entry = await self.bot.wait_for( 'message', check=lambda m: len(m.content) <= 100 and m.author == ctx. author and m.channel == ctx.channel, timeout=60.0) except asyncio.TimeoutError: break if not entry: break prefixes = [ get_server_prefixes(ctx.bot, ctx.guild), f"<@!{self.bot.user.id}>", self.bot.user.mention ] if any([entry.content.startswith(p) for p in prefixes]): break messages.append(entry) answers.append((self.to_keycap(i), entry.clean_content)) try: await ctx.channel.delete_messages(messages) except discord.Forbidden: pass # oh well answer = '\n'.join(map(lambda t: '%s - %s' % t, answers)) if answer == "": return await ctx.send(get_str(ctx, "cmd-poll-cancelled"), delete_after=10) actual_poll = await ctx.send('**%s** {}:\n\n*- %s*\n\n%s'.format( get_str(ctx, "cmd-poll-someone-asks")) % (ctx.author, question, answer)) for emoji, _ in answers: await actual_poll.add_reaction(emoji)
async def _add(self, ctx, command, *, content): """ {command_prefix}cc add [command] [content] {command_prefix}cc add [command] >>>[my_command] {help} """ command = command.lower() cmd = self.bot.get_command(command) if cmd: return await ctx.send( get_str(ctx, "cmd-customcommand-in-basic-command")) settings = await SettingsDB.get_instance().get_guild_settings( ctx.guild.id) if command not in settings.customcommands: content = format_mentions(content) settings.customcommands[command] = content await SettingsDB.get_instance().set_guild_settings(settings) await ctx.send(get_str(ctx, "cmd-customcommand-added")) else: await ctx.send( get_str(ctx, "cmd-customcommand-already-exists").format( "`{}customcom edit`".format( get_server_prefixes(ctx.bot, ctx.guild))))
async def send_cmd_help(self, ctx, command=None): if not command: command = ctx.command cname = str(command).replace(" ", "-").lower() cname = str(cname).replace('pl-', 'pl') if command.help: prefix = get_server_prefixes(ctx.bot, ctx.guild) help_msg = get_str(ctx, f"cmd-{cname}-help") if ctx.channel.permissions_for(ctx.me).embed_links: embed = discord.Embed(title=f'{prefix}{str(command)}') help_msg = '\n'.join( command.help.split('\n\n')[1:]).format(help=help_msg) embed.description = help_msg cmds = '\n'.join([ f'`{cmd.strip()}`' for cmd in command.help.split('\n\n')[0].format( command_prefix=prefix).split('\n') ]) embed.add_field(name='Usage', value=cmds, inline=False) if not ctx.guild: embed.color = 0x71368a else: embed.color = ctx.me.color if command.aliases: aliases = '\n'.join([ f'`{prefix}{(str(command.parent) + " ") if command.parent else ""}{a}`' for a in command.aliases ]) embed.add_field(name="Aliases", value=aliases, inline=False) return await ctx.send(embed=embed) else: return await ctx.send("```%s```" % dedent( ctx.command.help.format(command_prefix=get_server_prefixes( ctx.bot, ctx.guild), help=help_msg))) else: return await ctx.send(get_str(ctx, "cmd-help-help-not-found")) log.warning(f"MissingHelpError : {cname}")
async def _list(self, ctx): """ {command_prefix}cc list {help} """ msg = [get_str(ctx, "cmd-customcommand-list")] settings = await SettingsDB.get_instance().get_guild_settings( ctx.guild.id) if not settings.customcommands: return await ctx.send( get_str(ctx, "cmd-customcommand-raw-no-cc").format( "`{}customcom add`".format( get_server_prefixes(ctx.bot, ctx.guild)))) for n, command in enumerate(settings.customcommands, start=1): msg.append("``{}`` {}\n".format(n, command)) if len(msg) == 1: return await ctx.send( get_str(ctx, "cmd-customcommand-raw-no-cc").format( "`{}customcom add`".format( get_server_prefixes(ctx.bot, ctx.guild)))) to_send = "" for line in msg: if len(to_send) + len( line) > 1980: # TODO find a better way to do this await ctx.send(to_send) # This is ugly to_send = "" to_send += line if to_send: await ctx.send(to_send)
async def customcommand(self, ctx): """ {command_prefix}cc add [command] [content] {command_prefix}cc edit [command] [new_content] {command_prefix}cc delete [command] {command_prefix}cc list {command_prefix}cc raw [command] {help} """ if not ctx.invoked_subcommand: await ctx.send( get_str(ctx, "command-invalid-usage").format("{}help cc".format( get_server_prefixes(ctx.bot, ctx.guild))))
async def _raw(self, ctx, command): """ {command_prefix}cc raw [command] {help} """ command = command.lower() settings = await SettingsDB.get_instance().get_guild_settings( ctx.guild.id) if not settings.customcommands: await ctx.send( get_str(ctx, "cmd-customcommand-raw-no-cc").format( "`{}customcom add`".format( get_server_prefixes(ctx.bot, ctx.guild)))) elif command in settings.customcommands: await ctx.send( get_str(ctx, "cmd-customcommand-raw-content").format( f"`{command}`") + "\n{}".format(settings.customcommands[command])) else: await ctx.send(get_str(ctx, "cmd-customcommand-raw-dont-exist"))
async def meme(self, ctx, pic, *, msg): """ {command_prefix}meme [User] [top_text]|[bottom_text] {command_prefix}meme [custom_url] [top_text]|[bottom_text] {help} """ user = None # msg = unidecode.unidecode(msg) if len(ctx.message.mentions) > (2 if self.bot.user.mention in ctx.prefix else 1): return await ctx.send(get_str(ctx, "cmd-meme-one-user")) if ctx.message.mentions: # mention on a nicknamed user on mobile creates weird issue (no '!' where it should) user = ctx.message.mentions[-1] if str(user.id) not in pic: # It was a prefix user = None if len(ctx.message.mentions) > 1: user = ctx.message.mentions[0] if ctx.guild: if pic.isdigit(): target = ctx.guild.get_member(int(pic)) if target: user = target check_str = [ u for u in ctx.guild.members if u.name.lower() == pic.lower() ] if check_str: user = check_str[0] if user: pic = str(user.avatar_url) pic = pic.strip('<>') msg = " ".join(msg.split()) # remove useless spaces msg = msg.replace('\r', '').replace('\n', '').replace( "-", "--").replace("_", "__").replace("?", "~q").replace( "#", "~h").replace(" ", "_").replace("%", "~p").replace( "/", "~s").replace('"', "''") try: part1 = msg.split("|")[0] except IndexError: part1 = "_" try: part2 = msg.split("|")[1] except IndexError: part2 = "_" if part1 == "": part1 = "_" if part2 == "": part2 = "_" if not get_image_from_url(pic): return await ctx.send( get_str(ctx, "command-invalid-usage").format("{}help meme".format( get_server_prefixes(ctx.bot, ctx.guild)))) if part2 != "_": total = "https://memegen.link/custom/{}/{}.jpg?alt={}".format( part1, part2, pic) else: total = "https://memegen.link/custom/{}/_.jpg?alt={}".format( part1, pic) embed = discord.Embed() embed.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url) # download file async with aiohttp.request("GET", total) as resp: img = BytesIO(await resp.read()) f = discord.File(img, filename="image.png") embed.set_image(url="attachment://image.png") if not user: try: await ctx.message.delete() except discord.HTTPException: pass try: await ctx.send(file=f, embed=embed) except discord.Forbidden: await ctx.send(get_str(ctx, "need-embed-permission"))
async def on_command_error(self, ctx, error): if not isinstance(ctx.channel, discord.abc.PrivateChannel): if self.debug_mode == ctx.message.guild.id: try: await ctx.channel.send("```py\n" + str(error) + "```") except discord.HTTPException: log.info("Can't send debug messages - Missing Permissions") return if isinstance(error, commands.MissingRequiredArgument) or isinstance( error, commands.BadArgument): try: log.debug(f"{error}.. Sending help...") return await self.send_cmd_help(ctx) except discord.HTTPException: log.debug("Can't send help - Missing Permissions") return elif isinstance(error, commands.errors.CommandOnCooldown): try: scds = str(error).replace('You are on cooldown. Try again in', '') return await ctx.channel.send("```c\n{}{}```".format( get_str(ctx, "bot-cooldown"), scds), delete_after=10) except discord.HTTPException: log.debug("Can't send cooldown message - Missing Permissions") return elif isinstance(error, commands.errors.NoPrivateMessage): return await ctx.channel.send("```c\n{}```".format( get_str(ctx, "bot-not-mp")), delete_after=10) if isinstance(error, commands.errors.NotOwner): return elif isinstance(error, commands.errors.CheckFailure): try: return await ctx.channel.send( "```c\n{} ({}permsinfo)```".format( get_str(ctx, 'bot-not-enough-permissions'), get_server_prefixes(ctx.bot, ctx.guild)), delete_after=10) except discord.HTTPException: log.debug( "Can't send permissions failure message - Missing Permissions" ) return elif isinstance(error, commands.CommandNotFound): return elif NoVoiceChannel and isinstance(error, NoVoiceChannel): return else: if isinstance(error, commands.CommandInvokeError): if isinstance(error.original, discord.errors.Forbidden): log.debug( "discord.errors.Forbidden: FORBIDDEN (status code: 403): Missing Permissions" ) return if isinstance(error.original, discord.errors.NotFound): # log.info("discord.errors.NotFound: NotFound (status code: 404): Message not found") return if isinstance(error.original, aiohttp.ClientError): log.debug("Command raised an exception: ClientError") return if isinstance(error.original, asyncio.futures.TimeoutError): log.debug("Command raised an exception: TimeoutError") return print('Ignoring exception in command {}:'.format(ctx.command), file=sys.stderr) traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr) log.error(f'Exception in command {ctx.command.name}: {error}')