【问题标题】:python discord.py send DM to inviter on join guildpython discord.py 在加入公会时向邀请者发送 DM
【发布时间】:2020-09-23 23:30:18
【问题描述】:

我目前有以下on_guild_join 代码:

@client.event
async def on_guild_join(guild):
    embed = discord.Embed(title='Eric Bot', color=0xaa0000)
    embed.add_field(name="What's up everyone? I am **Eric Bot**.", value='\nTry typing `/help` to get started.', inline=False)
    embed.set_footer(text='Thanks for adding Eric Bot to your server!')
    await guild.system_channel.send(embed=embed)
    print(f'{c.bgreen}>>> {c.bdarkred}[GUILD JOINED] {c.black}ID: {guild.id} Name: {guild.name}{c.bgreen} <<<\n{c.darkwhite}Total Guilds: {len(client.guilds)}{c.end}')

(忽略c.color 的东西,这是我在控制台上的格式)

每当有人将机器人添加到公会时,它都会向系统频道发送包含一些信息的嵌入。
我希望它向邀请机器人的人(使用 oauth 授权链接的帐户)发送相同消息的 DM。问题是on_guild_join 事件只需要 1 个参数,guild,它不会为您提供有关使用授权链接将机器人添加到公会的人的任何信息。

有没有办法做到这一点?我是否必须使用“作弊”方法,例如拥有一个记录使用邀请的帐户的自定义网站?

【问题讨论】:

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


    【解决方案1】:

    由于没有“邀请”聊天机器人,因此在添加聊天机器人时会有一个审核日志事件。这使您可以遍历匹配特定条件的日志。

    如果您的机器人有权访问审核日志,您可以搜索bot_add 事件:

    @client.event
    async def on_guild_join(guild):
        bot_entry = await guild.audit_logs(action=discord.AuditLogAction.bot_add).flatten()
        await bot_entry[0].user.send("Hello! Thanks for inviting me!")
    

    如果您希望对照您自己的 ID 仔细检查机器人的 ID:

    @client.event
    async def on_guild_join(guild):
        def check(event):
            return event.target.id == client.user.id
        bot_entry = await guild.audit_logs(action=discord.AuditLogAction.bot_add).find(check)
        await bot_entry.user.send("Hello! Thanks for inviting me!")
    

    参考资料:

    【讨论】:

    • 在获得足够 ping 的情况下,可能会在获得事件之前添加另一个机器人,然后机器人可能会向错误的成员发送消息。是否可以查看邀请了哪个机器人?
    猜你喜欢
    • 2021-07-15
    • 2021-09-24
    • 2020-10-18
    • 2021-09-05
    • 2021-05-12
    • 1970-01-01
    • 2021-04-16
    • 2020-09-24
    相关资源
    最近更新 更多