[英]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.