Skip to content

Instantly share code, notes, and snippets.

@SJongeJongeJonge
Created July 21, 2018 01:50
Show Gist options
  • Save SJongeJongeJonge/583675bc4bdce495c722b56dcce33cd0 to your computer and use it in GitHub Desktop.
Save SJongeJongeJonge/583675bc4bdce495c722b56dcce33cd0 to your computer and use it in GitHub Desktop.
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = '!';
client.on('message', async msg => {
if (msg.author.bot || !msg.content.startsWith(prefix)) return;
const args = msg.content.slice(prefix.length).split(' ');
if (args[0] === 'activitycheck') {
// Usage: `!activitycheck {message_id} {TextChannel} {emoji}`
const targetChannel = msg.mentions.channels.first();
if (typeof targetChannel === 'undefined') {
msg.channel.send('No channels mentioned');
return;
}
const emoji = resolveEmoji(args[3]);
try {
const tMsg = await targetChannel.fetchMessage(args[1]);
if (typeof tMsg === 'undefined') throw 'MESSAGE_MISSING'; // it would be good practice putting this in a variable, but too much work right now
const reaction = tMsg.reactions.get(emoji);
if (typeof reaction === 'undefined') throw 'REACTION_MISSING';
const reactionUsers = await reaction.fetchUsers(); // TODO: make function fetchAllUsers() for when we need more than 100
const members = targetChannel.members.filter(member => { // TODO: add role filter
if (member.user.bot) return false;
if (reactionUsers.has(member.id)) return false;
if (tMsg.createdTimestamp < member.joinedTimestamp) return false;
return true;
});
const memberNames = members.reduce((text, member, snowflake) => {
return text + `\n${member.displayName}`;
}, '');
const reply = `**${members.size} did not react**\n\`\`\`${memberNames}\`\`\``;
msg.channel.send(reply);
}
catch(error) {
switch(error) {
case 'MESSAGE_MISSING':
msg.channel.send('Couldn\'t fetch message.');
break;
case 'REACTION_MISSING':
msg.channel.send('Couldn\'t fetch reaction.');
break;
default:
console.log(error);
msg.channel.send('Encountered an unknown error while fetching the message and reactions.');
break;
}
}
}
if (args[0] === 'stop') {
client.destroy();
}
});
/*
* Returns the emoji id for custom emojis.
* Returns the emoji itself for Unicode emojis.
* Returns the given input if no emoji id is found.
*/
function resolveEmoji(str) {
const emoji = str.trim();
const match = /<:([\d\w_]+:\d{18})>/m.exec(emoji);
if (match) return match[1];
else return emoji;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment