【问题标题】:Add cooldown / timer to on_message [Discord.py]将冷却时间/计时器添加到 on_message [Discord.py]
【发布时间】:2018-10-21 20:08:31
【问题描述】:

我最近开始用 Python 制作一个 Discord 机器人(用它测试 Python 的基础),并自己创建了一个带有多个命令的功能性机器人。为了扩大它的用途,我添加了一个级别/XP 系统,该系统目前正在运行。

[...]
@bot.event
async def on_message(message):
        user_add_xp(message.author.id, 2)
        await bot.process_commands(message)

# commands go here

def user_add_xp(user_id, xp):
    if os.path.isfile('users.json'):
            try:
                    with open('users.json', 'r') as fp:
                            users = json.load(fp)
                    users[user_id]['xp'] += xp
                    with open('users.json', 'w') as fp:
                            json.dump(users, fp, sort_keys=True, indent=4)
            except KeyError:
                    with open('users.json', 'r') as fp:
                            users = json.load(fp)
                    users[user_id] = {}
                    users[user_id]['xp'] = xp
                    with open('users.json', 'w') as fp:
                            json.dump(users, fp, sort_keys=True, indent=4)
    else:
        users = {user_id: {}}
        users[user_id]['xp'] = xp
        with open('users.json', 'w') as fp:
                json.dump(users, fp, sort_keys=True, indent=4)
[...]

但为了防止用户只是泛滥/发送垃圾邮件某些频道并飙升至顶部,我想为 XP 奖励添加一个冷却时间/计时器。我尝试将@commands.cooldown(1, 120, commands.BucketType.server) 添加到@bot.eventuser_add_xp,但两者都没有得到我想要的结果。 我不知道如何添加此冷却时间/计时器。

最后,我希望机器人每两分钟只授予一次 XP。

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    不确定是否可以仅使用 discord.py,但您可以在字典中存储上次向用户授予 XP 的消息的时间。

    以下代码存储自静态开始日期 (epoch) 以来的秒数,此时消息奖励 XP。然后,它会检查发生新消息事件的时间。

    [...]
    import datetime
    
    epoch = datetime.datetime.utcfromtimestamp(0)
    
    @bot.event
    async def on_message(message):
        user_add_xp(message.author.id, 2)
        await bot.process_commands(message)
    
    # commands go here
    
    def user_add_xp(user_id, xp):
        if os.path.isfile('users.json'):
            try:
                with open('users.json', 'r') as fp:
                    users = json.load(fp)
    
                time_diff = (datetime.datetime.utcnow() - epoch).total_seconds() - users[user_id]['xp_time']
                if time_diff >= 120:
                    users[user_id]['xp'] += xp
                    users[user_id]['xp_time'] = (datetime.datetime.utcnow() - epoch).total_seconds()
                    with open('users.json', 'w') as fp:
                        json.dump(users, fp, sort_keys=True, indent=4)
            except KeyError:
                with open('users.json', 'r') as fp:
                    users = json.load(fp)
                users[user_id] = {}
                users[user_id]['xp'] = xp
                users[user_id]['xp_time'] = (datetime.datetime.utcnow() - epoch).total_seconds()
                with open('users.json', 'w') as fp:
                    json.dump(users, fp, sort_keys=True, indent=4)
        else:
            users = {user_id: {}}
            users[user_id]['xp'] = xp
            users[user_id]['xp_time'] = (datetime.datetime.utcnow() - epoch).total_seconds()
            with open('users.json', 'w') as fp:
                json.dump(users, fp, sort_keys=True, indent=4)
    [...]
    

    【讨论】:

    • 正是我需要的,工作顺利,做我想做的事。非常感谢您的帮助!
    猜你喜欢
    • 2021-05-02
    • 2022-01-16
    • 2021-03-25
    • 1970-01-01
    • 2021-04-21
    • 2021-05-15
    • 1970-01-01
    • 2020-10-14
    相关资源
    最近更新 更多