如何在discord.py中获取公会ID?

时间:2021-01-07 11:12:03

标签: python python-3.x discord discord.py

我正在尝试为私人机器人制作一些特定于服务器的命令,但我不知道如何使公会 ID 进入我可以用来比较的状态。机器人在一些私人服务器中,但命令基本上是在笑话中,所以它们只需要在特定的服务器上工作。

我当前的代码是这样的:

    if discord.Guild.id == 765623206925041675:
      await message.channel.send("Funny joke!")
    else:
      await message.channel.send("Disabled in this server.")

命令和响应目前是占位符,只是示例。

这里的代码总是以 else 语句作为响应,即使在匹配的公会中也是如此。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您指的是类本身,而不是实例。获取公会ID

guild_id = message.guild.id

答案 1 :(得分:1)

你这样做:

@client.command()
async def myCommand(ctx):
    if ctx.guild.id != 765623206925041675:
        await ctx.send('Disabled in this server.')
        return

    await ctx.send('Funny joke!')

如果您尝试在 on_message 事件中执行此操作,它将如下所示:

@client.event
async def on_message(message):
    if message.guild.id != 765623206925041675:
        await message.channel.send('Disabled in this server.')
        return

    await message.channel.send('Funny joke!')
    await client.process_commands(message)     # Important for commands to work