javascript - 为什么我的不和谐机器人无法正常上线?

标签 javascript node.js discord

所以基本上,我已经尝试了几个小时来解决这个问题,但没有成功。这是我尝试启动机器人时发生的情况:

https://gyazo.com/3a941b4372c88be07c76c5b004327f71

我已经检查了 main 是否正确,并且一切都正确,例如 json 文件和实际代码。

bot.js ->

const botconfig = require("./botconfig.json");
const Discord = require("discord.js");

const bot = new Discord.Client({disableEveryone: true});

bot.on("ready", async () => {
  console.log(`${bot.user.username} is online!`);

  bot.user.setActivity("Tickets", {type: "WATCHING"});

});

bot.on('message', async message => {
  if(message.author.bot) return;
  if(message.channel.type === "dm") return;

  let prefix = botconfig.prefix;
  let messageArray = message.content.split(" ");
  let cmd = messageArray[0];
  let args = messageArray.slice(1);

  if(cmd === `${prefix}new`){

  let embed = new Discord.RichEmbed()
  .setTitle("Ticket created")
  .setDescription("Ticket #ticket- has been created successfully.")
  .setColor("#15f153")
  .setFooter("chefren#9582 | 2019")


    return message.channel.send(embed);

}


    if(cmd === `${prefix}ticketcreation`){

    let embed = new Discord.RichEmbed()
    .setTitle("Ticket")
    .setDescription("Do you require help or support? React with :white_check_mark: ")
    .setColor("#15f153")
    .setFooter("dl121#9582 | 2019")

      msg = await message.channel.send(embed);

      await msg.react('✅')

      message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
    .then(collected => {
        const reaction = collected.first();

        if (reaction.emoji.name === '✅') {
            message.reply('you reacted with a thumbs up.');
        }


});

bot.login(botconfig.token);
    }
  })



包.json

{
  "name": "bot",
  "version": "1.0.0",
  "description": "A bot",
  "main": "bot.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "andrew",
  "license": "ISC",
  "dependencies": {
    "cjs": "0.0.11",
    "discord.js": "^11.5.1",
    "fs": "0.0.1-security",
    "node.js": "0.0.0"
  },
  "devDependencies": {}
}



我之前修复过此错误消息:

Error: Cannot find module 'C:\Users\Andrew\Desktop\Discord Bots\Bot\bot,js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

最佳答案

根据您提供的代码,您的花括号似乎有点问题。 bot.login({...}) 调用实际上是在 bot.on('message' 处理程序内部。显然,如果不先登录,您将不会收到消息,所以这可能就是发生的情况。

我重新缩进了您的代码,并添加了必要的大括号和括号,以使 bot.login 调用在脚本末尾、任何处理程序函数之外运行。

const botconfig = require("./botconfig.json");
const Discord = require("discord.js");

const bot = new Discord.Client({disableEveryone: true});

bot.on("ready", async () => {
  console.log(`${bot.user.username} is online!`);

  bot.user.setActivity("Tickets", {type: "WATCHING"});

});

bot.on('message', async message => {
  if(message.author.bot) return;
  if(message.channel.type === "dm") return;

  let prefix = botconfig.prefix;
  let messageArray = message.content.split(" ");
  let cmd = messageArray[0];
  let args = messageArray.slice(1);

  if (cmd === `${prefix}new`) {

    let embed = new Discord.RichEmbed()
    .setTitle("Ticket created")
    .setDescription("Ticket #ticket- has been created successfully.")
    .setColor("#15f153")
    .setFooter("chefren#9582 | 2019")

    return message.channel.send(embed);

  }


  if (cmd === `${prefix}ticketcreation`) {

    let embed = new Discord.RichEmbed()
    .setTitle("Ticket")
    .setDescription("Do you require help or support? React with :white_check_mark: ")
    .setColor("#15f153")
    .setFooter("dl121#9582 | 2019")

    let msg = await message.channel.send(embed);

    await msg.react('✅')

    message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })'
    .then(collected => {
      const reaction = collected.first();

      if (reaction.emoji.name === '✅') {
        message.reply('you reacted with a thumbs up.');
      }
    });
  }
});

// *** CHANGES WERE MADE ABOVE HERE ***

bot.login(botconfig.token);

关于javascript - 为什么我的不和谐机器人无法正常上线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56960476/

相关文章:

node.js - 通过 Node js 应用程序访问 ejabberd ReST API

javascript - 这对 require 语句做了什么?

javascript - NodeJS - 如何创建一个在命令之后运行的命令?

python - 尝试运行命令 'ctx is a required argument that is missing' 时收到错误。 [Python, discord.py]

javascript - react Hook : what is best practice for wrapping multiple instances of a hook with a single hook?

javascript - 使用 FormData 上传 AJAX 图片不在服务器端显示图片

node.js - Firebase存储使用云功能仅svg上传不起作用其他格式可以使用base64

javascript - 未找到不和谐 FFmpeg

javascript - Jquery:获取 html 元素的 id:为什么一种解决方案有效而另一种则无效

javascript - jQuery - 隐藏一个特定的 div 直到另一个被点击,当 div 悬停时再次隐藏那个 div