問題描述
我目前正在嘗試找出一種方法來了解誰邀請了用戶.從官方文檔中,我認為 member
類將具有顯示誰邀請他們的屬性,但事實并非如此.我對獲取邀請的用戶的可能方法有一個非常模糊的想法,那就是獲取服務器中的所有邀請,然后獲取使用次數,當有人加入服務器時,它會檢查是否有增加的邀請一種用途.但我不知道這是否是最有效的方法,或者至少是使用過的方法.
I am currently trying to figure out a way to know who invited a user. From the official docs, I would think that the member
class would have an attribute showing who invited them, but it doesn't. I have a very faint idea of a possible method to get the user who invited and that would be to get all invites in the server then get the number of uses, when someone joins the server, it checks to see the invite that has gone up a use. But I don't know if this is the most efficient method or at least the used method.
推薦答案
制作一個 config.json
文件,內容為
Make a config.json
file with the content of
{
"token": "Bot token here",
"server-id": "server id here",
"logs-channel-id": "invite channel here"
}
然后保存.
創建一個名為 invites
的頻道,然后填寫 config.json
文件.然后創建一個名為 bot.py
的文件并將內容放入:
Make a channel called invites
then fill in the config.json
file. And then create a file called bot.py
and put the contents of:
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.
Then open the terminal and run python3 bot.py
. And for more help join this.
這篇關于Discord.py 顯示誰邀請了用戶的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!