zappyzep/src/plugins/ReactionRoles.ts

230 lines
8 KiB
TypeScript
Raw Normal View History

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";
import { ZeppelinPlugin } from "./ZeppelinPlugin";
import { GuildSavedMessages } from "../data/GuildSavedMessages";
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
export class ReactionRolesPlugin extends ZeppelinPlugin {
2019-01-12 13:42:11 +02:00
public static pluginName = "reaction_roles";
2018-07-29 15:18:26 +03:00
protected reactionRoles: GuildReactionRoles;
protected savedMessages: GuildSavedMessages;
2018-07-29 15:18:26 +03:00
2019-02-16 12:01:31 +02:00
protected reactionRemoveQueue: Queue;
protected pendingRoles: Set<string>;
2019-02-16 12:01:31 +02:00
2018-07-29 15:18:26 +03:00
getDefaultOptions() {
return {
permissions: {
manage: false,
2018-07-29 15:18:26 +03:00
},
overrides: [
{
level: ">=100",
permissions: {
manage: true,
},
},
],
2018-07-29 15:18:26 +03:00
};
}
async onLoad() {
this.reactionRoles = GuildReactionRoles.getInstance(this.guildId);
this.savedMessages = GuildSavedMessages.getInstance(this.guildId);
2019-02-16 12:01:31 +02:00
this.reactionRemoveQueue = new Queue();
this.pendingRoles = new Set();
}
2018-07-29 15:18:26 +03:00
/**
* COMMAND: Clear reaction roles from the specified message
*/
@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
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
this.reactionRoles.removeFromMessage(args.messageId);
2018-07-29 15:18:26 +03: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
msg.channel.createMessage(successMessage("Reaction roles cleared"));
}
/**
* 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
}
@d.command("reaction_roles", "<messageId:string> <reactionRolePairs:string$>")
2018-07-29 15:18:26 +03:00
@d.permission("manage")
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;
}
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;
}
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
.map(
(pair): ReactionRolePair => {
2019-02-06 20:07:53 +02:00
const customEmojiMatch = pair[0].match(/^<a?:(.*?):(\d+)>$/);
if (customEmojiMatch) {
2019-02-06 20:07:53 +02:00
return [customEmojiMatch[2], pair[1], customEmojiMatch[1]];
} else {
return pair as ReactionRolePair;
}
},
);
2018-07-29 15:18:26 +03:00
// Verify the specified emojis and roles are valid
for (const pair of newRolePairs) {
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);
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(
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(
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);
await this.reactionRoles.add(channel.id, targetMessage.id, rolePair[0], rolePair[1]);
2018-07-29 15:18:26 +03:00
}
}
@d.event("messageReactionAdd")
@d.nonBlocking()
2018-07-29 15:18:26 +03:00
async onAddReaction(msg: Message, emoji: CustomEmoji, userId: string) {
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;
const pendingKey = `${userId}-${matchingReactionRole.role_id}`;
if (this.pendingRoles.has(pendingKey)) return;
this.pendingRoles.add(pendingKey);
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
// Remove the reaction after a small delay
setTimeout(() => {
this.reactionRemoveQueue.add(async () => {
this.pendingRoles.delete(pendingKey);
const reaction = emoji.id ? `${emoji.name}:${emoji.id}` : emoji.name;
await msg.channel.removeMessageReaction(msg.id, reaction, userId).catch(noop);
await sleep(250);
});
}, 2000);
2018-07-29 15:18:26 +03:00
}
}