获取Discord.py中的用户邀请总数 [英] Getting the users total invites in Discord.py
本文介绍了获取Discord.py中的用户邀请总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试向我的bot添加一个命令,该命令将回复用户邀请到服务器的总人数
我的代码:
if message.content.startswith('!invites'):
totalInvites = message.guild.invites
await message.channel.send("You have invited: " + totalInvites + " members to the server")
机器人回复为:
You have invited: <bound method Guild.invites of <Guild id=server_id_goes_here name='my bot' shard_id=None chunked=True member_count=12>> members to the server
我做错了什么?
推荐答案
您几乎想对了!
ON_MESSAGE事件用法:
@bot.event
async def on_message(message):
if message.content.startswith('!invites'):
totalInvites = 0
for i in await message.guild.invites():
if i.inviter == message.author:
totalInvites += i.uses
await message.channel.send(f"You've invited {totalInvites}
member{'' if totalInvites == 1 else 's'} to the server!")
命令修饰符用法:
@bot.command()
async def invites(ctx):
totalInvites = 0
for i in await ctx.guild.invites():
if i.inviter == ctx.author:
totalInvites += i.uses
await ctx.send(f"You've invited {totalInvites} member{'' if totalInvites == 1 else 's'} to the server!")
首先,我遍历协会中的每个INVITE,检查是谁创建了每个INVITE。如果INVITE的创建者与执行该命令的用户匹配,则它会将该INVITE的使用次数加到运行总数中。
您不需要包括{'' if totalInvites == 1 else 's'}
,这只是针对他们邀请了1个人的特殊情况(将member
变成复数-members
)。
引用:
Guild.invites
-代码最初无法工作,因为我忘记了这是协程(必须调用()
和await
ed)。Invite.uses
Invite.inviter
commands.command()
- F-stringsPython 3.6+
这篇关于获取Discord.py中的用户邀请总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文