【问题标题】:I'm trying to create an invite tracker using discord.py我正在尝试使用 discord.py 创建邀请跟踪器
【发布时间】:2020-09-18 04:01:07
【问题描述】:

好的,我的目标是创建一个邀请跟踪器。目前,您可以运行命令/invites,它会告诉您您有多少邀请。这是它的代码。

@client.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"{ctx.author.mention}, you've invited {totalInvites} member{'' if totalInvites == 1 else 's'}!")

我想知道是否可以通过/invites @user 找到其他人的邀请。另外,更进一步,我可以创建一个/leaderboards 命令吗?它应该显示一个嵌入列表,其中包含前 10 名受邀者的列表以及他们有多少邀请。如果有办法在有人离开服务器时减去邀请数,我会很高兴。

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:
    @client.command()
    async def invites(ctx, usr: discord.Member=None):
        if usr == None:
           user = ctx.author
        else:
           user = usr
        total_invites = 0
        for i in await ctx.guild.invites():
            if i.inviter == user:
                total_invites += i.uses
        await ctx.send(f"{user.name} has invited {totalInvites} member{'' if totalInvites == 1 else 's'}!")
    

    抱歉,我无法实际运行代码。如果上面的代码产生错误请告诉我。

    【讨论】:

    • 抱歉,我不知道这是干什么用的。是为了/invites @user吗?
    • @iCrySam 它是您的函数的修改版本,允许您指定用户。您可以正常运行它 (/invites),它将获得所有 您的 邀请,或者您可以运行 /invites @user,它将获得该用户的所有邀请。
    • 对不起,我对这个东西真的很陌生,你能解释一下代码是如何工作的吗?此外,hasteb.in/diwudoda.py 似乎也不起作用。我在链接中包含了错误。
    • @iCrySam 代码的问题在于,当您发送消息时,您发送的是带有变量totalInvites 的邀请数,我将其重命名为“total_invites”。我在这里修复了它:hasteb.in/eyovibix.py。我还在其他代码中添加了更多 cmets,这里:hasteb.in/galoqehe.py
    • hasteb.in/eyovibix.py 发送此错误discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'str' object has no attribute 'name'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多