2019-02-09 14:36:03 +02:00
|
|
|
import { decorators as d } from "knub";
|
2019-02-16 12:01:31 +02:00
|
|
|
import { CustomEmoji, errorMessage, isSnowflake, noop, sleep, successMessage } from "../utils";
|
2018-07-29 15:18:26 +03:00
|
|
|
import { GuildReactionRoles } from "../data/GuildReactionRoles";
|
2019-01-12 13:42:11 +02:00
|
|
|
import { Channel, Message, TextChannel } from "eris";
|
2019-02-09 14:36:03 +02:00
|
|
|
import { ZeppelinPlugin } from "./ZeppelinPlugin";
|
|
|
|
import { GuildSavedMessages } from "../data/GuildSavedMessages";
|
2019-02-10 22:32:27 +02:00
|
|
|
import { SavedMessage } from "../data/entities/SavedMessage";
|
2019-02-16 12:01:31 +02:00
|
|
|
import { Queue } from "../Queue";
|
2018-07-29 15:18:26 +03:00
|
|
|
|
2019-02-06 20:07:53 +02:00
|
|
|
type ReactionRolePair = [string, string, string?];
|
2018-07-29 15:18:26 +03:00
|
|
|
|
2019-02-09 14:36:03 +02:00
|
|
|
export class ReactionRolesPlugin extends ZeppelinPlugin {
|
2019-01-12 13:42:11 +02:00
|
|
|
public static pluginName = "reaction_roles";
|
2019-01-03 06:15:28 +02:00
|
|
|
|
2018-07-29 15:18:26 +03:00
|
|
|
protected reactionRoles: GuildReactionRoles;
|
2019-02-09 14:36:03 +02:00
|
|
|
protected savedMessages: GuildSavedMessages;
|
2018-07-29 15:18:26 +03:00
|
|
|
|
2019-02-16 12:01:31 +02:00
|
|
|
protected reactionRemoveQueue: Queue;
|
2019-02-16 16:03:20 +02:00
|
|
|
protected pendingRoles: Set<string>;
|
2019-02-16 12:01:31 +02:00
|
|
|
|
2018-07-29 15:18:26 +03:00
|
|
|
getDefaultOptions() {
|
|
|
|
return {
|
|
|
|
permissions: {
|
2019-02-09 14:36:03 +02:00
|
|
|
manage: false,
|
2018-07-29 15:18:26 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
overrides: [
|
|
|
|
{
|
|
|
|
level: ">=100",
|
|
|
|
permissions: {
|
2019-02-09 14:36:03 +02:00
|
|
|
manage: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2018-07-29 15:18:26 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async onLoad() {
|
2018-10-26 06:41:20 +03:00
|
|
|
this.reactionRoles = GuildReactionRoles.getInstance(this.guildId);
|
2019-02-09 14:36:03 +02:00
|
|
|
this.savedMessages = GuildSavedMessages.getInstance(this.guildId);
|
2019-02-16 12:01:31 +02:00
|
|
|
this.reactionRemoveQueue = new Queue();
|
2019-02-16 16:03:20 +02:00
|
|
|
this.pendingRoles = new Set();
|
2019-02-09 14:36:03 +02:00
|
|
|
}
|
2018-07-29 15:18:26 +03:00
|
|
|
|
2019-02-10 22:32:27 +02:00
|
|
|
/**
|
|
|
|
* COMMAND: Clear reaction roles from the specified message
|
|
|
|
*/
|
2019-02-09 14:36:03 +02:00
|
|
|
@d.command("reaction_roles clear", "<messageId:string>")
|
|
|
|
@d.permission("manage")
|
|
|
|
async clearReactionRolesCmd(msg: Message, args: { messageId: string }) {
|
|
|
|
const savedMessage = await this.savedMessages.find(args.messageId);
|
|
|
|
if (!savedMessage) {
|
|
|
|
msg.channel.createMessage(errorMessage("Unknown message"));
|
|
|
|
return;
|
|
|
|
}
|
2018-07-29 15:18:26 +03:00
|
|
|
|
2019-02-09 14:36:03 +02:00
|
|
|
const existingReactionRoles = this.reactionRoles.getForMessage(args.messageId);
|
|
|
|
if (!existingReactionRoles) {
|
|
|
|
msg.channel.createMessage(errorMessage("Message doesn't have reaction roles on it"));
|
|
|
|
return;
|
|
|
|
}
|
2018-07-29 15:18:26 +03:00
|
|
|
|
2019-02-09 14:36:03 +02:00
|
|
|
this.reactionRoles.removeFromMessage(args.messageId);
|
2018-07-29 15:18:26 +03:00
|
|
|
|
2019-02-09 14:36:03 +02:00
|
|
|
const channel = this.guild.channels.get(savedMessage.channel_id) as TextChannel;
|
|
|
|
const targetMessage = await channel.getMessage(savedMessage.id);
|
|
|
|
await targetMessage.removeReactions();
|
2018-07-29 15:18:26 +03:00
|
|
|
|
2019-02-09 14:36:03 +02:00
|
|
|
msg.channel.createMessage(successMessage("Reaction roles cleared"));
|
2019-02-10 22:32:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* COMMAND: Refresh reaction roles in the specified message by removing all reactions and reapplying them
|
|
|
|
*/
|
|
|
|
@d.command("reaction_roles refresh", "<messageId:string>")
|
|
|
|
@d.permission("manage")
|
|
|
|
async refreshReactionRolesCmd(msg: Message, args: { messageId: string }) {
|
|
|
|
const savedMessage = await this.savedMessages.find(args.messageId);
|
|
|
|
if (!savedMessage) {
|
|
|
|
msg.channel.createMessage(errorMessage("Unknown message"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const reactionRoles = await this.reactionRoles.getForMessage(savedMessage.id);
|
|
|
|
const reactionRoleEmojis = reactionRoles.map(r => r.emoji);
|
|
|
|
|
|
|
|
const channel = this.guild.channels.get(savedMessage.channel_id) as TextChannel;
|
|
|
|
const targetMessage = await channel.getMessage(savedMessage.id);
|
|
|
|
const existingReactions = targetMessage.reactions;
|
|
|
|
|
|
|
|
// Remove reactions
|
|
|
|
await targetMessage.removeReactions();
|
|
|
|
|
|
|
|
// Re-add reactions
|
|
|
|
for (const emoji of Object.keys(existingReactions)) {
|
|
|
|
const emojiId = emoji.includes(":") ? emoji.split(":")[1] : emoji;
|
|
|
|
if (!reactionRoleEmojis.includes(emojiId)) continue;
|
|
|
|
|
|
|
|
await targetMessage.addReaction(emoji);
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.channel.createMessage(successMessage("Reaction roles refreshed"));
|
2018-07-29 15:18:26 +03:00
|
|
|
}
|
|
|
|
|
2019-02-09 14:36:03 +02:00
|
|
|
@d.command("reaction_roles", "<messageId:string> <reactionRolePairs:string$>")
|
2018-07-29 15:18:26 +03:00
|
|
|
@d.permission("manage")
|
2019-02-09 14:36:03 +02:00
|
|
|
async reactionRolesCmd(msg: Message, args: { messageId: string; reactionRolePairs: string }) {
|
|
|
|
const savedMessage = await this.savedMessages.find(args.messageId);
|
|
|
|
if (!savedMessage) {
|
|
|
|
msg.channel.createMessage(errorMessage("Unknown message"));
|
2018-07-29 15:18:26 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-09 14:36:03 +02:00
|
|
|
const channel = await this.guild.channels.get(savedMessage.channel_id);
|
|
|
|
if (!channel || !(channel instanceof TextChannel)) {
|
|
|
|
msg.channel.createMessage(errorMessage("Channel no longer exists"));
|
2018-07-29 15:18:26 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-09 14:36:03 +02:00
|
|
|
const targetMessage = await channel.getMessage(args.messageId);
|
|
|
|
if (!targetMessage) {
|
|
|
|
msg.channel.createMessage(errorMessage("Unknown message (2)"));
|
|
|
|
return;
|
|
|
|
}
|
2018-07-29 15:18:26 +03:00
|
|
|
|
|
|
|
// Turn "emoji = role" pairs into an array of tuples of the form [emoji, roleId]
|
|
|
|
// Emoji is either a unicode emoji or the snowflake of a custom emoji
|
|
|
|
const newRolePairs: ReactionRolePair[] = args.reactionRolePairs
|
|
|
|
.trim()
|
|
|
|
.split("\n")
|
2019-01-12 13:42:11 +02:00
|
|
|
.map(v => v.split("=").map(v => v.trim())) // tslint:disable-line
|
2018-08-03 19:26:27 +03:00
|
|
|
.map(
|
|
|
|
(pair): ReactionRolePair => {
|
2019-02-06 20:07:53 +02:00
|
|
|
const customEmojiMatch = pair[0].match(/^<a?:(.*?):(\d+)>$/);
|
2018-08-03 19:26:27 +03:00
|
|
|
if (customEmojiMatch) {
|
2019-02-06 20:07:53 +02:00
|
|
|
return [customEmojiMatch[2], pair[1], customEmojiMatch[1]];
|
2018-08-03 19:26:27 +03:00
|
|
|
} else {
|
|
|
|
return pair as ReactionRolePair;
|
|
|
|
}
|
2019-02-09 14:36:03 +02:00
|
|
|
},
|
2018-08-03 19:26:27 +03:00
|
|
|
);
|
2018-07-29 15:18:26 +03:00
|
|
|
|
|
|
|
// Verify the specified emojis and roles are valid
|
|
|
|
for (const pair of newRolePairs) {
|
2019-02-09 14:36:03 +02:00
|
|
|
if (!this.canUseEmoji(pair[0])) {
|
|
|
|
msg.channel.createMessage(errorMessage("I can only use regular emojis and custom emojis from servers I'm on"));
|
2018-07-29 15:18:26 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.guild.roles.has(pair[1])) {
|
|
|
|
msg.channel.createMessage(errorMessage(`Unknown role ${pair[1]}`));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const oldReactionRoles = await this.reactionRoles.getForMessage(targetMessage.id);
|
2018-10-26 06:41:20 +03:00
|
|
|
const oldRolePairs: ReactionRolePair[] = oldReactionRoles.map(r => [r.emoji, r.role_id] as ReactionRolePair);
|
2018-07-29 15:18:26 +03:00
|
|
|
|
|
|
|
// Remove old reaction/role pairs that weren't included in the new pairs or were changed in some way
|
|
|
|
const toRemove = oldRolePairs.filter(
|
2019-02-09 14:36:03 +02:00
|
|
|
pair => !newRolePairs.find(oldPair => oldPair[0] === pair[0] && oldPair[1] === pair[1]),
|
2018-07-29 15:18:26 +03:00
|
|
|
);
|
|
|
|
for (const rolePair of toRemove) {
|
|
|
|
await this.reactionRoles.removeFromMessage(targetMessage.id, rolePair[0]);
|
|
|
|
|
2019-02-06 20:07:53 +02:00
|
|
|
for (const emoji of Object.keys(targetMessage.reactions)) {
|
|
|
|
const emojiId = emoji.includes(":") ? emoji.split(":")[1] : emoji;
|
|
|
|
|
|
|
|
if (emojiId === rolePair[0]) {
|
|
|
|
targetMessage.removeReaction(emoji, this.bot.user.id);
|
2018-07-29 15:18:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add new/changed reaction/role pairs
|
|
|
|
const toAdd = newRolePairs.filter(
|
2019-02-09 14:36:03 +02:00
|
|
|
pair => !oldRolePairs.find(oldPair => oldPair[0] === pair[0] && oldPair[1] === pair[1]),
|
2018-07-29 15:18:26 +03:00
|
|
|
);
|
|
|
|
for (const rolePair of toAdd) {
|
|
|
|
let emoji;
|
|
|
|
|
2019-02-06 20:07:53 +02:00
|
|
|
if (rolePair[2]) {
|
2018-07-29 15:18:26 +03:00
|
|
|
// Custom emoji
|
2019-02-06 20:07:53 +02:00
|
|
|
emoji = `${rolePair[2]}:${rolePair[0]}`;
|
2018-07-29 15:18:26 +03:00
|
|
|
} else {
|
|
|
|
// Unicode emoji
|
|
|
|
emoji = rolePair[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
await targetMessage.addReaction(emoji);
|
2019-02-09 14:36:03 +02:00
|
|
|
await this.reactionRoles.add(channel.id, targetMessage.id, rolePair[0], rolePair[1]);
|
2018-07-29 15:18:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@d.event("messageReactionAdd")
|
|
|
|
async onAddReaction(msg: Message, emoji: CustomEmoji, userId: string) {
|
2018-10-26 06:41:20 +03:00
|
|
|
const matchingReactionRole = await this.reactionRoles.getByMessageAndEmoji(msg.id, emoji.id || emoji.name);
|
2018-07-29 15:18:26 +03:00
|
|
|
if (!matchingReactionRole) return;
|
|
|
|
|
|
|
|
const member = this.guild.members.get(userId);
|
|
|
|
if (!member) return;
|
|
|
|
|
2019-02-16 16:03:20 +02:00
|
|
|
const pendingKey = `${userId}-${matchingReactionRole.role_id}`;
|
|
|
|
if (this.pendingRoles.has(pendingKey)) return;
|
|
|
|
this.pendingRoles.add(pendingKey);
|
|
|
|
|
2019-02-16 11:58:11 +02:00
|
|
|
if (member.roles.includes(matchingReactionRole.role_id)) {
|
|
|
|
await member.removeRole(matchingReactionRole.role_id).catch(err => {
|
|
|
|
console.warn(`Could not remove role ${matchingReactionRole.role_id} from ${userId}`, err && err.message);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await member.addRole(matchingReactionRole.role_id).catch(err => {
|
|
|
|
console.warn(`Could not add role ${matchingReactionRole.role_id} to ${userId}`, err && err.message);
|
|
|
|
});
|
|
|
|
}
|
2018-07-29 15:18:26 +03:00
|
|
|
|
2019-02-16 14:40:44 +02:00
|
|
|
// Remove the reaction after a small delay
|
|
|
|
setTimeout(() => {
|
|
|
|
this.reactionRemoveQueue.add(async () => {
|
2019-02-16 16:03:20 +02:00
|
|
|
this.pendingRoles.delete(pendingKey);
|
|
|
|
|
2019-02-16 14:40:44 +02:00
|
|
|
const reaction = emoji.id ? `${emoji.name}:${emoji.id}` : emoji.name;
|
|
|
|
await msg.channel.removeMessageReaction(msg.id, reaction, userId).catch(noop);
|
|
|
|
await sleep(250);
|
|
|
|
});
|
2019-02-16 16:43:59 +02:00
|
|
|
}, 15 * 1000);
|
2018-07-29 15:18:26 +03:00
|
|
|
}
|
|
|
|
}
|