让我的机器人离开特定公会的命令:discord.py rewrite

问题描述 投票:0回答:2

我想发出命令让机器人离开特定的公会。用法是 -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'

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

python discord discord.py
2个回答
3
投票

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

@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})")

0
投票

5年后我从谷歌来到这里,上面的答案不再有效。对于那些和我处境相同的人来说,以下方法有效:

@bot.command()
async def leave(ctx, guild_id:int):
    guild=discord.utils.get(bot.guilds, id=guild_id)
    if guild==None:
        await ctx.send("Server not found by ID")
        return
    await guild.leave()
    await ctx.send(f"Left {guild.name}!")

如果您想通过服务器名称而不是 ID 离开,只需将 guild_id:int 更改为 guild_name:str 并更改命令中对 guild_id 的所有引用以匹配

© www.soinside.com 2019 - 2024. All rights reserved.