[英]discord.js try catch is not working and giving me an error
您好,我目前正在研究 discord 机器人,并且我的 unban 命令不断出现错误。
当用户被禁止并且我运行 unban 命令时,它可以正常工作。 当用户没有被禁止并且我运行 unban 命令时,它会给我一个错误,说DiscordAPIError[10026]: Unknown Ban
。 我希望这是因为用户没有被禁止。 但是,我将它放在 try catch 中,并且该 catch 似乎从未被使用过。
这是命令代码:
const { Console } = require('console');
const fs = require('fs');
const { EmbedBuilder } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('unban')
.setDescription('Unbans the spesified user.')
.addUserOption(option => option
.setName('target')
.setDescription('The user to unban.')
.setRequired(true)),
async execute(interaction) {
is_admin = false;
var server_data_path = "./data/" + interaction.guild.id + ".json";
if (fs.existsSync(server_data_path)) {
let rawdata = fs.readFileSync(server_data_path);
let server_data = JSON.parse(rawdata);
let sender_id = interaction.user.id;
if(server_data.admin.includes(sender_id)){
is_admin = true;
}
}
if(is_admin){
let user = interaction.options.getUser('target').id;
try{
interaction.guild.members.unban(user);
const success_embed = new EmbedBuilder().setColor('00ff00').setTitle("Unbanned: " + user + ".");
interaction.reply({ embeds: [success_embed] }).then(() => setTimeout(() => interaction.deleteReply(), 5000));
}catch (error){
const error_embed = new EmbedBuilder().setColor('ff0000').setTitle("Could not unban the target.");
interaction.reply({ embeds: [error_embed] }).then(() => setTimeout(() => interaction.deleteReply(), 5000));
}
}else{
const error_embed = new EmbedBuilder().setColor('ff0000').setTitle("You must be an admin to use this command.");
interaction.reply({ embeds: [error_embed] }).then(() => setTimeout(() => interaction.deleteReply(), 5000));
}
},
};```
AFAI 异步函数不会捕获这样的错误。
the try catch.您必须改用.catch(error)
或try catch 中使用 await。
如果您的interaction.guild.members.unban
function 返回 promise,您必须等待它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.