async def convert(self, ctx, argument): match = MESSAGE_ID_RE.match(argument) or MESSAGE_LINK_RE.match( argument) if not match: raise errors.BadArgument( "{} doesn't look like a message to me...".format(argument)) msg_id = int(match.group("message_id")) channel_id = int(match.group("channel_id") or ctx.channel.id) channel = ctx.guild.get_channel(channel_id) if not channel: channel = ctx.bot.get_channel(channel_id) if not channel: raise errors.BadArgument("Channel {} not found".format(channel_id)) author = channel.guild.get_member(ctx.author.id) if not channel.guild.me.permissions_in(channel).read_messages: raise errors.CheckFailure( "I don't have permission to view channel {0.mention}".format( channel)) if not author or not channel.permissions_for(author).read_messages: raise errors.CheckFailure( "You don't have permission to view channel {0.mention}".format( channel)) return (channel, msg_id)
async def convert(self, ctx, argument): match = MESSAGE_ID_RE.match(argument) or MESSAGE_LINK_RE.match( argument) if not match: cleaned = await clean_content().convert( ctx, f"{argument} doesn't look like a message to me…") raise errors.BadArgument(cleaned) msg_id = int(match['message_id']) channel_id = int(match['channel_id'] or ctx.channel.id) channel = ctx.guild.get_channel(channel_id) if not channel: channel = ctx.bot.get_channel(channel_id) if not channel: raise errors.BadArgument(f'Channel {channel_id} not found.') author = channel.guild.get_member(ctx.author.id) if not channel.guild.me.permissions_in(channel).read_messages: raise errors.CheckFailure( f"I don't have permission to view channel {channel.mention}.") if not author or not channel.permissions_for(author).read_messages: raise errors.CheckFailure( f"You don't have permission to view channel {channel.mention}." ) return (channel, msg_id)
async def invoke(self, ctx): """|coro| *Just added to run checks, so I get the code more dry* Invokes the command given under the invocation context and handles all the internal event dispatch mechanisms. Parameters ----------- ctx: :class:`.Context` The invocation context to invoke. """ if ctx.command is not None: self.dispatch('command', ctx) try: if await self.can_run(ctx, call_once=True): await ctx.command.invoke(ctx) else: raise errors.CheckFailure( 'The global check once functions failed.') except errors.CommandError as exc: await ctx.command.dispatch_error(ctx, exc) else: self.dispatch('command_completion', ctx) elif ctx.invoked_with: # TODO: Add the ability for custom commands here! exc = errors.CommandNotFound('Command "{}" is not found'.format( ctx.invoked_with)) self.dispatch('command_error', ctx, exc)
async def test_error_handler_check_failure(self): """Should await `ErrorHandler.handle_check_failure` when error is `CheckFailure`.""" self.ctx.reset_mock() cog = ErrorHandler(self.bot) cog.handle_check_failure = AsyncMock() error = errors.CheckFailure() self.assertIsNone(await cog.on_command_error(self.ctx, error)) cog.handle_check_failure.assert_awaited_once_with(self.ctx, error)
async def bc(self, ctx, *, args): args = args.strip(' `') for banned_word in ['open', 'read', 'write', 'getline']: if banned_word in args: raise errors.CheckFailure(f'Banned word found') try: await ctx.bot.say(str(cexprtk.evaluate_expression(args, {}))) except: raise errors.BadArgument(f'Invalid expression')
async def convert(self, ctx, argument): channel, msg_id = await MessageIdConverter().convert(ctx, argument) msg = discord.utils.get(ctx.bot.cached_messages, id=msg_id) if msg is None: try: msg = await channel.fetch_message(msg_id) except discord.NotFound: raise errors.BadArgument( "Message {0} not found in channel {1.mention}".format( msg_id, channel)) except discord.Forbidden: raise errors.CheckFailure( "I don't have permission to view channel {0.mention}". format(channel)) elif msg.channel.id != channel.id: raise errors.BadArgument("Message not found") return msg
async def convert(self, ctx, argument): channel, msg_id = await MessageId().convert(ctx, argument) msg = discord.utils.get(ctx.bot.cached_messages, id=msg_id) if msg is None: try: msg = await channel.fetch_message(msg_id) except discord.NotFound: raise errors.BadArgument( f'Message {msg_id} not found in channel {channel.mention}.' ) except discord.Forbidden: raise errors.CheckFailure( f"I don't have permission to view channel {channel.mention}." ) elif msg.channel.id != channel.id: raise errors.BadArgument('Message not found.') return msg
async def on_message(message): # override discord.py's process_commands coroutine in the commands.Bot class # this allows SpeedGamingBot to issue commands to SahasrahBot if message.author.bot and not message.author.id == 344251539931660288: return ctx = await discordbot.get_context(message) if ctx.command is not None: discordbot.dispatch('command', ctx) try: if await discordbot.can_run(ctx, call_once=True): await ctx.command.invoke(ctx) else: raise errors.CheckFailure('The global check once functions failed.') except errors.CommandError as exc: await ctx.command.dispatch_error(ctx, exc) else: discordbot.dispatch('command_completion', ctx)
async def invoke(self, ctx): handled_by_custom_handler = await self.execute_command_handlers(ctx) if ctx.command is not None: self.dispatch("command", ctx) try: if await self.can_run(ctx, call_once=True): await ctx.command.invoke(ctx) else: raise errors.CheckFailure( "The global check once functions failed." ) except errors.CommandError as exc: await ctx.command.dispatch_error(ctx, exc) else: self.dispatch("command_completion", ctx) elif ctx.invoked_with and handled_by_custom_handler is False: error = errors.CommandNotFound( 'Command "{}" is not found'.format(ctx.invoked_with) ) self.dispatch("command_error", ctx, error)
async def on_dispatch(self, data): if data["t"] != "INTERACTION_CREATE": return data = data["d"] guild: Guild = self.bot.get_guild(int(data["guild_id"])) channel = guild.get_channel(int(data["channel_id"])) # Hacking together fields lol data["attachments"] = [] data["embeds"] = [] data["edited_timestamp"] = None data["pinned"] = False data["mention_everyone"] = False data["tts"] = False data["content"] = None data["author"] = data["member"]["user"] message = Message(state=self.bot._connection, channel=channel, data=data) command_name = data["data"]["name"] context = SlashContext( prefix="/", message=message, bot=self.bot, invoked_with=command_name, command=self.bot.get_command(command_name), args=[] ) self.bot.dispatch('command', context) try: if not await self.bot.can_run(context): raise errors.CheckFailure('The global check once functions failed.') except errors.CommandError as exc: await context.command.dispatch_error(context, exc) else: self.bot.dispatch('command_completion', context) params = await self.parse_args(context, context.command, data["data"].get("options", [])) context.kwargs = params await context.command(context, **params)
def _(ctx): me = ctx.guild.me if ctx.message.guild else ctx.bot.user for perm in perms: if not getattr(ctx.channel.permissions_for(me), perm): raise errors.CheckFailure("I need the %s permission" % perm) return True
def _(ctx): if ctx.author.id != 86607397321207808: raise errors.CheckFailure("I don't think you can do that...") return True
def _(ctx): author = ctx.message.author for perm in perms: if not getattr(ctx.channel.permissions_for(author), perm): raise errors.CheckFailure("You need the %s permission" % perm) return True
def _(ctx): if ctx.author.id != 474104220770107392: raise errors.CheckFailure("I don't think you can do that...") return True