Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何让机器人在discord.py中使用命令离开公会?_Python_Discord_Discord.py - Fatal编程技术网

Python 如何让机器人在discord.py中使用命令离开公会?

Python 如何让机器人在discord.py中使用命令离开公会?,python,discord,discord.py,Python,Discord,Discord.py,我要机器人带着命令离开公会。我试过这个: @commands.command() @commands.check(user_is_me) async def leave(self,guild_id): await self.bot.get_guild(int(guild_id)).leave() 但这让我犯了以下错误 Ignoring exception in command leave: Traceback (most recent call last):

我要机器人带着命令离开公会。我试过这个:

 @commands.command()
    @commands.check(user_is_me)
    async def leave(self,guild_id):
        await self.bot.get_guild(int(guild_id)).leave()
但这让我犯了以下错误

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'

如何解决这个问题?

如果您查看文档,您会看到如下示例

@commands.command()
def test(ctx, arg)
当你在课堂上使用它时,你有
self
作为第一个参数,但你仍然需要其他参数

@commands.command()
def test(self, ctx, arg)
在您的代码中,它将是

def leave(self, ctx, guild_id)
但是您使用
leave(self,guild\u id)
这样您就可以在变量
guild\u id
中获得
context
,然后您尝试在
int()
中使用此
context
并在错误消息中获得有关
context
的信息

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

错误显示您的
int()
有问题,因此请检查在int()`-
print(guild\u id)
,print(type(guild\u id))
中尝试转换的内容。它不是字符串,而是某个类
Context`。也许你使用了误导性的名字,你应该使用
def leave(self,context)
,然后你可以尝试从
context
获取
guild\u id
,我检查文档,通常你应该有
def leave(context,guild\u id)
,但你似乎在课堂上使用它,你有
self
(类实例)作为第一个参数,但您仍然需要
context
(或
ctx
)作为
self
之后的第一个参数-因此您应该选择
def leave(self、context、guild\u id)