Math Bot 保存数据并对它们求和 | Discord.js
Math Bot keep data and sum them | Discord.js
我想创建一个命令来打印我写的数字加上该数字的 50%。例如,!te 100
打印 150。
我这样做了,但问题是我不知道如何让命令保留我在这个命令中使用的所有数字,以及当我执行 !sum
命令时对所有这些数字求和使用 !clear
命令清除数据,以便我可以添加更多数字来求和(警告我发现 50% 加上数字的数字)。
这是我的代码:
bot.on("message", (message) => {
if (message.content.includes("!te")) {
let ppis = message.content.substring(botprefix.length).split(" ");
message.channel.send(((50 / 100) * parseInt(ppis[1], 10)) + parseInt(ppis[1], 10))
}
});
如果您可以将这些存储在内存中并且在每次重启机器人时都从零开始,您可以使用简单的 collection or a map 来跟踪用户发送的号码。您可以使用作者的 ID 作为键,并将他们发送的数字数组作为值:
const map = new Discord.Collection();
client.on('message', async (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'boost') {
if (!args[0] || isNaN(args[0])) {
return message.channel.send('You have to provide a number');
}
// add the 50%
const amount = parseInt(args[0], 10) / 2 + parseInt(args[0], 10);
if (map.has(message.author.id)) {
map.set(message.author.id, [...map.get(message.author.id), amount]);
} else {
map.set(message.author.id, [amount]);
}
return message.channel.send(`\`${amount}\` added`);
}
if (command === 'sum') {
if (!map.has(message.author.id))
return message.channel.send('It seems you have no numbers yet. Maybe add some?');
const sum = map.get(message.author.id).reduce((a, b) => a + b, 0);
return message.channel.send(`The sum is \`${sum}\``);
}
if (command === 'clear') {
const removed = map.delete(message.author.id);
return message.channel.send(
removed
? 'Numbers are cleared'
: 'It seems you have no numbers yet. Maybe add some?',
);
}
});
我想创建一个命令来打印我写的数字加上该数字的 50%。例如,!te 100
打印 150。
我这样做了,但问题是我不知道如何让命令保留我在这个命令中使用的所有数字,以及当我执行 !sum
命令时对所有这些数字求和使用 !clear
命令清除数据,以便我可以添加更多数字来求和(警告我发现 50% 加上数字的数字)。
这是我的代码:
bot.on("message", (message) => {
if (message.content.includes("!te")) {
let ppis = message.content.substring(botprefix.length).split(" ");
message.channel.send(((50 / 100) * parseInt(ppis[1], 10)) + parseInt(ppis[1], 10))
}
});
如果您可以将这些存储在内存中并且在每次重启机器人时都从零开始,您可以使用简单的 collection or a map 来跟踪用户发送的号码。您可以使用作者的 ID 作为键,并将他们发送的数字数组作为值:
const map = new Discord.Collection();
client.on('message', async (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'boost') {
if (!args[0] || isNaN(args[0])) {
return message.channel.send('You have to provide a number');
}
// add the 50%
const amount = parseInt(args[0], 10) / 2 + parseInt(args[0], 10);
if (map.has(message.author.id)) {
map.set(message.author.id, [...map.get(message.author.id), amount]);
} else {
map.set(message.author.id, [amount]);
}
return message.channel.send(`\`${amount}\` added`);
}
if (command === 'sum') {
if (!map.has(message.author.id))
return message.channel.send('It seems you have no numbers yet. Maybe add some?');
const sum = map.get(message.author.id).reduce((a, b) => a + b, 0);
return message.channel.send(`The sum is \`${sum}\``);
}
if (command === 'clear') {
const removed = map.delete(message.author.id);
return message.channel.send(
removed
? 'Numbers are cleared'
: 'It seems you have no numbers yet. Maybe add some?',
);
}
});