Khaotic Codes
Khaotic Codes

Reputation: 17

Discord.py Leaving a server

I dont have access to some of these test servers my bot is in and I want the bot to leave them, my code is

@bot.command()
@commands.is_owner()
async def guilds(ctx):
  await ctx.channel.purge(limit=1)
  guild = ""
  servers = bot.guilds
  for a in servers:
    guild += f"{a}\n"
    if a == "Fleshy's server":
      b = a.id()
      await bot.leave(b)
  await ctx.author.send(guild)

I dont get any error when i use this but it also doenst leave the Fleshy's servers These are the servers he is in my server The Judas Cradle 7 test server yosh’s rock Fleshy's server Retronized Cafe Fleshy's server Fleshy's server Able's Hub

Upvotes: 0

Views: 7272

Answers (1)

Dominik
Dominik

Reputation: 3592

If you want to leave a guild by knowing its name you can run the following command:

@bot.command()
async def leaveg(ctx, *, guild_name):
    guild = discord.utils.get(bot.guilds, name=guild_name) # Get the guild by name
    if guild is None:
        print("No guild with that name found.") # No guild found
        return
    await guild.leave() # Guild found
    await ctx.send(f"I left: {guild.name}!")
  • We use * in order to also allow names with a space in it
  • Import from discord.utils import get

If you know the ID of the server you can also run the following command:

@bot.command()
async def leave(ctx, guild_id):
    await bot.get_guild(int(guild_id)).leave()
    await ctx.send(f"I left: {guild_id}")
  • Import from discord.utils import get
  • Make sure to put in the ID: leave ServerID

You can also have a look at the docs.

Upvotes: 3

Related Questions