Discord.py 离开服务器

时间:2021-03-03 14:38:00

标签: python discord.py

我无法访问我的机器人所在的一些测试服务器,我希望机器人离开它们,我的代码是

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

当我使用它时我没有收到任何错误,但它也不会离开 Fleshy 的服务器 这些是他所在的服务器 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

1 个答案:

答案 0 :(得分:1)

如果你想知道一个公会的名字就离开它,你可以运行以下命令:

@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}!")
  • 我们使用 * 也是为了允许名称中包含空格
  • 导入from discord.utils import get

如果您知道服务器的 ID,您还可以运行以下命令:

@bot.command()
async def leave(ctx, guild_id):
    await bot.get_guild(int(guild_id)).leave()
    await ctx.send(f"I left: {guild_id}")
  • 导入from discord.utils import get
  • 确保输入 ID:leave ServerID

您还可以查看docs