SomePerson
SomePerson

Reputation: 1309

When bot is restarted or turned on, do a specific function

I'm trying to make it so that when my bot gets a restart, in the ready.js file it will search for webhooks it made in all of the guilds the bot is in, the bot will then use that webhook and send messages through it. I wasn't able to get anywhere and this is in JS.

I've referred to the documentation for Discord.JS and really haven't gotten anywhere. I tried to get the client ID from webhook.owner and see if the bot's ID matches up with it. I am not sure how to extract the client ID from webhook.owner

guild.fetchWebhooks()
    if(webhook.owner == `${bot.user.id}`);

(suggested)

guild.fetchWebhooks()
    if(webhook.owner == `${bot.user.username}`);

(actual) This is the only code I could come up with, can add full file if needed.

I expect a bot that when it restarts (bot.on) then it will search through all of the guilds it is in and find webhooks that it owns, and send messages through it without doing it to another active webhook.

What ends up happening is that my bot just sends out a mention of the bot.

Upvotes: 0

Views: 182

Answers (1)

PLASMA chicken
PLASMA chicken

Reputation: 2785

Do something like this:

 bot.on('ready', async () => {                          // on ready
    await bot.guilds.forEach(async guild => {           // in all guilds:
        const webhooks = await guild.fetchWebhooks();   // check for Webhooks
        await webhooks.forEach(async webhook => {       // for all found Webhooks:
            if (webhook.owner.id == bot.user.id) {      // check if bot owns them
                webhook.send('test');                   // Do something with the Webhook example
            }
        });
    });
});

Upvotes: 3

Related Questions