首页 > 解决方案 > 公会加入事件打印机器人被邀请 - Discord.py

问题描述

我不确定我将如何编写一个命令/事件,该命令/事件会在将机器人添加到新服务器时打印出机器人所在的行会列表。我知道如何自己打印服务器列表,但我不知道允许我检查机器人何时添加到新服务器的条件。我一直在网上搜索,但没有运气。

@bot.event
async def on_bot_join(bot):
    if # condition that sends true when the bot has been added to a new server
        print('Bot has been added to a new server')
        print('List of servers the bot is in: ')
        for guild in bot.guilds:
            await print(guild.name)

如果有人能告诉我如何只打印机器人添加到的新服务器的名称,而不是全部打印,那将会更有帮助。

标签: pythonpython-3.xdiscorddiscord.pybots

解决方案


是的on_guild_join,它不需要bot事件参数。

相反,您传递guildon_guild_join用作您的事件。这里适用于你的问题,

@bot.event
async def on_guild_join(guild):
    print('Bot has been added to a new server')
    print('List of servers the bot is in: ')

    for guild in bot.guilds:
        await print(guild.name)

if添加时也不需要使用

如果你只想打印公会,你可以使用如下并简单地使用guild

@bot.event
async def on_guild_join(guild):
    print(f'Bot has been added to: {guild}')

推荐阅读