【问题标题】:Earn Credits for Each Message Sent Bot Discord.js为发送的每条消息赚取积分 Bot Discord.js
【发布时间】:2021-01-21 18:13:25
【问题描述】:

我的 Discord 机器人需要帮助,我正在使用 discord.js

所以我做了这个:

client.on("message", async(message) => {
  if (!message.guild) return;
  if (message.author.bot) return;
  if (mentionn) {

    const randomAmountOfCredits = Math.floor(Math.random() * 29) + 1; // Min 1, Max 30
    (message.author.id, message.guild.id, randomAmountOfCredits);
    if (collected.first().content === number) {
      m.delete();
      collected.first().delete();
      credits[mentionn.id].credits += (+randomAmountOfCredits);
      fs.writeFile(path, JSON.stringify(credits, null, 5), function(err) {
        if (err) console.log(err)
      });

      if (collected.first().content !== number) {
        return m.delete();
      } else if (!mentionn) {
        credits[author].credits += (+randomAmountOfCredits);
        fs.writeFile(path, JSON.stringify(credits, null, 5), function(err) {
          if (err) console.log(err)
        });
      }
    }
  }
});

这就像每发送一条消息都获得积分,但我没有获得任何积分,控制台上没有错误:

Thats the console

在这里我必须获得学分,但没有任何反应: Thats the results

我是首发,谢谢

【问题讨论】:

  • 第三个 if 子句中的 mentionn 是什么?
  • 问:第三个 if 子句中提到了什么? – Pascal Stockert 26 分钟前 A:当脚本检测到客户端(打字的用户)得到它的时候?
  • @Issemoran 看起来你不熟悉 SO。快速提醒:如果答案对您有帮助,请点赞。如果它解决了您的问题,请单击复选标记。

标签: discord discord.js bots currency credits


【解决方案1】:

我不明白这段代码 sn-p 的一行,我不明白你对 Pascal Stockert 问题的回答,我也不明白 collected(来自收集器?)var 在这里做什么。但我会尽力而为。这就是我添加此功能的方式,您可以随意修改此代码并添加任意数量的条件。

client.on("message", async msg => {
    // First, generate a random number
    const newXP = Math.ceiling(Math.random() * 30);
    // Read the existing XPs
    const file = fs.readFileSync("xp.json");
    // Add some XP to the author of the message
    file[msg.author.id].credits += newXP;
    // Write new XP file
    fs.writeFileSync("xp.json", JSON.stringify(file, null, 4));
    // Done.
});

我没加,不过要小心,file[msg.author.id]不存在的情况你好像没有处理。

【讨论】:

  • 你好@a2br,文件是 credits.json 而不是 xp.json prnt.sc/znh7u0 这就是文件的样子! prnt.sc/znh94p 和我的积分在发送消息时仍然相同
  • const args = message.content.split(' '); const credits = require('./credits.json');常量路径 = './credits.json';常量提及 = message.mentions.users.first() || client.users.cache.get(args[1]) ||消息.作者;常量提及 = message.mentions.users.first() || client.users.cache.get(args[1]);常量作者 = message.author.id;常量余额 = args[2];
猜你喜欢
  • 2017-10-17
  • 2020-06-13
  • 1970-01-01
  • 2021-01-12
  • 2021-03-26
  • 1970-01-01
  • 2021-10-25
  • 2019-10-25
  • 1970-01-01
相关资源
最近更新 更多