【问题标题】:How to change discord-xp from giving XP per message to giving XP per minute?如何将 discord-xp 从每条消息提供 XP 更改为每分钟提供 XP?
【发布时间】:2021-05-24 18:34:30
【问题描述】:

机器人会为每条随机消息提供 50 到 100 点之间的随机 XP,这真的很慢。每分钟拥有 XP 会更好。

这是提供XP的代码,来自discord-xp中的levels.js:

client.on("message", async message => {
    if (!message.guild) return;
    if (message.author.bot) return;
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).trim().split(/ +/);
    const command = args.shift().toLowerCase();

    const randomXp = Math.floor(Math.random() * 50) + 50;
    console.log(randomXp);
    const hasLeveledUp = await Levels.appendXp(message.author.id, message.guild.id, randomXp);
    if (hasLeveledUp) {
        const user = await Levels.fetch(message.author.id, message.guild.id);
        message.channel.send(`<:GG:807231058507464735> ${message.author}, you just leveled up to **level ${user.level}!** Keep going!`);
    }
});

我已经试过了

const mongoose = require("mongoose");
 
const LevelSchema = new mongoose.Schema({
  userID: { type: String },
  guildID: { type: String },
  xp: { type: Number, default: 0 },
  level: { type: Number, default: 0 },
  lastUpdated: { type: Date, default: new Date() },
  last_message: 0,
});
 
module.exports = mongoose.model('Levels', LevelSchema);

if (Date.now() - levels.last_message > 60000) {
    const randomXp = levels.xp += random.int(15, 25);
    levels.last_message = Date.now();
}

在那里,但它并没有完全按照我的意愿行事。

【问题讨论】:

  • 您尝试解决问题的代码是..? (“请为我重写这段代码,让它做我想做的事”不是问题。)
  • 我尝试过使用: if (Date.now() - levels.last_message > 60000) { const randomXp = levels.xp += random.int(15, 25); level.last_message = Date.now(); levels.js 是 discord-xp 中的一个文件,其中包含所有数据,以下是其内容:pastebin.pl/view/23d4914a

标签: node.js discord.js


【解决方案1】:

我不确定 Mongoose 是否真的有必要。我会从这样的事情开始。

const checkpoint = {};

client.on("message", async message => {
    if (!message.guild) return;
    if (message.author.bot) return;
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).trim().split(/ +/);
    const command = args.shift().toLowerCase();
    const authorKey = message.author.id + '|' + message.guild.id;

    if (!(authorKey in checkpoint)) checkpoint[authorKey] = Date.now();
    const minutes = Math.floor((Date.now() - checkpoint[authorKey]) / 60000);
    if (!minutes) return; else checkpoint[authorKey] = Date.now();

    // add a random XP amount for each minute (better than `minutes * random.int(15, 25)`)
    let xpEarned = 0;
    for (let m = 0; m < minutes; m++) xpEarned += random.int(15, 25);

    const hasLeveledUp = await Levels.appendXp(message.author.id, message.guild.id, xpEarned);
    if (hasLeveledUp) {
        const user = await Levels.fetch(message.author.id, message.guild.id);
        message.channel.send(`<:GG:807231058507464735> ${message.author}, you just leveled up to **level ${user.level}!** Keep going!`);
    }
});

【讨论】:

  • 嘿,请回答,但它似乎不起作用。你能解释一下 const 检查点的用途或作用吗?
  • @Dillan 我不知道“似乎不起作用”是什么意思。也许你应该更具体? checkpoint 保存每个公会和用户的最后一条消息的时间。我认为通过阅读代码会变得很清楚。
猜你喜欢
  • 2021-02-09
  • 2012-09-01
  • 1970-01-01
  • 2021-06-11
  • 2021-06-21
  • 2018-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多