async def test_try_get_tag_command_error(self): """Should call `on_command_error` when `CommandError` raised.""" err = errors.CommandError() self.tag.get_command.can_run = AsyncMock(side_effect=err) self.cog.on_command_error = AsyncMock() self.assertIsNone(await self.cog.try_get_tag(self.ctx)) self.cog.on_command_error.assert_awaited_once_with(self.ctx, err)
async def setbattletag(self, ctx, tag: battle_tag): """Sets the Blizzard BattleTag associated with this Discord account. Tags are of the format <platform>/<region>/username#number, or username#number. For example: "pc/us/noob#0001". If platform and region are not specified, I will search. If the incorrect one is chosen, please specify the platform and region. """ async with aiohttp.ClientSession() as client: if tag.is_complete: async with client.head(tag.profile_url) as resp: if resp.status == 404: raise errors.BadArgument( "That battletag does not match a user") elif resp.status != 200: raise errors.CommandError( "There was an unknown error setting your battletag" ) else: await self.attr.set_attributes( ctx.message.author, blizzard_battletag=tag.tag) await ctx.send( "Updated your battletag to {}".format(tag)) else: tag = await find_tag(client, tag) await self.attr.set_attributes(ctx.message.author, blizzard_battletag=tag.tag) await ctx.send("Updated your battletag to {}".format(tag))
async def test_error_handler_already_handled(self): """Should not do anything when error is already handled by local error handler.""" self.ctx.reset_mock() cog = ErrorHandler(self.bot) error = errors.CommandError() error.handled = "foo" self.assertIsNone(await cog.on_command_error(self.ctx, error)) self.ctx.send.assert_not_awaited()
async def update_and_restart(self, ctx): if not self.footgun(): raise errors.CommandError("You probably don't want to run this...") async with utils.loading_emoji(ctx): await utils.run_subprocess("git fetch origin master && git reset --hard FETCH_HEAD") await utils.run_subprocess("python -m pip install --upgrade -r requirements.txt") log.info("Restarting due to update_and_restart") os.execve(sys.executable, ['python', '-m', 'dango'], os.environ)
async def sh(self, ctx, *, cmd): if not self.footgun(): raise errors.CommandError("You probably don't want to run this...") with ctx.typing(): stdout, stderr = await utils.run_subprocess(cmd) if stderr: out = "stdout:\n{}\nstderr:\n{}".format(stdout, stderr) else: out = stdout await ctx.send("```{}```".format(out))
async def find_tag(client, tag): for platform in Platform: tag.platform = platform for region in get_regions(platform): tag.region = region async with client.head(tag.profile_url, allow_redirects=True) as resp: if resp.status == 404: continue elif resp.status != 200: log.error("Unknown error looking up battletag %s", resp) raise errors.CommandError( "There was an unknown error looking up your battletag") else: return tag raise errors.BadArgument("Could not find that user in any region!")
async def send_message(ctx, message: str = None, embed: Embed = None, sep: str = ", "): if message is None and embed is None: raise errors.CommandError("A message or a embed must be provided") if isinstance(ctx, TextChannel): channel = ctx elif isinstance(ctx.message.channel, (DMChannel, TextChannel)): channel = ctx.message.channel if message is not None: message = f"{ctx.message.author.mention}{sep}{message}" else: message = ctx.message.author.mention await channel.send(message, embed=embed)
async def test_try_silence_no_permissions_to_run_command_error(self): """Should return `False` because `CommandError` raised (no permissions).""" self.ctx.invoked_with = "foo" self.bot.get_command.return_value.can_run = AsyncMock( side_effect=errors.CommandError()) self.assertFalse(await self.cog.try_silence(self.ctx))