B
B
Biohazard-python2021-02-22 10:44:12
Python
Biohazard-python, 2021-02-22 10:44:12

I want my discord bot to leave the servers that are on the list. It gives an error, how to fix it?

I tried to make such code. But it gives an error, it does not depend on the list, I have already checked. Here is the code:

@bot.event
async def on_connect(guild):
  black = [#потом поставлю айди серверов]
  if guild.id in black:
     await bot.leave_guild()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Nevzorov, 2021-02-22
@Biohazard-python

The function bot.leave_guilddoes not exist in the current version of the library. Much of the functionality in the rewrite version of the library has been moved to the appropriate models .
The event on_connectis fired when connected to Discord , not when the bot is added to the server.
When a bot is added to the server, an event is called on_guild_join: https://discordpy.readthedocs.io/en/stable/api.htm...
Thus:

GUILD_BLACKLIST = [779450659245255793, 853811295717358421, ...]  # Список ID

@bot.event
async def on_guild_join(guild):
    if guild.id in GUILD_BLACKLIST:
        print(f"Guild {guild.name} ({guild.id}) is blacklisted! Leaving…"
        await guild.leave()

M
Maxim Siomin, 2021-02-22
@MaxSiominDev

@bot.event
async def on_connect(guild):
  black = ["""потом поставлю айди серверов"""]
  if guild.id in black:
     await bot.leave_guild()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question