Am为一个不和谐的bot编码,如果在服务器中运行该bot可以正常工作,但是我想添加一些在DM内也可以运行的命令,我有一些为每个服务器设置前缀的代码,当我运行bot并尝试DM中的命令会产生错误,因为它正在寻找DM没有的公会ID,我该如何解决。
//the code from line index.js:63:30 in the error message
if(message.author.bot) return;
let prefixes = JSON.parse(fs.readFileSync("./prefixes.json", "utf8"));
if(!prefixes[message.guild.id]){
prefixes[message.guild.id] = {
prefixes: botconfig.prefix
};
}
我尝试使用if语句,但是当我在DM中运行命令时,该命令仍然不起作用,但不会产生任何错误。
bot.on("message", async message => {
if (message.channel.type === "dm") {
let prefix = "!";
} else (message.guild) {
let prefixes = JSON.parse(fs.readFileSync("./prefixes.json", "utf8"));
if(!prefixes[message.guild.id]){
prefixes[message.guild.id] = {
prefixes: botconfig.prefix
};
let prefix = prefixes[message.guild.id].prefixes;
if(!message.content.startsWith(prefix)) return;
let messageArray = message.content.split(" ");
let cmd = messageArray[0];
let args = messageArray.slice(1);
let commandfile = bot.commands.get(cmd.slice(prefix.length));
if(commandfile) commandfile.run(bot,message,args);
}
}
}
下面是错误的提示,该错误将我指向我的代码中的63行
at Client.bot.on (C:\Users\Milan\Desktop\episode-18-code\index.js:63:30)
at Client.emit (events.js:198:13)
at MessageCreateHandler.handle (C:\Users\Milan\Desktop\episode-18-code\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
at WebSocketPacketManager.handle (C:\Users\Milan\Desktop\episode-18-code\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:105:65)
at WebSocketConnection.onPacket (C:\Users\Milan\Desktop\episode-18-code\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
at WebSocketConnection.onMessage (C:\Users\Milan\Desktop\episode-18-code\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
at WebSocket.onMessage (C:\Users\Milan\Desktop\episode-18-code\node_modules\ws\lib\event-target.js:120:16)
at WebSocket.emit (events.js:198:13)
at Receiver.receiverOnMessage (C:\Users\Milan\Desktop\episode-18-code\node_modules\ws\lib\websocket.js:789:20)
at Receiver.emit (events.js:198:13)
(node:2912) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:2912) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.```
答案 0 :(得分:2)
You can run certain things based on what type of channel it is.
if (message.channel.type === 'text') {
// Server text channel commands
}
if (message.channel.type === 'dm') {
// DM channel commands
}
All types of channels, according to the docs
dm
direct message channelgroup
group DM channeltext
server text channelvoice
server voice channelcategory
server category channelnews
server news channelstore
server store channel答案 1 :(得分:0)
您可以使用
if(message.channel.type === 'dm') {
let prefix = 'bot main prefix'
}
else if(message.channel.type === 'guild') {
let prefixes = JSON.parse(fs.readFileSync("./prefixes.json", "utf8"));
}