我目前有一些代码可以检测您在特定秒数内可以点击多少次反应。
我试图为每个人制作一个排行榜,以最高 CPS(每秒点击次数)保存前 10 名。否则代码运行良好,但我被困在排行榜上。
这是我的代码:
let CPStest = new Discord.MessageEmbed()
.setColor("#8f82ff")
.setTitle("CPS Test")
.setDescription(`Alright, ${message.author.username}. Ready to begin your CPS test? All you have to do is spam click this reaction above ^ as fast as you can, you have 10 seconds before the timer runs out. Good Luck!`)
.setFooter("Note: This may be inaccurate depending on Discord API and Rate Limits.")
message.channel.send(CPStest)
message.react('?️');
// Create a reaction collector
const filter = (reaction, user) => reaction.emoji.name === '?️' && user.id === message.author.id;
var clicks = 1 * 3; // (total clicks)
var timespan = 10; // (time in seconds to run the CPS test)
const collector = message.createReactionCollector(filter, { time: timespan * 1000 });
collector.on('collect', r => {
console.log(`Collected a click on ${r.emoji.name}`);
clicks++;
});
collector.on('end', collected => {
message.channel.send(`Collected ${clicks} raw clicks (adding reactions only)`);
clicks *= 2;
message.channel.send(`Collected ${clicks} total approximate clicks.`);
var cps = clicks / timespan / 0.5; // Use Math.round if you want to round the decimal CPS
message.channel.send(`Your CPS averaged ~${cps} clicks per second over ${timespan} seconds.`);
let userNames = '';
const user = (bot.users.fetch(message.author)).tag;
userNames += `${user}\n`;
let cpsLeaderboard = new Discord.MessageEmbed()
.setColor("#8f82ff")
.setTitle("Current CPS Record Holders:")
.addFields({ name: 'Top 10', value: userNames, inline: true },
{ name: 'CPS', value: cps, inline: true })
.setTimestamp()
message.channel.send(cpsLeaderboard)
});
答案 0 :(得分:0)
如果你想要一个排行榜,你要么需要将它保存到一个文件中,使用一个数据库,或者(如果你对它没有意见,当你重新启动你的机器人时你会丢失它)你可以声明一个client.on('message')
之外的变量。用于测试目的似乎已经足够了。
您可以创建一个 topUsers
数组,在每次游戏结束时将玩家推送到该数组,然后对其进行排序并创建排行榜。
检查以下代码段:
const topUsers = [];
client.on('message', (message) => {
if (message.author.bot) return;
let CPStest = new Discord.MessageEmbed()
.setColor('#8f82ff')
.setTitle('CPS Test')
.setDescription(
`Alright, ${message.author.username}. Ready to begin your CPS test? All you have to do is spam click this reaction above ^ as fast as you can, you have 10 seconds before the timer runs out. Good Luck!`,
)
.setFooter(
'Note: This may be inaccurate depending on Discord API and Rate Limits.',
);
message.channel.send(CPStest);
message.react('?️');
// Create a reaction collector
const filter = (reaction, user) =>
reaction.emoji.name === '?️' && user.id === message.author.id;
let clicks = 1 * 3; // (total clicks)
const timespan = 10; // (time in seconds to run the CPS test)
const collector = message.createReactionCollector(filter, {
time: timespan * 1000,
});
collector.on('collect', (r) => {
console.log(`Collected a click on ${r.emoji.name}`);
clicks++;
});
collector.on('end', (collected) => {
message.channel.send(
`Collected ${clicks} raw clicks (adding reactions only)`,
);
clicks *= 2;
message.channel.send(`Collected ${clicks} total approximate clicks.`);
const cps = clicks / timespan / 0.5;
message.channel.send(
`Your CPS averaged ~${cps} clicks per second over ${timespan} seconds.`,
);
topUsers.push({ tag: message.author, cps });
let cpses = '';
let usernames = '';
topUsers
// sort the array by CPS in descending order
.sort((a, b) => b.cps - a.cps)
// only show the first ten users
.slice(0, 10)
.forEach((user) => {
cpses += `${user.cps}\n`;
usernames += `${user.tag}\n`;
});
const cpsLeaderboard = new Discord.MessageEmbed()
.setColor('#8f82ff')
.setTitle('Current CPS Record Holders:')
.addFields(
{ name: 'Top 10', value: usernames, inline: true },
{ name: 'CPS', value: cpses, inline: true },
)
.setTimestamp();
message.channel.send(cpsLeaderboard);
});
});