首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Discord.JS排行榜

Discord.JS排行榜
EN

Stack Overflow用户
提问于 2021-02-11 06:02:37
回答 1查看 145关注 0票数 3

我目前有一些代码来检测你可以在一定的秒数内点击一个反应的次数。

我正在尝试为每个人做一个排行榜,用最高的CPS (每秒点击量)保存前10名。除此之外,代码运行得很好,但我被困在排行榜上了。

这是我的代码:

代码语言:javascript
复制
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)
    
});
EN

回答 1

Stack Overflow用户

发布于 2021-02-11 07:18:12

如果你想要一个排行榜,你需要将它保存到一个文件中,使用一个数据库,或者(如果你不介意,当你重新启动你的机器人时,你会丢失它)你可以在你的client.on('message')之外声明一个变量。对于测试目的,它似乎已经足够好了。

您可以创建一个topUsers数组,在每次游戏结束时将玩家推入该数组,然后对其进行排序并创建排行榜。

检查以下代码片段:

代码语言:javascript
复制
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);
  });
});

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66145762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档