【问题标题】:Getting the users total invites in Discord.py在 Discord.py 中获取用户总邀请
【发布时间】:2020-09-04 15:15:03
【问题描述】:

我正在尝试向我的机器人添加一个命令,该命令会回复用户邀请到服务器的总人数

我的代码:

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

我做错了什么?

【问题讨论】:

    标签: discord discord.py


    【解决方案1】:

    您的想法几乎是正确的!


    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!")
    

    首先,我会遍历公会中的每个邀请,检查每个邀请的创建者。如果邀请的创建者与执行该命令的用户匹配,则它将邀请已被使用的次数添加到运行总数中。

    你不需要包含{'' if totalInvites == 1 else 's'},这只是为了他们邀请了1个人的奇怪情况(将member变成复数-members)。


    参考资料:

    【讨论】:

    • 您好,谢谢您的回复!我已经尝试过这段代码,我收到以下错误:for i in message.guild.invites: TypeError: 'method' object is not iterable
    • 不用担心,非常感谢您的帮助,我将您的答案标记为解决方案。
    • 不用担心,很高兴您成功了!祝机器人好运:^)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    • 2021-07-15
    • 1970-01-01
    • 2020-12-02
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多