【问题标题】:Discord.py How to keep track of all guildsDiscord.py 如何跟踪所有公会
【发布时间】:2021-06-20 11:56:51
【问题描述】:

我有一个不和谐机器人,我希望这个不和谐机器人只在我的服务器上,所以我创建了一个新的状态任务,它通过服务器并离开其他服务器。问题是 .guilds 只会在我每次运行脚本时更新。我非常感谢解决此问题的每一个新解决方案或方法。祝你有美好的一天。

async def Join_Protection():
    global PROTECTION
    global Verified_Servers
    Verified_Servers = []
    ProtecFile = open('SaveServers.txt', 'r')
    while True:
        time.sleep(0.5)
        print(client.guilds)
        for i in client.guilds:
            id = i.id
            if str(id) not in ProtecFile.readlines():
                print('Left unverified Server: {} | Id: {}'.format(i, id))
                await i.leave()
            else:
                if id not in Verified_Servers:
                    print('Verified Server: {} | Id: {}'.format(i, id))
                    Verified_Servers.append(i)

【问题讨论】:

  • 如果您不希望自己的机器人出现在其他服务器上,为什么不在开发者门户中禁用“公共机器人”?引用:“未选中时,只有您可以将此机器人加入服务器。”
  • 我希望其他人能够邀请机器人

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


【解决方案1】:

您可以使用client.on_guild_join 事件。每次将机器人添加到新公会时都会触发它。

@client.event
async def on_guild_join(guild):
    ProtecFile = open('SaveServers.txt', 'r')
    if str(guild.id) not in ProtecFile.readlines():
        await guild.leave()

    ProtecFile.close()

【讨论】:

  • 谢谢,这是个好主意。我会调查的!
猜你喜欢
  • 1970-01-01
  • 2021-03-14
  • 2020-03-20
  • 1970-01-01
  • 2021-04-30
  • 2021-02-28
  • 1970-01-01
  • 2013-03-02
  • 1970-01-01
相关资源
最近更新 更多