Eric Jin
Eric Jin

Reputation: 3934

python discord.py send DM to inviter on join guild

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?

Upvotes: 2

Views: 1883

Answers (2)

codeofandrin
codeofandrin

Reputation: 1558

From this post

With discord.py 2.0 you can get the BotIntegration of a server and with that the user who invited the bot.

Example

from discord.ext import commands

bot = commands.Bot()

@bot.event
async def on_guild_join(guild):
    # get all server integrations
    integrations = await guild.integrations()

    for integration in integrations:
        if isinstance(integration, discord.BotIntegration):
            if integration.application.user.name == bot.user.name:
                bot_inviter = integration.user# returns a discord.User object
                
                # send message to the inviter to say thank you
                await bot_inviter.send("Thank you for inviting my bot!!")
                break

Note: guild.integrations() requires the Manage Server (manage_guild) permission.


References:

Upvotes: 0

Diggy.
Diggy.

Reputation: 6944

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:

Upvotes: 3

Related Questions