L
L
LimeGeeg2021-12-24 12:02:38
Python
LimeGeeg, 2021-12-24 12:02:38

How to leave a discord server by its id in discord.py?

I need to make the command ">server-leave [server id]", by which the discord bot will leave this server whose id I will specify when calling. I've been trying all day to solve this easy problem, but all the codes I've used from the documentation/questions from other users just don't work. Please, help. Here is the code I have and it is not working:

@bot.command(aliases=['server-leave'])
async def __leave_from_server(ctx, serverid = None):

if ctx.author.id in devlist:

    if serverid == None:
        await ctx.send(embed = discord.Embed(title = "Укажите id сервера"))
    else:
        try:
            toleave = bot.get_guild(serverid)
            await bot.leave_guild(toleave)
            await ctx.send(embed = discord.Embed(title = "Успешно"))
        except:
            try:
                toleave = bot.get_guild(serverid)
                await toleave.leave()
                await ctx.send(embed = discord.Embed(title = "Успешно"))
            except:
                await ctx.send(embed = discord.Embed(title = "Ошибка"))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Nevzorov, 2021-12-24
@LimeGeeg

  1. Don't use outdated documentation .
    59mQYWj.png
    The method bot.leave_guildhas not been in the given library for two years.
  2. Use the discord.Guild object converter :
    5Udu5yr.png
  3. Why use team aliases for team name? The command decorator has a name argument .
  4. There are checks in discord.py to check who is executing a command .

DEV_LIST = [...]

def check_dev(ctx):
    """Check if command caller is dev"""
    return ctx.author.id in DEV_LIST

@bot.command(name="server-leave")
@commands.check(check_dev)
async def __leave_from_server(ctx, server: discord.Guild = None):
    """Leave from selected server"""
    if server:
        await server.leave()
    else:
        await ctx.send("You need to specify server")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question