3
votes

I currently have the following on_guild_join code:

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

(Ignore the c.color stuff, it's my formatting on the console)

It sends an embed with a bit of information to the system channel whenever someone adds the bot to a guild.
I want it to send a DM to whoever invited the bot (the account that used the oauth authorize link) the same message. The problem is that the on_guild_join event only takes 1 argument, guild, which does not give you any information about the person who used the authorize link to add the bot to the guild.

Is there a way to do this? Do I have to use a "cheat" method like having a custom website that logs the account that uses the invite?

1

1 Answers

2
votes

Since bots aren't "invited", there's instead an audit log event for when a bot is added. This lets you iterate through the logs matching specific criteria.

If your bot has access to the audit logs, you can search for a bot_add event:

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

And if you wish to double-check the bot's ID against your own:

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

References: