Created
March 14, 2022 07:19
-
-
Save MSFTserver/a3348ea0fd773866648b11436d8b52f9 to your computer and use it in GitHub Desktop.
simple discord bot to react to every message in a list/array of channel IDs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Discord = require('discord.js'); | |
const bot = new Discord.Client({ intents: [Discord.Intents.FLAGS.GUILDS] }); | |
// config | |
let botToken = 'Insert-Bot-Token-Here'; | |
let messageLimit = 2000; //number of messages to look back by not limited by discord.js default of 100 | |
let emoji = '✅'; //emoji to react with | |
let channelsList = ['952594922194739220','952587451728285746']; //list of channels to react in | |
let reactionTotal = 0; | |
async function fetchMore(channel, limit) { | |
if (!channel) { | |
throw new Error(`Expected channel, got ${typeof channel}.`); | |
} | |
if (limit <= 100) { | |
return channel.messages.fetch({ limit }); | |
} | |
let collection = new Discord.Collection(); | |
let lastId = null; | |
let options = {}; | |
let remaining = limit; | |
while (remaining > 0) { | |
options.limit = remaining > 100 ? 100 : remaining; | |
remaining = remaining > 100 ? remaining - 100 : 0; | |
if (lastId) { | |
options.before = lastId; | |
} | |
let messages = await channel.messages.fetch(options); | |
if (!messages.last()) { | |
break; | |
} | |
collection = collection.concat(messages); | |
lastId = messages.last().id; | |
} | |
return collection; | |
} | |
// Listener Event: Runs whenever the bot sends a ready event (when it first starts for example) | |
bot.on('ready', async () => { | |
console.log('connected') | |
try { | |
for (channelID in channelsList) { | |
console.log('running in channel ${channelsList[channelID]}') | |
const channel = bot.channels.cache.get(channelsList[channelID]); | |
if (!channel) return console.error('Invalid ID or missing channel.'); | |
const messages = await fetchMore(channel, messageLimit) | |
for (const [id, message] of messages) { | |
reactionTotal++ | |
await message.react(emoji); | |
} | |
console.log('Reactions: ${reactionTotal}'); | |
} | |
console.log('Reactions total: ${reactionTotal}'); | |
console.log('you may now kill the bot.') | |
} catch(err) { | |
console.error(err); | |
} | |
}); | |
bot.login(botToken); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment