Search code examples
pythondiscorddiscord.py

Command to make my bot leave a specific guild: discord.py rewrite


I want to make a command to make the bot leave a specific guild. Usage would be -leave [guild]

I don't really know what to try out, but I've messed with some of the arguments a bit; did nothing

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

I'm getting the following error:

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

I'd like the bot to leave the guild and send a confirmation message shown in the code


Solution

  • There is no built in converter for guilds, so you'll have to do it yourself:

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