python - Discord.py 显示谁邀请了用户

标签 python discord discord.py

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

最佳答案

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

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

然后保存。

创建一个名为invites 的 channel ,然后填写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 .

关于python - Discord.py 显示谁邀请了用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44594309/

相关文章:

python - 使用 python 的 TCP 端口 - 如何将命令输出转发到 tcp 端口?

node.js - 仅在一台服务器上测试不和谐机器人discordjs

python - 如何创建赋予提到的用户角色的机器人命令 |不和谐.py

discord.py - 在discord.py中实现免费字典Api时出错

python - Django TimeLimitExceeded 错误

python - 在 IPython 5.x 中跳过历史搜索的长条目

python - pandas:如何为列中的每个子字符串复制一个值

python - 如何修复 "UnicodeDecodeError: ' cp94 9' codec can' t 解码位置 24 : illegal multibyte sequence"中的字节 0xeb

Python-属性错误: 'function' object has no attribute 'author'

python - Discord 丰富的嵌入按钮