7

I want to send a DM to the user, who invited/added the bot to his server.
I noticed that it's displayed in the audit log. Can I fetch that and get the user or is there a easier way to achieve that?

Example:
bot = commands.Bot()

@bot.event
async def on_guild(guild, inviter):
    await inviter.send("Thanks for adding the bot to your server!")
0

3 Answers 3

4

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:

1

Discord.py doesn't support Bot Integrations yet. Please check this Pull Request. Once it is merged, you can do integration.user to get the user who invited the bot.

1
  • integration.user does not guarantee that it is a bot's inviter because e.g. it could also be a StreamIntegration.
    – puncher
    Commented Aug 6, 2022 at 1:04
1

In discord.py 1.7.3 and lower does not have any actual method. However you can instead fetch the Audit Log Entry (documentation) and find out who invited the Bot from there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.