所以我不知道是否需要为此提供代码,所以我不会。
基本上我和一个朋友正在试验我的机器人。有一次,我阻止了它,并要求我的朋友使用一个命令,让机器人向我发送消息。他们这样做了,机器人崩溃了。我绝对认为人们会阻止这个机器人;它可能会很烦人,那么当它尝试向已阻止它的用户发送 DM 时,如何防止它崩溃?
一个简单的 try catch 很可能就足够了。
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
} else {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
}
查看discord.js 文档,了解如何防止此类事情发生。