javascript - 如何修复机器人在 DM 中请求 id,JavaScript 中的 discord bot

标签 javascript bots discord.js prefix

我正在编写一个 discord 机器人,如果在服务器内部运行该机器人工作正常,但我想添加一些也可以在 DM 内部工作的命令,我有一些代码为每个服务器设置前缀,当我运行该机器人并尝试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.```

最佳答案

您可以根据 channel 类型运行某些内容。

if (message.channel.type === 'text') {
  // Server text channel commands
}

if (message.channel.type === 'dm') {
  // DM channel commands
}

所有类型的 channel ,根据docs

  • dm 直接消息 channel
  • group 群组私信 channel
  • text 服务器文本 channel
  • voice 服务器语音 channel
  • category 服务器类别 channel
  • news 服务器新闻 channel
  • store 服务器商店 channel

关于javascript - 如何修复机器人在 DM 中请求 id,JavaScript 中的 discord bot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56394155/

相关文章:

javascript - Meteor + React 中的错误消息 "state is not defined"。

python - 每 30 分钟自动重启一个 .py

c# - 多次登录失败后阻止请求

discord.js - 如何在 discord.js 富嵌入中使用本地镜像?

javascript - Discord 随机图像机器人垃圾邮件

javascript - 在条件 for 循环中渲染 React

javascript - jQuery 在 .find() 之后使用选择器

javascript - TypeError 应用带有上下文的预编译 Handlebars 模板

javascript - 用 JavaScript 制作一个 Discord 机器人 : Attempting to make a game of wordchain between two players

node.js - NodeJS 调试在 127.0.0.1 :42457 failed: address already in use error 上启动检查器