简体   繁体   English

当我们在Discord机器人上触发错误时,如何向用户回复消息?

[英]How do I respond with a message to a user when they trigger an error on my Discord bot?

I'm trying to make my Discord bot reply to a user letting them know that there was an error if one occurred when they ran a command. 我正在尝试让我的Discord bot回复给用户,让他们知道如果他们运行命令时发生了错误。 I'm unsure of how I'm supposed to get my message variable from the command message in my index file from the command. 我不确定我应该如何从命令中的索引文件中的命令消息中获取我的消息变量。

I've already tried using the message variable in a function that is running in my index.js file from the command file, but when it runs it says that 'message' is not defined. 我已经尝试在命令文件中的index.js文件中运行的函数中使用消息变量,但是当它运行时它表示没有定义'message'。 I suspect that it may be where I've put my .catch() . 我怀疑它可能是我放置我的.catch() Here is the code that I am using. 这是我正在使用的代码。

//This is the area in my index that handles errors
bot.on('error', (err, message) => {
  console.error();
  message.reply("error here, possibly a rich presence");
});

//Heres the function with the .catch()
http.request(options, function(res) {
    var body = '';

    res.on('data', function (chunk) {
        body+= chunk;
    });

    res.on('end', function() {
       var jsondata = JSON.parse(body);
       var converteddate = new Date(jsondata.toTimestamp*1000)
       console.log(converteddate)
       var hours = converteddate.getHours();
       var minutes = "0" + converteddate.getMinutes();
       var finishedtime = hours + ':' + minutes.substr(-2);
        message.reply(finishedtime + " EST.")
})
}).end()
.catch(err => bot.emit('error', err, message));
}

The expected output of this is to run the command and if there are any errors, notify the user of them. 预期的输出是运行命令,如果有任何错误,请通知用户。 Thanks for the help! 谢谢您的帮助!

Ok, so first you have a problem with your '}' (there's one that shouldn't be here at the end) 好的,首先你的'}'有问题(最后一个不应该在这里)

Then, a simple example how to catch: 然后,一个简单的例子如何捕获:

 function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } async function error() { await sleep(3000); // wait 5 sec for the sake of the example throw 'I have a problem'; } console.log('starting') error().catch((err) => console.log('The error:',err)) 

I'm not sure what is the .end() , but I think you don't need it 我不确定.end()是什么,但我认为你不需要它

So here, you want to do : 所以在这里,你想做:

http.request(options, function(){
    // do something
    return 'text message'; // what you want to reply
}).catch((err) => err).then((msg) => message.reply(msg))

You can access the message because in the http.request isn't close properly. 您可以访问该消息,因为http.request未正确关闭。 Make sure you run a linter and bveautify your code to easily spot extra or unclosed sections in your code. 确保运行linter并修复代码,以便在代码中轻松发现额外或未封闭的部分。

as for replying to the user you could simply do: 至于回复用户你可以简单地做:

message.author.send("There was an error: "+err);

I would try something like this: 我会尝试这样的事情:

http.request(options, (res) => {
    let body = '';
    res.on('data', (chunk) => {
        body+= chunk;
    });
    res.on('end', () => {
       const jsondata = JSON.parse(body);
       const converteddate = new Date(jsondata.toTimestamp*1000)
       console.log(converteddate)
       const hours = converteddate.getHours();
       const minutes = "0" + converteddate.getMinutes();
       const finishedtime = hours + ':' + minutes.substr(-2);
        message.reply(finishedtime + " EST.")
    })
}).catch(err => message.author.send("There was an error: "+err.toString()));

If you want to do a more global approach I would create a errorHandler and import it. 如果你想做一个更全局的方法,我会创建一个errorHandler并导入它。 However I do not see any particular reason to have a generic one since you do not do any specific actions with it. 但是我没有看到任何特殊原因要使用通用的,因为您没有对它进行任何特定操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让我的不和谐机器人自动回复消息? - How do I make my discord bot auto respond to a message? 当特定用户玩特定游戏时,如何让我的不和谐机器人发送消息? - How do I make my discord bot send a message when a specific user plays a specific game? 在为 Discord 服务器创建 BOT 时,如何“scanf()”和“printf()”discord.js 中的用户消息? - How do I "scanf()" and "printf()" a user's message in discord.js when creating a BOT for Discord server? 如何让我的不和谐机器人复制并发送每个用户写的消息? - How do I make my discord bot copy and message every user written message? 当提到特定用户时,如何让不和谐机器人发送消息? - How do i make a discord bot send a message when a specific user is mentioned? 当用户标记 discord 机器人时,我该如何做到这一点,它会回复一条消息? - How do I make it so when a user tags the discord bot, it responds with a message? 如何让我的 Discord 机器人发送欢迎消息? - How do I get my Discord bot to send a welcome message? 当我的 discord 机器人加入服务器时,如何发布欢迎消息 - How do I post a welcome message when my discord bot joins a server 如何让我的 Discord.JS 机器人在 DM 中回复我 - How do I have my Discord.JS bot respond to me in DMs 如何让我的 discord 机器人删除我的消息并对上面的消息作出反应? - How do I make my discord bot delete my message and react to the message above?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM