Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test_context_copy():
with utils.mock_ctx() as ctx:
await copy_context_with(ctx, author=1, channel=2, content=3)
ctx.bot.get_context.assert_called_once()
alt_message = ctx.bot.get_context.call_args[0][0]
alt_message._update.assert_called_once()
assert alt_message._update.call_args[0] == ({"content": 3},)
async def jsk_repeat(self, ctx: commands.Context, times: int, *, command_string: str):
"""
Runs a command multiple times in a row.
This acts like the command was invoked several times manually, so it obeys cooldowns.
You can use this in conjunction with `jsk sudo` to bypass this.
"""
with self.submit(ctx): # allow repeats to be cancelled
for _ in range(times):
alt_ctx = await copy_context_with(ctx, content=ctx.prefix + command_string)
if alt_ctx.command is None:
return await ctx.send(f'Command "{alt_ctx.invoked_with}" is not found')
await alt_ctx.command.reinvoke(alt_ctx)
async def jsk_in(self, ctx: commands.Context, channel: discord.TextChannel, *, command_string: str):
"""
Run a command as if it were run in a different channel.
"""
alt_ctx = await copy_context_with(ctx, channel=channel, content=ctx.prefix + command_string)
if alt_ctx.command is None:
return await ctx.send(f'Command "{alt_ctx.invoked_with}" is not found')
return await alt_ctx.command.invoke(alt_ctx)
async def jsk_su(self, ctx: commands.Context, target: discord.User, *, command_string: str):
"""
Run a command as someone else.
This will try to resolve to a Member, but will use a User if it can't find one.
"""
if ctx.guild:
# Try to upgrade to a Member instance
# This used to be done by a Union converter, but doing it like this makes
# the command more compatible with chaining, e.g. `jsk in .. jsk su ..`
target = ctx.guild.get_member(target.id) or target
alt_ctx = await copy_context_with(ctx, author=target, content=ctx.prefix + command_string)
if alt_ctx.command is None:
if alt_ctx.invoked_with is None:
return await ctx.send('This bot has been hard-configured to ignore this user.')
return await ctx.send(f'Command "{alt_ctx.invoked_with}" is not found')
return await alt_ctx.command.invoke(alt_ctx)