Search code examples
javascriptdiscorderis

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


When I require code and load it into my main code for my Discord bot, it's unable to find variables or sub parts of them even if they're defined in the main code.

Required code:

module.exports.stupid = () => {

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

}

-- Code in main script

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()

Solution

  • You'll have to edit a few lines in order to make everything work as intended :

    1. Replace your module.exports.stupid = () => { with module.exports.stupid = (client) => {
    2. When calling the function (cm1.stupid()), you need to pass the bot variable as an argument like so: cm1.stupid(client)

    Hope this helps !