制作Discord.js机器人时发生错误“错误:找不到模块'随机'”

时间:2020-07-30 16:54:37

标签: javascript node.js discord discord.js

请帮助解决以下问题。

我正在制造Discord.js机器人,但是当我使用“ node”启动机器人时。它给出以下错误:

internal/modules/cjs/loader.js:1068
  throw err;
  ^

Error: Cannot find module 'random'
Require stack:
- D:\quote\index.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1065:15)
    at Function.Module._load (internal/modules/cjs/loader.js:911:27)
    at Module.require (internal/modules/cjs/loader.js:1125:19)
    at require (internal/modules/cjs/helpers.js:75:18)
    at Object.<anonymous> (D:\quote\index.js:2:16)
    at Module._compile (internal/modules/cjs/loader.js:1236:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1257:10)
    at Module.load (internal/modules/cjs/loader.js:1085:32)
    at Function.Module._load (internal/modules/cjs/loader.js:950:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ 'D:\\quote\\index.js' ]
}

我的代码:

const Discord = require('discord.js');
const random = require('random');

const bot = new Discord.Client();

var stats = {};

bot.on('message' , (message) => {
    if (message.guild.id in stats === false) {
        stats[message.guild.id] = {};
    }

    const guildStats = stats[message.guild.id];
    if (message.author.id in guildStats === false) {
        guildStats[message.author.id] = {
            xp: 0,
            level: 0,
            last_message: 0
        };
    }

    const userStats = guildStats[message.author.id];
    userStats.xp == random.int(15, 25);

    const xpToNextLevel = 5 * Math.pow(userStats.level, 2) - 50 * userStats.level + 100;

    console.log(message.author.username + 'now has' + userStats.xp);
    console.log(xpToNextLevel = ' XP needed for next level.');

const parts = message.content.split(' ');

if(message.content === '!hello') {
    message.reply('hi');
 }
});

bot.login('REMOVED');

1 个答案:

答案 0 :(得分:0)

您正在尝试加载尚未安装的模块。尝试在控制台的同一目录中运行npm i random