为发送的每条消息赚取积分 Bot Discord.js

时间:2021-01-21 18:13:25

标签: discord discord.js bots currency credits

我的 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

我是初学者,谢谢

2 个答案:

答案 0 :(得分:0)

我没有理解这段代码片段中的一行,也没有理解您对 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]不存在的情况你好像没处理好。

答案 1 :(得分:0)

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("credits.json");
// Add some XP to the author of the message
credits[author].credits += -newXP;
    credits[mention.id].credits += +newXP;
  fs.writeFileSync("xp.json", JSON.stringify(file, null, 4));

我试过了,还是不行