正如标题所示,我正在按照 YT 上的播放列表为我的 Discord 服务器制作一个货币机器人 当我输入命令时,它显示机器人正在思考但没有执行任何操作,并且终端中会弹出以下错误。
PS C:\Discord Bot> node .
Ready! Logged in as Money Bot#6039
Connected to the database!
Error executing give
Error [InteractionAlreadyReplied]: The reply to this interaction has already been sent or deferred.
at ChatInputCommandInteraction.deferReply (C:\Discord Bot\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:67:46)
at Object.execute (C:\Discord Bot\commands\give.js:48:25)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Object.execute (C:\Discord Bot\events\interactionCreate.js:33:7) {
code: 'InteractionAlreadyReplied'
}
我试图允许我的服务器的用户以服务器货币进行交易,这是 /give 命令的代码
const { SlashCommandBuilder } = require("discord.js");
const profileModel = require("../Models/profileSchema");
module.exports = {
data: new SlashCommandBuilder()
.setName("give")
.setDescription("give coins to another user")
.addUserOption((option) =>
option
.setName("user")
.setDescription("The user you want to give to")
.setRequired(true)
)
.addIntegerOption((option) =>
option
.setName("amount")
.setDescription("The amount of coins you want to give")
.setRequired(true)
.setMinValue(1)
),
async execute(interaction, profileData) {
const receiveUser = interaction.options.getUser("user");
const giveAmt = interaction.options.getInteger("amount");
const { balance } = profileData;
if (balance < giveAmt) {
await interaction.deferReply({ ephemeral: true });
return await interaction.editReply(
`You do not have ${giveAmt} coins in your balance`
);
}
await interaction.deferReply();
const receiveUserData = await profileModel.findOneAndUpdate(
{
userId: receiveUser.id,
},
{
$inc: {
balance: giveAmt,
},
}
);
if (!receiveUserData) {
await interaction.deferReply({ ephemeral: true });
return await interaction.editReply(
`${receiveUser.username} is not in the currency system`
);
}
await interaction.deferReply();
await profileModel.findOneAndUpdate(
{
userId: interaction.user.id,
},
{
$inc: {
balance: -giveAmt,
},
}
);
interaction.editReply(
`You have donated ${giveAmt} coins to ${receiveUser.username}`
);
},
};
当您拨打
interaction.deferReply
时,您正在响应交互,该交互只能进行一次。推迟回复会导致命令的用户按预期看到“机器人正在思考...”。机器人需要调用 interaction.followUp
才能用用户请求的内容更新“机器人正在思考...”消息。否则它就永远停留在“思考”的状态。