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)