这些按钮不能正常工作,因为它给了我以下错误: Discord button click error
const { MessageButton } = require("discord-buttons")
require("discord-buttons")(client);
import * as Discord from 'discord.js';
async function selectCategory(subject: string, message) {
let exitButton = new MessageButton()
.setStyle("blurple")
.setID("exit")
.setLabel("↩️")
let backButton = new MessageButton()
.setStyle("blurple")
.setID("back")
.setLabel("?")
let selectButton = new MessageButton()
.setStyle("blurple")
.setID("select")
.setLabel("☑️")
let nextButton = new MessageButton()
.setStyle("blurple")
.setID("next")
.setLabel("?")
const buttonArray = [exitButton, backButton, selectButton, nextButton];
let overviewEmbed = new Discord.MessageEmbed().setColor('0x4286f4').setDescription("Select a Category:")
let mybuttonsmsg = await message.channel.send({ embed: overviewEmbed, buttons: buttonArray })
const embedArray = [overviewEmbed]
let files:string;
if (subject === '?'){
files = fs.readdirSync('src/answers/Mathematics');
}else if (subject === '⚛️'){
files = fs.readdirSync('src/answers/Sciences');
}else if (subject === '?'){
files = fs.readdirSync('src/answers/Geography');
}else if (subject === '?'){
files = fs.readdirSync('src/answers/English');
}
for (let i = 0; i < files.length; i++) {
embedArray.push(new Discord.MessageEmbed()
.setColor('0x4286f4')
.setDescription(files[i].replace('.txt', ''))
)
}
let currentPage: number = 0;
const collector = mybuttonsmsg.createButtonCollector((button) => button.clicker.user.id === message.author.id, { time: 60e3 });
collector.on("collect", (b) => {
b.defer();
if (b.id == "3") {
//select
}
else if (b.id == "2") {
if (currentPage !== 0) {
--currentPage;
mybuttonsmsg.edit({ embed: embedArray[currentPage], buttons: buttonArray })
} else {
currentPage = embedArray.length - 1;
mybuttonsmsg.edit({ embed: embedArray[currentPage], buttons: buttonArray })
}
console.log('back button pressed');
}
else if (b.id == "4") {
if (currentPage < embedArray.length - 1) {
currentPage++;
mybuttonsmsg.edit({ embed: embedArray[currentPage], buttons: buttonArray })
} else {
currentPage = 0;
mybuttonsmsg.edit({ embed: embedArray[currentPage], buttons: buttonArray })
}
}
else if (b.id == "1") {
mybuttonsmsg.delete();
}
})
}
记住,在我在这个函数中写任何其他东西之前,我做了一个 python 脚本,它需要在继续这个程序之前完成运行,并且这个函数本身正在被调用:
client.on('messageReactionAdd', (reaction, user) => {
let { name } = reaction.emoji;
let member = reaction.message.guild.members.cache.get(user.id);
if (reaction.message.id === playID && user.tag !== 'freerice#4898') {
reaction.message.reactions.removeAll().catch(error => console.error('Failed to clear reactions: ', error));
selectCategory(name, reaction.message);
}
});