【问题标题】:How to fix the bot from asking for id in DM , discord bot in JavaScript如何修复机器人在 DM 中询问 id,在 JavaScript 中不和谐机器人
【发布时间】:2019-10-17 01:09:58
【问题描述】:

我正在编写一个不和谐的机器人,如果在服务器内部运行,机器人工作正常,但我想添加一些也可以在 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.```

【问题讨论】:

    标签: javascript bots discord.js prefix


    【解决方案1】:

    你可以根据它是什么类型的频道来运行某些东西。

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

    所有类型的频道,根据docs

    • dm私信频道
    • group群DM频道
    • text服务器文字频道
    • voice服务器语音通道
    • category服务器分类频道
    • news服务器新闻频道
    • store服务器存储频道

    【讨论】:

    • 您也可以使用与 DM 中的服务器前缀相同的内容,只要确保命令兼容即可
    • 我用过,现在没有报错,但是 DM 中的命令不起作用
    • @Ayodele,这是因为“命令”是!command-name 而不是command-name,你应该这样做const args = message.content.trim().slice(prefix.length).split(/\s+/g); const command = args.shift()
    • 与您的示例相同,使用commandfile.run(client, message, args)(在您的情况下客户端为bot)
    【解决方案2】:

    你可以使用

    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"));
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-21
      • 2018-03-20
      • 2021-03-29
      • 1970-01-01
      • 2022-11-10
      • 2021-02-01
      • 1970-01-01
      • 2021-03-11
      • 2020-09-23
      相关资源
      最近更新 更多