在要求Discord的子文件中包含bot时遇到麻烦

时间:2018-07-06 17:48:18

标签: node.js discord discord.js

我在/ cmd /中获取文件以从index.js读取正确的函数时遇到问题。我知道我需要在index.js中以某种方式使用module.exports,但是我不确定它的语法。本质上,我希望能够使用在profile.js中发送命令的用户的化身和用户名,但是我需要为此使用Discord.Client,但我不确定如何这样做。预先感谢。

未捕获的承诺错误: TypeError:无法读取未定义的属性“ msg”     在sql.get.then.row(/home/kosaki/kosaki/cmd/profile.js:13:18)     

index.js

const chalk = require("chalk");
const config = require("./config.json")
const Discord = require("discord.js");
const express = require("express");
const cheerio = require("cheerio")
const request = require("request")
const sql = require("sqlite");
    sql.open("./scores.sqlite");


const fs = require('fs');
const path = require('path');

// Create saki.
const saki = new Discord.Client();
if (!fs.existsSync(path.join(__dirname, 'config.json'))) {
    saki.err(t + "config.json not found!")
    process.exit()
}

saki.on('ready', () => {
    saki.config = config
    saki.log("Loading modules...")
    saki.commandInit()
    //saki.commandInit()
    saki.user.setAFK(true)
    saki.log(`Logged in as ${saki.user.tag}!`);
});

saki.on('message', (msg) => {

    // Kosaki scores. --------------------------------------------- //

    sql.get(`SELECT * FROM scores WHERE userId ="${msg.author.id}"`).then(row => {
        if (!row) {
            sql.run("INSERT INTO scores (userId, points, level) VALUES (?, ?, ?)", [msg.author.id, 1, 0]);
        } else {
            sql.run(`UPDATE scores SET points = ${row.points + 1} WHERE userId = ${msg.author.id}`);
        }
    }).catch(() => {
        console.error;
        sql.run("CREATE TABLE IF NOT EXISTS scores (userId TEXT, points INTEGER, level INTEGER)").then(() => {
            sql.run("INSERT INTO scores (userId, points, level) VALUES (?, ?, ?)", [msg.author.id, 1, 0]);
        });
    });

    // Kosaki scores. --------------------------------------------- //

    // Ignore if the message doesn't start with our prefix
    if (!msg.content.startsWith(config.prefix)) return

    // Ignore if empty command
    if (msg.content.length === config.prefix.length) return

    // Get all the arguments
    let tmp = msg.content.substring(config.prefix.length, msg.length).split(' ')
    let args = []

    for (let i = 1; i < tmp.length; i++) {
        args.push(tmp[i])
    }

    // Store the command separately
    let cmd = tmp[0]

    if (saki.modules.hasOwnProperty(cmd)) return saki.modules[cmd].run(msg, args)
    //if (config.commandError.sendToModule === true) {
    //return saki.modules[config.commandError.module][config.commandError.function](msg, cmd)
    //}

    return msg.delete()
})

saki.on('disconnect', () => {
    saki.err('Client Kosaki disconnected!')
    process.exit()
})

saki.on('reconnecting', () => {
    saki.log('Client Kosaki reconnecting...', 'done.')
})

saki.log = function(msg) {
    console.log(chalk.green(`[saki] `) + `${msg}`)
}

saki.err = function(msg) {
    console.log(chalk.red(`[saki] `) + `${msg}`)
}

saki.commandInit = function() {
    saki.modules = {}

    // Load up all the modules
    fs.readdirSync('./cmd/').forEach((file) => {
        let name = file.slice(0, -3)

        delete require.cache[require.resolve(`./cmd/${file}`)]

        try {
            saki.modules[name] = require(`./cmd/${file}`)
            if (saki.modules[name].hasOwnProperty('init')) {
                saki.modules[name].init(saki)
            }

            saki.log(`Module '${name}' is ready.`)
        } catch (e) {
            saki.err(`Error in module '${name}':\n${e.stack}`)
        }
    })
}

saki.edit = function(msg, content, timeout = 5000) {
    if (timeout === 0) return msg.edit(content).catch(console.error)

    return msg.edit(content).then(() => {
        setTimeout(() => msg.delete().catch(console.error), timeout)
    })
}

saki.log("Kosaki initialized. Created by vex.")

saki.login(config.token);

process.on('unhandledRejection', err => {
    saki.err(`Uncaught Promise Error:\n${err.stack}`)
})

cmd / profile.js

module.exports.desc = "Shows your profile on the Kosaki leveling system.";

const sql = require("sqlite")
const kosaki = require("./../index.js")

exports.run = function(msg) {

sql.get(`SELECT * FROM scores WHERE userId ="${msg.author.id}"`).then(row => {

msg.channel.send({embed: {
    color: 3447003,
    author: {
      name: kosaki.saki.msg.user.username,
      icon_url: kosaki.saki.msg.user.avatarURL
    },
    title: "Kosaki Profile",
    description: "Your profile on Kosaki.",
    fields: [{
        name: "Points",
        value: `${row.points}`
      },
      {
        name: "Level",
        value: `${row.level}`
      },
      {
        name: "Markdown",
        value: "You can put all the *usual* **__Markdown__** inside of them."
      }
    ],
    timestamp: new Date(),
    footer: {
      icon_url: kosaki.saki.msg.user.avatarURL,
      text: "© Example"
    }
  }
});

});

}

1 个答案:

答案 0 :(得分:1)

我建议您不要使用module.exports,因为当您需要index.js时,请再次运行它。

您可以设置全局变量,以便在index.js需要profile.js文件时,它可以使用先前在第一个文件中声明的所有变量。为此,必须将要使用的变量存储在global对象中:例如,如果要与其他文件共享客户端,请设置global.your_var_name = client。然后,在另一个文件中,您必须像在文件中声明的那样键入your_var_name

//index.js
global.client = saki;

//cmd/profile.js
client.blablabla...

我使用这种方法已经有一段时间了,我发现它非常有用,即使有人会告诉您您不应该这样做,并且还有更多“正确”的方法可以做到这一点。

希望这对您有所帮助,如果您还有其他疑问,请告诉我!