3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-16 22:21:51 +00:00
zeppelin/src/plugins/AutoReactions.ts

107 lines
3.3 KiB
TypeScript

import { Plugin, decorators as d } from "knub";
import { GuildSavedMessages } from "../data/GuildSavedMessages";
import { SavedMessage } from "../data/entities/SavedMessage";
import { GuildAutoReactions } from "../data/GuildAutoReactions";
import { Message } from "eris";
import { CustomEmoji, customEmojiRegex, errorMessage, isEmoji, successMessage } from "../utils";
export class AutoReactions extends Plugin {
public static pluginName = "auto_reactions";
protected savedMessages: GuildSavedMessages;
protected autoReactions: GuildAutoReactions;
private onMessageCreateFn;
getDefaultOptions() {
return {
permissions: {
use: false
},
overrides: [
{
level: ">=100",
permissions: {
use: true
}
}
]
};
}
onLoad() {
this.savedMessages = GuildSavedMessages.getInstance(this.guildId);
this.autoReactions = GuildAutoReactions.getInstance(this.guildId);
this.onMessageCreateFn = this.savedMessages.events.on("create", this.onMessageCreate.bind(this));
}
onUnload() {
this.savedMessages.events.off("create", this.onMessageCreateFn);
}
@d.command("auto_reactions", "<channelId:channelId> <reactions...>")
@d.permission("use")
async setAutoReactionsCmd(msg: Message, args: { channelId: string; reactions: string[] }) {
const guildEmojis = this.guild.emojis as CustomEmoji[];
const guildEmojiIds = guildEmojis.map(e => e.id);
const finalReactions = [];
for (const reaction of args.reactions) {
if (!isEmoji(reaction)) {
console.log("invalid:", reaction);
msg.channel.createMessage(errorMessage("One or more of the specified reactions were invalid!"));
return;
}
let savedValue;
const customEmojiMatch = reaction.match(customEmojiRegex);
if (customEmojiMatch) {
// Custom emoji
if (!guildEmojiIds.includes(customEmojiMatch[2])) {
msg.channel.createMessage(errorMessage("I can only use regular emojis and custom emojis from this server"));
return;
}
savedValue = `${customEmojiMatch[1]}:${customEmojiMatch[2]}`;
} else {
// Unicode emoji
savedValue = reaction;
}
finalReactions.push(savedValue);
}
await this.autoReactions.set(args.channelId, finalReactions);
msg.channel.createMessage(successMessage(`Auto-reactions set for <#${args.channelId}>`));
}
@d.command("auto_reactions disable", "<channelId:channelId>")
@d.permission("use")
async disableAutoReactionsCmd(msg: Message, args: { channelId: string }) {
const autoReaction = await this.autoReactions.getForChannel(args.channelId);
if (!autoReaction) {
msg.channel.createMessage(errorMessage(`Auto-reactions aren't enabled in <#${args.channelId}>`));
return;
}
await this.autoReactions.removeFromChannel(args.channelId);
msg.channel.createMessage(successMessage(`Auto-reactions disabled in <#${args.channelId}>`));
}
async onMessageCreate(msg: SavedMessage) {
const autoReaction = await this.autoReactions.getForChannel(msg.channel_id);
if (!autoReaction) return;
const realMsg = await this.bot.getMessage(msg.channel_id, msg.id);
if (!realMsg) return;
for (const reaction of autoReaction.reactions) {
await realMsg.addReaction(reaction);
}
}
}