简体   繁体   English

discord.py:如何让邀请/添加机器人到他的服务器的用户? [解决方案]

[英]discord.py: How to get the user who invited/added the bot to his server? [solution]

I want to send a DM to the user, who invited/added the bot to his server.我想向邀请/添加机器人到他的服务器的用户发送 DM。
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!")

With discord.py 2.0 you can get the BotIntegration of a server and with that the user who invited the bot.使用discord.py 2.0 ,您可以获得服务器的BotIntegration以及邀请机器人的用户。

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.注意: guild.integrations()需要Manage Server ( manage_guild ) 权限。


References:参考:

Discord.py doesn't support Bot Integrations yet. Discord.py 尚不支持机器人集成。 Please check this Pull Request .请检查此拉取请求 Once it is merged, you can do integration.user to get the user who invited the bot.合并后,您可以执行integration.user以获取邀请机器人的用户。

In discord.py 1.7.3 and lower does not have any actual method.discord.py 1.7.3及更低版本中没有任何实际方法。 However you can instead fetch the Audit Log Entry ( documentation ) and find out who invited the Bot from there.但是,您可以取而代之的是获取审计日志条目( 文档)并从那里找出是谁邀请了机器人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM