Search code examples
node.jsdiscord.js

Send message to channel in a function


I have an event handler for the ready event. ready.js:

const { Events } = require('discord.js');
const db = require("../config/database");
const itemdb = require("../config/itemdb");
const items = require("../models/items");
const AHItems = require('../models/ahitems.js');
const RSS = require('../models/regionserversettings.js');

module.exports = {
    name: Events.ClientReady,
    once: true,
    execute(client) {
        console.log(`Ready! Logged in as ${client.user.tag}`);
        db.authenticate()
            .then(() => {
                console.log('Logged in to DB!');
                AHItems.init(db);
                AHItems.sync();
                RSS.init(db);
                RSS.sync();
            })
            .catch(err => console.log(err));
        itemdb.authenticate()
            .then(() => {
                console.log('Logged in to Item DB!');
                items.init(itemdb);
                items.sync();
            })
            .catch(err => console.log(err));

    },
};

From inside the execute block I can use client.channels.cache.get('xxxxxx').send('Hello');

I want to use the send method in another File:

const AHItems = require("../models/ahitems");
const RSS = require("../models/regionserversettings");
const getprice = require("../api/getcurrentPrice");
const client = require("../events/ready");
const pricealarm = async function()
{
    let ahitems = await AHItems.findAll({attributes: ['guildID', 'itemID']});
    for (let i = 0; i < ahitems.length; i++)  {
        const guild = ahitems[i].dataValues.guildID;
        const RSSData = await RSS.findOne({where: {guildID: guild}});
        const item = ahitems[i].dataValues.itemID;
        const access_token = RSSData.AccessToken;
        const server = RSSData.serverID;
        const price = await getprice(access_token, item, server);
        const channel = client.channels.cache.get('x').send('test');
        console.log(channel);
    }
}
module.exports = pricealarm;

But if I try to do this, it tells me 'Unresolved function or method send()' I think I am requiring the wrong file, but am unsure, which one I have to require


Solution

  • The issue with your code is that you are trying to use the send() method from an object client that has not been properly instantiated in the file where you want to use it. In your ready.js file, you correctly initialize the client object and can use its send() method inside the execute block.

    However, in the other file where you want to use the send() method, you import the ready.js file, but you are only importing the module, not the instantiated client object. Therefore, the send() method is unresolved and cannot be called.

    To fix this issue, you need to modify the ready.js file to export the client object in addition to the module.exports statement. For example, you can add the following line at the end of the execute block:

    module.exports.client = client;
    

    Then, in your other file, you can import the client object by requiring the ready.js file and accessing the client property of the exported module. For example:

    const ready = require("../events/ready");
    const client = ready.client;
    
    // Now you can use client.channels.cache.get('xxxxxx').send('Hello');
    

    With these modifications, you should be able to properly use the send() method from the client object in both files.