【问题标题】:Discord.py show who invited a userDiscord.py 显示谁邀请了用户
【发布时间】:2017-11-19 12:43:04
【问题描述】:

我目前正在尝试找出一种方法来了解谁邀请了用户。从官方文档中,我认为 member 类将具有显示谁邀请他们的属性,但事实并非如此。我对获取邀请的用户的可能方法有一个非常模糊的想法,那就是获取服务器中的所有邀请,然后获取使用次数,当有人加入服务器时,它会检查是否有增加的邀请一种用途。但我不知道这是否是最有效的方法,或者至少是使用过的方法。

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    制作一个config.json文件,内容为

    {
        "token": "Bot token here",
        "server-id": "server id here",
        "logs-channel-id": "invite channel here"
    }
    

    然后保存。

    创建一个名为invites 的频道,然后填写config.json 文件。然后创建一个名为bot.py的文件,并把内容:

    import asyncio
    import datetime
    import json
    import os
    import commands
    
    client = discord.Client()
    cfg = open("config.json", "r")
    tmpconfig = cfg.read()
    cfg.close()
    config = json.loads(tmpconfig)
    
    token = config["token"]
    guild_id = config["server-id"]
    logs_channel = config["logs-channel-id"]
    
    
    invites = {}
    last = ""
    
    async def fetch():
     global last
     global invites
     await client.wait_until_ready()
     gld = client.get_guild(int(guild_id))
     logs = client.get_channel(int(logs_channel))
     while True:
      invs = await gld.invites()
      tmp = []
      for i in invs:
       for s in invites:
        if s[0] == i.code:
         if int(i.uses) > s[1]:
          usr = gld.get_member(int(last))
          testh = f"{usr.name} **joined**; Invited by **{i.inviter.name}** (**{str(i.uses)}** invites)"
          await logs.send(testh)
       tmp.append(tuple((i.code, i.uses)))
      invites = tmp
      await asyncio.sleep(4)
    
    
    @client.event
    async def on_ready():
     print("ready!")
     await client.change_presence(activity = discord.Activity(name = "joins", type = 2))
    
    
    @client.event
    async def on_member_join(meme):
     global last
     last = str(meme.id)
    
    
    
    
    client.loop.create_task(fetch())
    client.run(token)
    

    然后打开终端并运行python3 bot.py。如需更多帮助,请加入this

    【讨论】:

    • 这工作得很好。一些 cmets:如果用户通过右键单击配置文件被邀请,并将邀请发送到服务器,则机器人不会对此进行重新定位。此外,支持服务器似乎无法正常工作,因为无法通过验证和进入。
    • 您能告诉我如何获取有关谁邀请的用户的信息吗?我只想要这个,而不是完整的代码。不过谢谢。
    【解决方案2】:

    在 Discord 中,您永远无法 100% 确定谁邀请了用户。

    使用 Invite,您可以知道是谁创建了邀请。

    使用 on_member_join,您可以知道谁加入了。

    所以,是的,您可能必须检查邀请,看看哪个邀请被撤销了。但是,您永远无法确定邀请了谁,因为任何人都可以在任何地方粘贴相同的邀请链接。

    【讨论】:

      【解决方案3】:

      查看邀请的使用次数,或使用次数用完并被撤销的情况,是查看用户如何被邀请到服务器的唯一方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-16
        • 2021-07-15
        • 2020-09-04
        • 2018-06-20
        • 1970-01-01
        • 2020-10-18
        • 1970-01-01
        相关资源
        最近更新 更多