【问题标题】:Send message when bot joins server [Discord.py]当 bot 加入服务器时发送消息 [Discord.py]
【发布时间】:2021-01-09 23:57:25
【问题描述】:

我目前正在制作一个不和谐机器人,并希望在服务器加入时在#general 频道中发送消息,这是我目前拥有的代码。

async def on_guild_join(guild):
    general = find(lambda x: x.name == 'general',  guild.text_channels)
    if general and general.permissions_for(guild.me).send_messages:
        embed=discord.Embed(title="**======== *Thanks For Adding Me!* ========**", description=f"""
        Thanks for adding me to {guild.name}!
        You can use the `--help` command to get started!
        """, color=0xd89522)
        await general.send(embed=embed)

当我运行这段代码时,注意到发生了。我没有得到任何错误或输出。

如果有人可以提供帮助,那就太好了。谢谢!

【问题讨论】:

标签: python discord.py


【解决方案1】:
async def on_guild_join(guild):
    embed=discord.Embed(title="**======== *Thanks For Adding Me!* ========**", description=f"Thanks for adding me to {guild.name}"!You can use the `--help` commandto get started!"", color=0xd89522)
    await guild.text_channels[0].send(embed=embed)

【讨论】:

    【解决方案2】:

    您的on_guild_joinevent decorator (@client.event) 吗?当我用这个装饰器测试你的代码时,它工作正常。

    @client.event
    async def on_guild_join(guild):
        general = find(lambda x: x.name == 'general',  guild.text_channels)
        if general and general.permissions_for(guild.me).send_messages:
            embed=discord.Embed(title="**======== *Thanks For Adding Me!* ========**", description=f"""
            Thanks for adding me to {guild.name}!
            You can use the `--help` command to get started!
            """, color=0xd89522)
            await general.send(embed=embed)
    

    此外,如果您的机器人加入的公会没有名为“general”的频道,您可能希望有一个备用方案。在guild.text_channels[0]发送消息可能更可靠,它将在第一个通道发送消息,或者guild.system_channel在系统通道发送消息。

    【讨论】:

      猜你喜欢
      • 2021-09-10
      • 2018-07-27
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      • 2021-03-08
      • 2020-12-05
      • 2021-02-13
      • 2020-01-10
      相关资源
      最近更新 更多