首页 > 解决方案 > 让我的机器人离开特定公会的命令:discord.py rewrite

问题描述

我想发出命令让机器人离开特定的公会。用法是 -leave [guild]

我真的不知道该尝试什么,但我对一些论点有点混乱;什么也没做

@commands.command(hidden=True)
@commands.is_owner()
async def leave(self, ctx, guild: discord.Guild):
    await self.bot.leave_guild(guild)
    await ctx.send(f":ok_hand: Left guild: {guild.name} ({guild.id})")

我收到以下错误:

AttributeError: module 'discord.ext.commands.converter' has no attribute 'GuildConverter'

我希望机器人离开公会并发送代码中显示的确认消息

标签: pythondiscorddiscord.pydiscord.py-rewrite

解决方案


公会没有内置转换器,所以你必须自己做:

@commands.command()
@commands.is_owner()
async def leave(self, ctx, *, guild_name):
    guild = discord.utils.get(self.bot.guilds, name=guild_name)
    if guild is None:
        await ctx.send("I don't recognize that guild.")
        return
    await self.bot.leave_guild(guild)
    await ctx.send(f":ok_hand: Left guild: {guild.name} ({guild.id})")

推荐阅读