【问题标题】:My bot can't take errors when failling to run a "join" voice channel command未能运行“加入”语音通道命令时,我的机器人不会出错
【发布时间】:2021-09-27 05:16:28
【问题描述】:

我目前正在编写一个discord.js 机器人,我做了这个join 命令,以便机器人可以加入我的语音频道

module.exports.run = async (client, message, args) => {

let membervc = message.member.voice.channel;
let botvc = message.guild.me.voice.channel;
if(!membervc) return message.send('You need to join a voice channel first.');
if(botvc){
    if(!(botvc.members.size - 1)) return message.reply(`I am already on ${botvc}`);
    if(membervc.id == botvc.id) return message.reply('We are already on the same voice channel.');
};
membervc.join();
}

问题是我不知道如何制作它,以便如果最后一个函数出错或根本不起作用它可以向用户发送错误消息,例如 @User, I could not join the channel, "THE ERROR" :/ 我不希望我的机器人因为一个小细节而崩溃并不得不再次运行它。有没有办法解决它?这对我有很大帮助!提前致谢!

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    我不会让用户知道这个错误,因为这可能会让他们感到困惑,但你可以通过try catch 来检查它是否通过,如果没有发送消息。

    try {
          membervc.join();
    } catch(error){
          console.log(error);
          message.reply(`Something went wrong while joining voice channel`);
    }
    

    【讨论】:

    • 别担心,只有我和一些使用机器人的程序员,所以直接给出错误是一件好事!也非常感谢你,它的工作!
    • 记得在任何承诺(例如membervc.join()message.channel.send())之前添加await,以避免任何未捕获的承诺拒绝(这将不会被@987654326 捕获@)。
    猜你喜欢
    • 2021-12-06
    • 2020-07-16
    • 2023-02-25
    • 1970-01-01
    • 2021-05-03
    • 2020-12-30
    • 2021-05-10
    • 2019-01-03
    • 2021-07-13
    相关资源
    最近更新 更多