2019-03-04 21:44:04 +02:00
|
|
|
import { decorators as d, IBasePluginConfig, IPluginOptions } from "knub";
|
2019-01-12 13:42:11 +02:00
|
|
|
import { GuildSavedMessages } from "../data/GuildSavedMessages";
|
|
|
|
import { SavedMessage } from "../data/entities/SavedMessage";
|
|
|
|
import { GuildAutoReactions } from "../data/GuildAutoReactions";
|
|
|
|
import { Message } from "eris";
|
2019-02-09 14:35:24 +02:00
|
|
|
import { customEmojiRegex, errorMessage, isEmoji, successMessage } from "../utils";
|
|
|
|
import { ZeppelinPlugin } from "./ZeppelinPlugin";
|
2019-07-11 12:23:57 +03:00
|
|
|
import * as t from "io-ts";
|
2019-01-12 13:42:11 +02:00
|
|
|
|
2019-07-21 21:15:52 +03:00
|
|
|
const ConfigSchema = t.type({
|
2019-07-11 12:23:57 +03:00
|
|
|
can_manage: t.boolean,
|
|
|
|
});
|
2019-07-21 21:15:52 +03:00
|
|
|
type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
|
2019-03-04 21:44:04 +02:00
|
|
|
|
2019-07-11 12:23:57 +03:00
|
|
|
export class AutoReactionsPlugin extends ZeppelinPlugin<TConfigSchema> {
|
2019-01-12 13:42:11 +02:00
|
|
|
public static pluginName = "auto_reactions";
|
2019-07-21 21:15:52 +03:00
|
|
|
protected static configSchema = ConfigSchema;
|
2019-01-12 13:42:11 +02:00
|
|
|
|
|
|
|
protected savedMessages: GuildSavedMessages;
|
|
|
|
protected autoReactions: GuildAutoReactions;
|
|
|
|
|
|
|
|
private onMessageCreateFn;
|
|
|
|
|
2019-07-22 00:09:45 +03:00
|
|
|
protected static getStaticDefaultOptions(): IPluginOptions<TConfigSchema> {
|
2019-01-12 13:42:11 +02:00
|
|
|
return {
|
2019-04-13 01:44:18 +03:00
|
|
|
config: {
|
|
|
|
can_manage: false,
|
2019-01-12 13:42:11 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
overrides: [
|
|
|
|
{
|
|
|
|
level: ">=100",
|
2019-04-13 01:44:18 +03:00
|
|
|
config: {
|
|
|
|
can_manage: true,
|
2019-02-09 14:35:24 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2019-01-12 13:42:11 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
onLoad() {
|
2019-05-25 21:25:34 +03:00
|
|
|
this.savedMessages = GuildSavedMessages.getGuildInstance(this.guildId);
|
|
|
|
this.autoReactions = GuildAutoReactions.getGuildInstance(this.guildId);
|
2019-01-12 13:42:11 +02:00
|
|
|
|
|
|
|
this.onMessageCreateFn = this.savedMessages.events.on("create", this.onMessageCreate.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
onUnload() {
|
|
|
|
this.savedMessages.events.off("create", this.onMessageCreateFn);
|
|
|
|
}
|
|
|
|
|
2019-08-10 01:47:45 +03:00
|
|
|
@d.command("auto_reactions", "<channelId:channelId> <reactions...>")
|
2019-05-14 14:49:30 +03:00
|
|
|
@d.permission("can_manage")
|
2019-01-12 13:42:11 +02:00
|
|
|
async setAutoReactionsCmd(msg: Message, args: { channelId: string; reactions: string[] }) {
|
|
|
|
const finalReactions = [];
|
|
|
|
|
|
|
|
for (const reaction of args.reactions) {
|
|
|
|
if (!isEmoji(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
|
2019-02-09 14:35:24 +02:00
|
|
|
if (!this.canUseEmoji(customEmojiMatch[2])) {
|
2019-01-12 13:42:11 +02:00
|
|
|
msg.channel.createMessage(errorMessage("I can only use regular emojis and custom emojis from this server"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-12 16:03:27 +02:00
|
|
|
savedValue = `${customEmojiMatch[1]}:${customEmojiMatch[2]}`;
|
2019-01-12 13:42:11 +02:00
|
|
|
} 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}>`));
|
|
|
|
}
|
|
|
|
|
2019-08-10 01:47:45 +03:00
|
|
|
@d.command("auto_reactions disable", "<channelId:channelId>")
|
2019-05-14 14:49:30 +03:00
|
|
|
@d.permission("can_manage")
|
2019-01-12 13:42:11 +02:00
|
|
|
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) {
|
2019-01-13 23:31:09 +02:00
|
|
|
realMsg.addReaction(reaction);
|
2019-01-12 13:42:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|