python discord.py 在加入公会时向邀请者发送 DM

我目前有以下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它不会为您提供有关使用授权链接将机器人添加到公会的人的任何信息。


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


墨色风雨
浏览 96回答 2
2回答

千巷猫影

由于没有“邀请”机器人,因此当添加机器人时会有一个审核日志事件。这使您可以遍历匹配特定条件的日志。如果您的机器人可以访问审核日志,您可以搜索bot_add事件:@client.eventasync def on_guild_join(guild):&nbsp; &nbsp; bot_entry = await guild.audit_logs(action=discord.AuditLogAction.bot_add).flatten()&nbsp; &nbsp; await bot_entry[0].user.send("Hello! Thanks for inviting me!")如果您希望根据您自己的 ID 仔细检查机器人的 ID:@client.eventasync def on_guild_join(guild):&nbsp; &nbsp; def check(event):&nbsp; &nbsp; &nbsp; &nbsp; return event.target.id == client.user.id&nbsp; &nbsp; bot_entry = await guild.audit_logs(action=discord.AuditLogAction.bot_add).find(check)&nbsp; &nbsp; await bot_entry.user.send("Hello! Thanks for inviting me!")

慕村9548890

从这篇文章使用discord.py 2.0,您可以获得BotIntegration服务器的信息以及邀请机器人的用户信息。例子from discord.ext import commandsbot = commands.Bot()@bot.eventasync def on_guild_join(guild):&nbsp; &nbsp; # get all server integrations&nbsp; &nbsp; integrations = await guild.integrations()&nbsp; &nbsp; for integration in integrations:&nbsp; &nbsp; &nbsp; &nbsp; if isinstance(integration, discord.BotIntegration):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if integration.application.user.name == bot.user.name:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bot_inviter = integration.user# returns a discord.User object&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # send message to the inviter to say thank you&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await bot_inviter.send("Thank you for inviting my bot!!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break注意:&nbsp;guild.integrations()需要Manage Server(&nbsp;manage_guild) 权限。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python