Kryfulli
Kryfulli

Reputation: 15

Is it possible to fetch the user inviting your bot?

When my Discord bot joins a guild, I want to send a private message to the member inviting the bot. I am using Discord.js. Now, i know there is a guildCreate event i can use on the client, that fires when the bot joins a guild.

This event however, only provides the Guild the bot has been invited in.

I've seen multiple bots sending me private messages this way, so i know there must be a way. One could get the guild owner and send him private message, but there is a chance that the owner is not the one inviting the bot. And unfortunately, since i was always the owner when it happened, i can't say if it was this or not.


Is there a way to retrieve the GuildMember, or the User that invited the bot ?

Upvotes: 0

Views: 501

Answers (1)

MrMythical
MrMythical

Reputation: 9041

This requires view audit log permissions for the bot:

let logs = await guild.fetchAuditLogs()
logs = logs.entries.filter(e => e.action === "BOT_ADD")
let user = logs.find(l => l.target?.id === client.user.id)?.executor
//user is the inviter

Upvotes: 1

Related Questions