即使已经定义了变量,也无法通过所需代码找到它们

Variables are unable to be found by required code even if they're defined already

当我需要代码并将其加载到我的 Discord 机器人的主代码中时,它无法找到变量或它们的子部分,即使它们是在主代码中定义的。

所需代码:

module.exports.stupid = () => {

bot.on('messageCreate', (msg) => {
    if (msg.content === 'yes'){
        bot.createMessage(msg.channel.id,'yes!!')
    }
});

}

-- 主脚本中的代码

const Eris = require('eris');
const axios = require('axios');
const firebase = require('firebase/app');
const FieldValue = require('firebase-admin').firestore.FieldValue
const admin = require('firebase-admin');
const serviceAccount = require('./serviceAccount.json');

// Commands
const cm1 = require('./staff.js')

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
})

let db = admin.firestore()

const bot = new Eris('token_removed');
let prefix = ';'
const sprefix = ">"
const botname = "bee"
const version = '0.6.6'
const emoji = '<:bee_logo:730125457638425039>'
const semoji = '<:bshield:729463155587022860>'
const remoji = 'buzz:683498511341191245'

const bee_check = '<:bee_check:729878268811018300>'
const bee_dash = '<:bee_dash:729878268790177862>'
const bee_x = '<:bee_x:729878268848898159>'
const hex = 0xF3DC3E
const gethex = 'F3DC3E'
const server = "Amazon (Linux/UNIX)"
const today = new Date();
const time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();

cm1.stupid()

您必须编辑几行才能使一切按预期工作:

  1. 将您的 module.exports.stupid = () => { 替换为 module.exports.stupid = (client) => {
  2. 调用函数(cm1.stupid())时,需要将bot变量作为参数传递,如下所示:cm1.stupid(client)

希望对您有所帮助!