我的“问题”更多是我要添加的功能,我使用了本指南: https://anidiots.guide/coding-guides/sqlite-based-points-system 我对代码进行了一些更改,主要是给您随机的XP,我希望编辑升级所需的XP。
现在这是一个静态数量,需要5000才能升级。我正在尝试使其每次升级时所需的额外量增加5000。
当前,它的工作方式如下:
第1级到第2级=需要5000总经验值
2至3级=总共需要10000 xp
当前,升级所需的数量始终在每个级别之间为5000。
这就是我想要的工作方式:
第1级到第2级=需要5000总经验值
2至3级=总共需要15000 XP
第2级将达到5000,第3级则达到10000,依此类推(每次升级所需的金额增加5000)
我花了2个小时的大部分时间来尝试不同的事情,主要是看代码完全超出了我的理解范围。 我相信这样做是可行的,但我不知道它是否正确
if (score.level == '1') {
nextLevel = 5000
}
if (score.level == '2' {
nextLevel = 10000
}
我高度怀疑这是正确的,否则,由于我计划拥有100个级别,因此我的消息事件会很长
完整的代码:
let score;
if (message.guild) {
score = bot.getScore.get(message.author.id, message.guild.id);
if (!score) {
score = {
id: `${message.guild.id}-${message.author.id}`,
user: message.author.id,
guild: message.guild.id,
points: 0,
level: 1,
};
}
const xpAdd = Math.floor(Math.random() * 10) + 50;
const curxp = score.points;
const curlvl = score.level;
const nxtLvl = score.level * 5000;
score.points = curxp + xpAdd;
if (nxtLvl <= score.points) {
score.level = curlvl + 1;
const lvlup = new MessageEmbed()
.setAuthor(
`Congrats ${message.author.username}`,
message.author.displayAvatarURL()
)
.setTitle('You have leveled up!')
.setThumbnail('https://i.imgur.com/lXeBiMs.png')
.setColor(color)
.addField('New Level', curlvl + 1);
message.channel.send(lvlup).then(msg => {
msg.delete({
timeout: 10000,
});
});
}
bot.setScore.run(score);
}
按原样运行的代码可以按预期工作,但是按原样运行不是很好,因为从30-31级获得的收益不高,因为它与从1-2级获得的XP数量相同
答案 0 :(得分:1)
这里有一个公式可以解决问题(如果我正确理解了您的问题):
const nxtLvl = 5000 * (Math.pow(2, score.level) - 1);
这提供了以下xp要求以进行升级:
1->2: 5000
2->3: 15000
3->4: 35000
4->5: 75000
5->6: 155000
答案 1 :(得分:0)
尝试这样的事情:
const levels = [0, 5000, 15000, 30000, 50000, 75000];
....
nextLevel = levels[score.level];
@Dan您的意思是这样的:
nextLevel = 5000 * Math.round( score.level * (score.level + 1) / 2 );
答案 2 :(得分:0)
这是我正在使用的代码 但问题是我无法添加或删除 XP 也是我用划痕做的,所以我很疯狂地理解这个
let Discord;
let Database;
if (typeof window !== "undefined") {
Discord = DiscordJS;
Database = EasyDatabase;
} else {
Discord = require("discord.js");
Database = require("easy-json-database");
}
const delay = (ms) => new Promise((resolve) => setTimeout(() => resolve(), ms));
const s4d = {
Discord,
client: null,
tokenInvalid: false,
reply: null,
joiningMember: null,
database: new Database("./db.json"),
checkMessageExists() {
if (!s4d.client) throw new Error('You cannot perform message operations without a Discord.js client')
if (!s4d.client.readyTimestamp) throw new Error('You cannot perform message operations while the bot is not connected to the Discord API')
}
};
s4d.client = new s4d.Discord.Client({
fetchAllMembers: true
});
s4d.client.on('raw', async (packet) => {
if (['MESSAGE_REACTION_ADD', 'MESSAGE_REACTION_REMOVE'].includes(packet.t)) {
const guild = s4d.client.guilds.cache.get(packet.d.guild_id);
if (!guild) return;
const member = guild.members.cache.get(packet.d.user_id) || guild.members.fetch(d.user_id).catch(() => {});
if (!member) return;
const channel = s4d.client.channels.cache.get(packet.d.channel_id);
if (!channel) return;
const message = channel.messages.cache.get(packet.d.message_id) || await channel.messages.fetch(packet.d.message_id).catch(() => {});
if (!message) return;
s4d.client.emit(packet.t, guild, channel, message, member, packet.d.emoji.name);
}
});
var member_xp, member_level;
s4d.client.login('My Dumb Token').catch((e) => {
s4d.tokenInvalid = true;
s4d.tokenError = e;
});
s4d.client.on('message', async (s4dmessage) => {
if (!((s4dmessage.member).user.bot)) {
member_xp = s4d.database.get(String(('xp-' + String(s4dmessage.author.id))));
member_level = s4d.database.get(String(('level-' + String(s4dmessage.author.id))));
if (!member_xp) {
member_xp = 0;
} else if (!member_level) {
member_level = 0;
}
s4d.database.set(String(('xp-' + String(s4dmessage.author.id))), (member_xp + 1));
member_xp = member_xp + 1;
if (member_xp > 100) {
s4d.database.set(String(('level-' + String(s4dmessage.author.id))), (member_level + 1));
member_level = member_level + 1;
s4dmessage.channel.send(String((['Congratulations, ', s4dmessage.member, 'you jumped to level ', member_level, '!!'].join(''))));
}
if ((s4dmessage.content) == '-level') {
s4dmessage.channel.send(String(([s4dmessage.member, ', you are currently level: ', member_level].join(''))));
} else if ((s4dmessage.content) == '-xp') {
s4dmessage.channel.send(String(([s4dmessage.member, ', you need ', 100 - member_xp, ' to jump to level ', member_level + 1].join(''))));
}
}
});
s4d;