[英]Discord.js, Cannot read properties of undefined (reading 'edit')
我正在為我的 Discord 機器人創建 SlashCommands,現在我正在使用按鈕並遇到“無法讀取未定義的屬性”錯誤
下面是 RPS 命令的代碼:
const {
MessageEmbed,
MessageActionRow,
MessageButton,
Message,
} = require("discord.js");
module.exports = {
data: new SlashCommandBuilder()
.setName("rps")
.setDescription("Rock Paper Scissors"),
async execute(interaction) {
let hand = [
{
txt: "Rock",
emoji: "✊",
index: 0,
},
{
txt: "Paper",
emoji: "✋",
index: 1,
},
{
txt: "Scissors",
emoji: "✌",
index: 2,
},
];
let botMove = hand[Math.floor(Math.random() * 3)];
const rpsMsg = await interaction.reply({
embeds: [
new MessageEmbed() // embed
.setColor("GREEN")
.setTitle("RockPaperScissors")
.setDescription("Choose a handsign!"),
/*.setImage("") // Provide the source of Image*/
],
components: [
// components
new MessageActionRow().addComponents(
new MessageButton() // making Rock button
.setCustomId("rps_rock") //set the custom id to the rock
.setLabel("✊ Rock")
.setStyle("PRIMARY"), // there are different styles of buttons you can use
new MessageButton() // making Paper button
.setCustomId("rps_paper") // set the custom id for Paper
.setLabel("✋ Paper")
.setStyle("PRIMARY"),
new MessageButton() // making Scissors button
.setCustomId("rps_scissors") // making Scissors button
.setLabel("✌ Scissors")
.setStyle("PRIMARY")
),
],
fetchyReply: true, //we will edit the message later
});
// define variables
let win = 0; // 0 is Loss, 1 is Tie, 2 is Win
let userMove;
const filter = (interaction) => !interaction.user.bot; // filter out bots
const collector = interaction.channel.createMessageComponentCollector({
// create a message component collector with some options below
filter, // apply the filter defined above
componentType: "BUTTON", // button collector
time: 10000, // im ms = 10 seconds
});
collector.on("collect", async (i) => {
if (!i.isButton()) return; //if collected is not button then return
if (i.customId.startsWith("rps")) {
await i.deferUpdate(); //deffering the interaction so it will not load so long
let move = i.customId.split("_")[1]; // split the button custom ID to 2 parts (it will split in the _), and define the hand sign which is rock, paper and scissors as the variable
userMove = hand.find((v) => v.txt.toLowerCase() == move); // find the object which name property equals to the move variable, which is rock, paper and scissors, from the hand array defined above
switch (
move // a switch statement
) {
case "rock":
win = botMove.index == 0 ? 1 : botMove.index == 1 ? 0 : 2;
break;
case "paper":
win = botMove.index == 0 ? 2 : botMove.index == 1 ? 1 : 0;
break;
case "scissors":
win = botMove.index == 0 ? 0 : botMove.index == 1 ? 2 : 1;
break;
}
let embed = rpsMsg.embeds[0]; // get the embed that sent before
// edit the embed
embed.color = "BLUE";
embed.description = `I choose ${botMove.txt}! ${
win == 0 ? "You lost!" : win == 1 ? "We tied!" : "You win!"
} (${userMove.emoji} ${win == 0 ? "<" : win == 1 ? "=" : ">"} ${
botMove.emoji
})`;
let components = rpsMsg.components; // get the components which are buttons that sent before
// Disabling all buttons
components[0].components.forEach((comp) => {
if (comp.customId == i.customId) {
comp.disabled = true; // disable the button
comp.style = "SECONDARY"; // change the style of the button, color is gray
} else comp.disabled = true;
});
// edit the message
await rpsMsg.edit({
embeds: [embed],
components: components,
fetchyReply: true,
});
collector.stop(); // stop the message component collector
}
});
},
};
但是,當我直接設置“嵌入”和組件的值時:
let embed = /*rpsMsg.embeds[0];*/ new MessageEmbed() // embed
.setColor("GREEN")
.setTitle("RockPaperScissors")
.setDescription("Choose a handsign!");
...
let components = /*rpsMsg.components;*/ [
new MessageActionRow().addComponents(
new MessageButton() // making Rock button
.setCustomId("rps_rock") //set the custom id to the rock
.setLabel("✊ Rock")
.setStyle("PRIMARY"), // there are different styles of buttons you can use
new MessageButton() // making Paper button
.setCustomId("rps_paper") // set the custom id for Paper
.setLabel("✋ Paper")
.setStyle("PRIMARY"),
new MessageButton() // making Scissors button
.setCustomId("rps_scissors") // making Scissors button
.setLabel("✌ Scissors")
.setStyle("PRIMARY")
),
];
問題似乎消失了,剩下的唯一問題是最后一種方法:
await rpsMsg.edit({
embeds: [embed],
components: components,
fetchyReply: true,
});
現在是“無法讀取未定義的屬性(讀取'編輯')”
我怎樣才能提供正確的訪問權限?
fetchyReply
需要是fetchReply
。
您的代碼:
await rpsMsg.edit({
embeds: [embed],
components: components,
fetchyReply: true,
});
編輯代碼:
await rpsMsg.edit({
embeds: [embed],
components: components,
fetchReply: true,
});
我希望它有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.