Discord.py:如何获取邀请/添加机器人到其服务器的用户?[解决方案]

pun*_*her 7 python discord discord.py

我想向邀请/添加机器人到他的服务器的用户发送一条私信。
我注意到它显示在审核日志中。我可以获取它并获得用户,还是有更简单的方法来实现这一目标?

例子:
bot = commands.Bot()

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

pun*_*her 4

使用discord.py 2.0,您可以获得BotIntegration服务器的信息以及邀请机器人的用户。

例子

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
Run Code Online (Sandbox Code Playgroud)

注: guild.integrations()需要Manage Servermanage_guild)权限。


参考: