user15050992
user15050992

Reputation:

I am making Discord bot that sends a message at an interval that is triggered by a particular message but its not working?

I am making a bot that sends a message at an interval that is triggered when a user sends a message [$here] in a particular channel it automatically gets the channel id and sends a message only in that channel.

But what's happening is it sends the message one time and after that, I get this error:

bumpChannel.send('!d bump');
                    ^
TypeError: Cannot read property 'send' of undefined

This is my code:

const { time } = require('console');
const Discord = require('discord.js');
const { send } = require('process');
const client = new Discord.Client();

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);

    setInterval(interval, 10000);

    client.user.setActivity('With javascript');

    client.guilds.cache.forEach((guild) => {
        console.log(guild.name);

        guild.channels.cache.forEach((channel) => {
            console.log(` -${channel.name} ${channel.type} ${channel.id}`);
        });

        // General channel id:- 800353026904031257
    });

    let generalChannel = client.channels.cache.get('800353026904031257');

    generalChannel.send('Hello world');

    client.on('message', (receivedMessage) => {
        if (receivedMessage.content.startsWith('$')) {
            let bumpId = `${receivedMessage.channel.id}`;

            let bumpChannel = client.channels.cache.get(bumpId);
            processCommand(receivedMessage, bumpChannel);
        } else if (receivedMessage.author.id == client.user.id && receivedMessage.content == '!d bump') {
            let bumpId = `${receivedMessage.channel.id}`;

            let bumpChannel = client.channels.cache.get(bumpId);

            setInterval(interval, bumpChannel, 10000);
        }
    });
});

function processCommand(receivedMessage, bumpChannel) {
    let fullCommand = receivedMessage.content.substr(1);
    let splitCommand = fullCommand.split(' ');
    let primaryCommand = splitCommand[0];
    let arguments = splitCommand.splice(1);

    if (primaryCommand == 'here') {
        helpCommand(arguments, receivedMessage, bumpChannel);
    } else {
        receivedMessage.channel.send('I am not sure what are you talking about. Try `!here` to auto bump your server');
    }
}

function helpCommand(arguments, receivedMessage, bumpChannel) {
    if (arguments.length > 0) {
        receivedMessage.channel.send('I am not sure what are you talking about. Try `!here` to auto bump your server');
    } else {
        
        interval(bumpChannel);
    }
}

function interval(bumpChannel) {
   

    bumpChannel.send('!d bump');
}


client.login("[my bot token]")

Upvotes: 2

Views: 156

Answers (1)

sdrm
sdrm

Reputation: 305

setInterval(interval, 10000); calls the interval() function without any parameters, so bumpChannel inside interval() is undefined.

Also, setInterval(interval, bumpChannel, 10000), the milliseconds interval should be the second parameter, not the third.

Upvotes: 1

Related Questions