Reputation: 35
I am trying to get my command to run slower, if that is the correct way to say it. :(
I think I have something, but its not working
What I am looking to do is wait for 1 second before each message is sent, as I have to send the message to over 500 guilds but don't want to be rate limited.
let i = 0;
data.message.client.guilds.cache.forEach((guild) => setTimeout(function Wait ()
{
i += 1;
if (guild.id && guild.systemChannel && guild.systemChannel.permissionsFor(guild.me).has("SEND_MESSAGES"))
{
console.log(`Embed Message ${i} Sent to guild ${guild.id} - ${guild.name}`);
return guild.systemChannel.send({"embed": {
"color": 9514728,
"description": `${data.announcement.message}`,
"footer": {
"text": "This is a test message"
},
"title": `${data.announcement.title}`
}});
}
}, 1000));
All this does is it waits for 1 second before sending all messages.
I have also tried:
let i = 0;
for (const guild of data.message.client.guilds.cache)
{
setTimeout(function Wait ()
{
i += 1;
if (guild.id && guild.systemChannel && guild.systemChannel.permissionsFor(guild.me).has("SEND_MESSAGES"))
{
console.log(`Embed Message ${i} Sent to guild ${guild.id} - ${guild.name}`);
return guild.systemChannel.send({"embed": {
"color": 9514728,
"description": `${data.announcement.message}`,
"footer": {
"text": "This is a test message"
},
"title": `${data.announcement.title}`
}});
}
}, 1000);
}
Upvotes: 0
Views: 388
Reputation: 10201
What you have right now is you go through each 500 guildes at once and set delay of 1sec. After 1sec all 500 messages are sent.
What you need to do is send each message after previous message was sent.
Try this (assuming data.message.client.guilds.cache
is an array):
let i = 0;
!function sendMessage ()
{
const guild = data.message.client.guilds.cache[i++];
if (guild.id && guild.systemChannel && guild.systemChannel.permissionsFor(guild.me).has("SEND_MESSAGES"))
{
console.log(`Embed Message ${i} Sent to guild ${guild.id} - ${guild.name}`);
guild.systemChannel.send({"embed": {
"color": 9514728,
"description": `${data.announcement.message}`,
"footer": {
"text": "This is a test message"
},
"title": `${data.announcement.title}`
}});
}
if (i <= data.message.client.guilds.cache.length)
setTimeout(sendMessage, 1000);
}();
Upvotes: 1
Reputation: 35
So this may not be the best way, but it works.
let i = 0;
const guilds = [];
for (const guild of data.message.client.guilds.cache)
{
guilds.push(guild);
}
const wait = setInterval(function ()
{
const guild = guilds.shift();
i += 1;
if (guild === undefined)
{
console.log(`Done all servers`);
clearInterval(wait);
}
else if (guild[1].id && guild[1].systemChannel && guild[1].systemChannel.permissionsFor(guild[1].me).has("SEND_MESSAGES"))
{
console.log(`Embed Message ${i} Sent to guild ${guild[1].id} - ${guild[1].name}`);
return guild[1].systemChannel.send({"embed": {
"color": 9514728,
"description": `${data.announcement.message}`,
"footer": {
"text": "This is a test message"},
"title": `${data.announcement.title}`
}});
}
}, 1000);
Upvotes: 1
Reputation: 676
You could use setTimeOut and a promise.
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms))
then running sleep(MS)
will wait before continuing
async function run() {
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
console.log(1);
await sleep(1000); // wait one second
console.log(2);
await sleep(1000); // wait one second
console.log(3);
}
run()
Upvotes: 0