简体   繁体   English

如何让机器人通过 discord.py 中的命令离开公会?

[英]How to make the bot leave a guild with a command in discord.py?

I want the bot leave a guild with a command .我希望机器人离开一个带有命令的公会。 I tried this:我试过这个:

 @commands.command()
    @commands.check(user_is_me)
    async def leave(self,guild_id):
        await self.bot.get_guild(int(guild_id)).leave()

But this throws me the following error,但这会给我带来以下错误,

Ignoring exception in command leave:
Traceback (most recent call last):
  File "C:\Users\Rohit\AppData\Roaming\Python\Python37\site-packages\discord\ext\commands\core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:\Users\Rohit\Desktop\discord bots\quotient\cogs\owner.py", line 47, in leave
    await self.bot.get_guild(int(guild_id)).leave()
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Context'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Rohit\AppData\Roaming\Python\Python37\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Rohit\AppData\Roaming\Python\Python37\site-packages\discord\ext\commands\core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Rohit\AppData\Roaming\Python\Python37\site-packages\discord\ext\commands\core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: int() argument must be a string, a bytes-like object or a number, not 'Context'

How do I solve it?我该如何解决?

if you check documentation for command you see example like如果您检查命令的文档,您会看到类似的示例

@commands.command()
def test(ctx, arg)

When you use it in class then you have self as first argument but you still need other arguments当您在课堂上使用它时,您将self作为第一个参数,但您仍然需要其他参数

@commands.command()
def test(self, ctx, arg)

In you code it will be在你的代码中,它将是

def leave(self, ctx, guild_id)

But you use leave(self, guild_id) so you get context in variable guild_id and later you try to use this context in int() and in error message you get information about Context但是你使用leave(self, guild_id)所以你在变量guild_id获得context ,然后你尝试在int()和错误消息中使用这个context你得到关于Context信息

int() argument must be a string, a bytes-like object or a number, not 'Context'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM