【问题标题】:Guild join event prints the bot was invited -- Discord.py公会加入事件打印机器人被邀请 - Discord.py
【发布时间】:2021-05-12 17:39:12
【问题描述】:

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

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

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

【问题讨论】:

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


    【解决方案1】:

    on_guild_join,事件参数不需要bot

    相反,您传递guild 并使用on_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}')
    

    【讨论】:

      【解决方案2】:

      所以我在自己的机器人中使用了这段代码来看看它是如何工作的。首先,我将 on_bot_join 事件更改为 on_guild_join,然后删除了 bot 参数,因为该事件不需要它。

      @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:
          print(guild.name)
      

      要仅打印 bot 加入的服务器的名称,只需使用 guild.name 即可。像这样的:

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

      【讨论】:

        猜你喜欢
        • 2021-09-05
        • 2020-10-18
        • 2019-03-02
        • 2019-08-28
        • 2020-12-02
        • 2020-09-23
        • 2021-01-29
        • 1970-01-01
        • 2021-11-02
        相关资源
        最近更新 更多