Skip to content

Instantly share code, notes, and snippets.

@taradinoc
Created July 26, 2020 04:35
Show Gist options
  • Select an option

  • Save taradinoc/5f3036d1ee55008320f6bfc2d44e450e to your computer and use it in GitHub Desktop.

Select an option

Save taradinoc/5f3036d1ee55008320f6bfc2d44e450e to your computer and use it in GitHub Desktop.
$.lang.register('retrorole.usage', 'Usage: !retrorole <channel> <msg-id> <emoji> <role>');
$.lang.register('retrorole.added', 'Added role "$1" to $2 user(s) ($3 failed).');
$.lang.register('retrorole.err.channel', 'Cannot find channel "$1".');
$.lang.register('retrorole.err.message', 'Cannot find message "$1" in that channel.');
$.lang.register('retrorole.err.role', 'Cannot find role "$1".');
/* TaradinoC */
/**
* retroRole.js
*
* Assign roles based on existing reactions on a message
*/
(function () {
var Snowflake = Packages.discord4j.core.object.util.Snowflake,
ReactionEmoji = Packages.discord4j.core.object.reaction.ReactionEmoji;
/**
* @event discordChannelCommand
*/
$.bind('discordChannelCommand', function (event) {
if (!$.bot.isModuleEnabled('./discord/custom/retroRole.js')) {
return;
}
var command = event.getCommand(),
channel = event.getDiscordChannel(),
mention = event.getMention(),
args = event.getArgs(),
sender = event.getSender(),
arg1 = args[0],
arg2 = args[1],
arg3 = args[2],
arg4plus = args.slice(3).join(' ');
/**
* @discordcommandpath retrorole [channel] [msg-id] [emoji] [role] - Assign a role to everyone who gave a certain reaction to a message
*/
if (command.equalsIgnoreCase('retrorole')) {
if (!arg1 || !arg2 || !arg3 || !arg4plus) {
$.discord.say(channel, $.discord.userPrefix(mention) + $.lang.get('retrorole.usage'));
return;
}
var targetChannel = $.discordAPI.getChannel(arg1);
if (!targetChannel) {
$.discord.say(channel, $.discord.userPrefix(mention) + $.lang.get('retrorole.err.channel', arg1));
return;
}
var message = targetChannel.getMessageById(Snowflake.of(arg2)).block();
if (!message) {
$.discord.say(channel, $.discord.userPrefix(mention) + $.lang.get('retrorole.err.message', arg2));
return;
}
var emoji = ReactionEmoji.unicode(arg3),
role = $.discordAPI.getRole(arg4plus);
if (!role) {
$.discord.say(channel, $.discord.userPrefix(mention) + $.lang.get('retrorole.err.role', arg4plus));
return;
}
var reactors = message.getReactors(emoji).collectList().block();
var added = 0, failed = 0;
for (var i = 0; i < reactors.size(); i++) {
var r = reactors.get(i);
if (String(r.getUsername()).equalsIgnoreCase('carl-bot')) {
continue;
}
try {
$.discordAPI.addRole(role, r);
added++;
} catch (err) {
$.log.error('Failed to add role ' + role.getName() + ' to user ' + r.getUsername() + ': ' + err);
failed++;
}
}
$.discord.say(channel, $.discord.userPrefix(mention) + $.lang.get('retrorole.added', role.getName(), added, failed));
return;
}
});
/**
* @event initReady
*/
$.bind('initReady', function () {
$.discord.registerCommand('./discord/custom/retroRole.js', 'retrorole', 1);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment