-1

I have code that should edit a discord message per each execution. Everything seems to be work fine, but when I add interval functionality(to execute it code every minute) it couldn't edit bot message anymore:

call:

setInterval(function () { var data = getData(response, list); editMessage(data) }, 60000)

function with message edit

function editMessage(data) {
    client.on('ready', () => {
        client.guilds.cache.get('server').channels.cache.get('channel').messages.fetch('message').then(message => {
            message.edit('new message content');
        }).catch(err => {
            console.error(err);
        });;
    });
}

So I can receive the data inside editMessage function, but anything that goes inside of client.on('ready', () => {... can't be engaged with interval function. What could be the case?

I'm using Discord.js 12 version.

RedEclipse
  • 123
  • 2
  • 11
  • 1
    Bad idea. Making an API call every minute is API abuse and will lead your account being suspended from using it – Elitezen Sep 30 '21 at 11:40
  • it will be adjusted later. Its my API so no one going to suspend for extra requests. – RedEclipse Sep 30 '21 at 11:42
  • If you're editing a *Discord* message it's *Discord*'s API. `.edit()` makes a POST request each time – Elitezen Sep 30 '21 at 11:42
  • Ah, thought you were saying about ```response```. No problem, it will be edited as things will be sorted out. Thanks for heads up though. – RedEclipse Sep 30 '21 at 11:45
  • ALso https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – mplungjan Sep 30 '21 at 11:46

1 Answers1

0

Just for the record, I made this work in a different approach. I've wrapped the setInterval function inside of client.on and it works as intended.

        client.on('ready', () => {
            client.guilds.cache.get('server').channels.cache.get('channel').messages.fetch('message').then(message => {
                setInterval(function () {
                message.edit('new message content');
                console.log('new message content')
            }, 1 * 30000);
            }).catch(err => {
                console.error(err);
            });;
        });
Staxlinou
  • 1,351
  • 1
  • 5
  • 20
RedEclipse
  • 123
  • 2
  • 11