<i id='rwTpF'><tr id='rwTpF'><dt id='rwTpF'><q id='rwTpF'><span id='rwTpF'><b id='rwTpF'><form id='rwTpF'><ins id='rwTpF'></ins><ul id='rwTpF'></ul><sub id='rwTpF'></sub></form><legend id='rwTpF'></legend><bdo id='rwTpF'><pre id='rwTpF'><center id='rwTpF'></center></pre></bdo></b><th id='rwTpF'></th></span></q></dt></tr></i><div id='rwTpF'><tfoot id='rwTpF'></tfoot><dl id='rwTpF'><fieldset id='rwTpF'></fieldset></dl></div>

    • <bdo id='rwTpF'></bdo><ul id='rwTpF'></ul>
  • <tfoot id='rwTpF'></tfoot>

    <small id='rwTpF'></small><noframes id='rwTpF'>

  • <legend id='rwTpF'><style id='rwTpF'><dir id='rwTpF'><q id='rwTpF'></q></dir></style></legend>

      1. 将冷却时间/计时器添加到 on_message [Discord.py]

        时间:2023-10-11
        <tfoot id='TAzWu'></tfoot>
        <i id='TAzWu'><tr id='TAzWu'><dt id='TAzWu'><q id='TAzWu'><span id='TAzWu'><b id='TAzWu'><form id='TAzWu'><ins id='TAzWu'></ins><ul id='TAzWu'></ul><sub id='TAzWu'></sub></form><legend id='TAzWu'></legend><bdo id='TAzWu'><pre id='TAzWu'><center id='TAzWu'></center></pre></bdo></b><th id='TAzWu'></th></span></q></dt></tr></i><div id='TAzWu'><tfoot id='TAzWu'></tfoot><dl id='TAzWu'><fieldset id='TAzWu'></fieldset></dl></div>
          <tbody id='TAzWu'></tbody>
            <bdo id='TAzWu'></bdo><ul id='TAzWu'></ul>

                <small id='TAzWu'></small><noframes id='TAzWu'>

                <legend id='TAzWu'><style id='TAzWu'><dir id='TAzWu'><q id='TAzWu'></q></dir></style></legend>
                • 本文介绍了将冷却时间/计时器添加到 on_message [Discord.py]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  I got into making a Discord bot in Python very recently (testing the grounds of Python with it) and created a functioning one with several commands myself. To widen its uses, I have added a level/XP system, which is working so far.

                  [...]
                  @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,但两者都没有得到我想要的结果.我不知道如何添加此冷却时间/计时器.

                  But to prevent users from just flooding/spamming some channels and rocketing to the top, I want to add a cooldown/timer on the awarding of XP. I have tried to add @commands.cooldown(1, 120, commands.BucketType.server) to both @bot.event and user_add_xp, but both do not get me the desired result. I have no other idea how to add this cooldown/timer.

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

                  In the end, I want the bot to only grant XP once every two minutes.

                  推荐答案

                  不确定是否可以仅使用 discord.py,但您可以存储最后一次将消息授予用户 XP 的时间在你的字典里.

                  Not sure if it's possible with just discord.py, but you can store the last time a message was awarded XP to a user in your dictionary.

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

                  The below code stores the number of seconds since a static start date (epoch) when a message awards XP. It then checks against this time when a new message event happens.

                  [...]
                  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)
                  [...]
                  

                  这篇关于将冷却时间/计时器添加到 on_message [Discord.py]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:是否可以使用 discord.py(从视频中的给定时间戳播 下一篇:Discord.py-Rewrite 获取列入黑名单的术语列表并检查

                  相关文章

                  最新文章

                • <legend id='jTxR8'><style id='jTxR8'><dir id='jTxR8'><q id='jTxR8'></q></dir></style></legend>

                    • <bdo id='jTxR8'></bdo><ul id='jTxR8'></ul>
                  1. <i id='jTxR8'><tr id='jTxR8'><dt id='jTxR8'><q id='jTxR8'><span id='jTxR8'><b id='jTxR8'><form id='jTxR8'><ins id='jTxR8'></ins><ul id='jTxR8'></ul><sub id='jTxR8'></sub></form><legend id='jTxR8'></legend><bdo id='jTxR8'><pre id='jTxR8'><center id='jTxR8'></center></pre></bdo></b><th id='jTxR8'></th></span></q></dt></tr></i><div id='jTxR8'><tfoot id='jTxR8'></tfoot><dl id='jTxR8'><fieldset id='jTxR8'></fieldset></dl></div>

                    <tfoot id='jTxR8'></tfoot>

                      <small id='jTxR8'></small><noframes id='jTxR8'>