【问题标题】:Command to make my bot leave a specific guild: discord.py rewrite让我的机器人离开特定公会的命令:discord.py rewrite
【发布时间】:2019-10-17 14:13:00
【问题描述】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!我能够根据这个答案找到不同的解决方案:@commands.command() @commands.is_owner() async def leave(self, ctx, *, guildinput): try: guildid = int(guildinput) except: await ctx.send("Invalid guild: failed to convert to int") try: guild = self.bot.get_guild(guildid) except: await ctx.send("Invalid guild") try: await guild.leave() await ctx.send(f"left {guild.name}") except: await ctx.send("Error leaving")
    猜你喜欢
    • 2018-07-28
    • 2020-09-11
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 2021-09-05
    • 2021-01-29
    • 1970-01-01
    • 2021-12-01
    相关资源
    最近更新 更多