2

I want to make that discord bot will send message interval for ex. in 10 minutes but it throws error! Please help me! (sorry for my english it is not perfect)

Here is code:

const Commando = require('discord.js-commando');
const bot = new Commando.Client({commandPrefix: '$'});
const TOKEN = 'here is token';
const MIN_INTERVAL = 100;

bot.registry.registerGroup('connectc', 'Connectc');
bot.registry.registerGroup('defaultc', 'Defaultc');
bot.registry.registerDefaults();
bot.registry.registerCommandsIn(__dirname + "/commands")

bot.on('ready', function(){
    console.log("Ready");
});
setInterval(function(){
    var generalChannel = bot.channels.get("542082004821213197"); // Replace with known channel ID
    generalChannel.send("Hello, world!") ;
}, MIN_INTERVAL);

bot.login(TOKEN);

And it throws this error

PS C:\Users\User\Documents\Visual Studio Code\Discord Bots\VblacqeBot> node .
C:\Users\User\Documents\Visual Studio Code\Discord Bots\VblacqeBot\index.js:18
    generalChannel.send("Eldo!") ;
                   ^

TypeError: Cannot read property 'send' of undefined
    at eldo (C:\Users\User\Documents\Visual Studio Code\Discord Bots\VblacqeBot\index.js:18:20)
    at Object.<anonymous> (C:\Users\User\Documents\Visual Studio Code\Discord Bots\VblacqeBot\index.js:28:13)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
PS C:\Users\User\Documents\Visual Studio Code\Discord Bots\VblacqeBot> 
Xan
  • 74,770
  • 16
  • 179
  • 206
Vblacqe
  • 35
  • 1
  • 2
  • 5

2 Answers2

1

You are probably trying to do that before the bot is ready - because the interval is actually in milliseconds.

  1. Make the interval correct:

    const MIN_INTERVAL = 10 * 60 * 1000;
    // 10 minutes, 60 seconds in a minute, 1000 milliseconds in a second
    
  2. Make sure to start your interval only after the bot is ready:

    bot.on('ready', function(){
      console.log("Ready");
      setInterval(/* ... */);
    });
    

As an aside, you should use interval methods defined on the Client - they guarantee that they are cancelled if the client is destroyed:

bot.setInterval(/*...*/);

See https://discord.js.org/#/docs/main/master/class/BaseClient - Commando's client extends that.

Xan
  • 74,770
  • 16
  • 179
  • 206
-1

It depends on how you do it. One of my bots, for example, sends a message every 4 hours to a specific channel, I just use an async loop function:

async function notifLoop(){
  while(true){
    client.channels.get(/*channelid*/).send("This message is sent every ten minutes");
    await Sleep(600000)
  }
}

Using this sleep function:

function Sleep(milliseconds) {
    return new Promise(resolve => setTimeout(resolve, milliseconds));
}

This loop automatically starts when my bot is ready, working like this:

bot.on('ready', function(){
    notifLoop();
    console.log("Ready");
});

Now it depends on how you host it, it could happen that the server restarts every 24 hours for example (like heroku), then you would need to add a timestamp. You could for example store that in your config.json (if you use one)

Zer0
  • 1,580
  • 10
  • 28
  • If you don't await your Promise, it won't pause. Also, the way you set it up there's no way to stop it short for killing the Node process. – Xan Sep 24 '19 at 15:55
  • It gives error. I only change time to try this in 5 seconds and client to bot Here is error: https://pastebin.com/mwNeWsfB – Vblacqe Sep 25 '19 at 07:44