尝试制作排行榜脚本时已确认交互

问题描述 投票:0回答:1

我正在尝试制作一个排行榜,但它说交互已被承认,我对如何解决这个问题感到非常困惑,如果有人可以提供帮助,那就太棒了!

const { Client, Intents, MessageEmbed, MessageActionRow, MessageButton } = require('discord.js');
const fs = require('fs');
const client = new Client({ 
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MESSAGES,
    Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
    Intents.FLAGS.GUILD_MESSAGE_TYPING,
    Intents.FLAGS.DIRECT_MESSAGES,
  ],
});

const { token, prefix } = require('./config.json');

// Check if leaderboard file exists
let leaderboard = [];
if (fs.existsSync('leaderboard.json')) {
  // If file exists, read the file and parse it to JSON
  leaderboard = JSON.parse(fs.readFileSync('leaderboard.json', 'utf8'));
} else {
  // If file doesn't exist, create a new file with an empty array
  fs.writeFileSync('leaderboard.json', JSON.stringify([]));
}

let currentPage = 0; // Define currentPage at a higher scope
let leaderboardMessage = null;

// Function to update leaderboard
function updateLeaderboard() {
  // Sort leaderboard by points in descending order
  leaderboard.sort((a, b) => b.points - a.points);

  // Create embed for leaderboard
  const embed = new MessageEmbed()
    .setTitle(`Leaderboard - Page ${currentPage + 1}`)
    .setColor('#0099ff')
    .setDescription('Tournament Leaderboard');

  // Add players to the embed
  leaderboard.slice(currentPage * 10, (currentPage + 1) * 10).forEach((player, index) => {
    embed.addField(`#${currentPage * 10 + index + 1} ${player.name}`, `Points: ${player.points}\nWins: ${player.wins}\nLosses: ${player.losses}`);
  });

  // Create buttons for navigation
  const row = new MessageActionRow()
    .addComponents(
      new MessageButton()
        .setCustomId('previous')
        .setLabel('Previous')
        .setStyle('PRIMARY'),
      new MessageButton()
        .setCustomId('next')
        .setLabel('Next')
        .setStyle('PRIMARY'),
    );

  // Find and update existing leaderboard message
  const channel = client.channels.cache.get('1211897926276751398');
  if (leaderboardMessage) {
    leaderboardMessage.edit({ embeds: [embed], components: [row] });
  } else {
    channel.send({ embeds: [embed], components: [row] }).then(message => {
      leaderboardMessage = message;
    });
  }

  // Write the updated leaderboard back to the file
  fs.writeFileSync('leaderboard.json', JSON.stringify(leaderboard));
}

// Function to add player to leaderboard
function addPlayerToLeaderboard(name) {
  const playerExists = leaderboard.find(player => player.name === name);
  if (!playerExists) {
    leaderboard.push({ name, points: 0, wins: 0, losses: 0 });
  }
}

// Bot ready event
client.once('ready', () => {
  console.log('Bot is ready!');
  updateLeaderboard();
  setInterval(updateLeaderboard, 10 * 60 * 1000); // Update leaderboard every 10 minutes
});

// Bot message event
client.on('message', message => {
  if (!message.content.startsWith(prefix) || message.author.bot) return;

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

  if (command === 'enterlb') {
    addPlayerToLeaderboard(message.author.username);
    updateLeaderboard();
  }
});

// Bot interaction event
client.on('interaction', interaction => {
  if (!interaction.isButton()) return;

  if (interaction.customId === 'previous') {
    if (currentPage > 0) {
      currentPage--;
    }
  } else if (interaction.customId === 'next') {
    if (currentPage < Math.ceil(leaderboard.length / 10) - 1) {
      currentPage++;
    }
  }

  updateLeaderboard();
  interaction.reply({ content: 'Page updated!', ephemeral: true });
});

client.login(token);

我尝试了很多方法,但仍然无法弄清楚,我尝试使用 json 文件leaderboard.json,但这似乎不起作用。

discord.js leaderboard
1个回答
0
投票

导致此错误的可能原因有多种。尝试检查下面几行:

  • 初始响应时间超过 3 秒,因此您需要推迟响应
  • 两个进程处理相同的命令
  • 您已经回复了互动,因此您
    <Interaction>.editReply()
    <Interaction>.followUp()

但一般来说,这可能来自于您的机器人的重复实例。因此,请检查您的机器人是否仅在一个终端(本地或远程)中运行

© www.soinside.com 2019 - 2024. All rights reserved.