|
@@ -4,9 +4,9 @@ import { MessageReaction } from "@shared/db/entity/MessageReaction";
|
|
import { KnownUser } from "@shared/db/entity/KnownUser";
|
|
import { KnownUser } from "@shared/db/entity/KnownUser";
|
|
import { ReactionType, ReactionEmote } from "@shared/db/entity/ReactionEmote";
|
|
import { ReactionType, ReactionEmote } from "@shared/db/entity/ReactionEmote";
|
|
import { isAuthorisedAsync } from "../util";
|
|
import { isAuthorisedAsync } from "../util";
|
|
-import { CommandSet, Command, Action, ActionType } from "src/model/command";
|
|
|
|
import { Message } from "discord.js";
|
|
import { Message } from "discord.js";
|
|
import { logger } from "src/logging";
|
|
import { logger } from "src/logging";
|
|
|
|
+import { Command, ICommandData, Event, BotEventData, Plugin } from "src/model/plugin";
|
|
|
|
|
|
const pattern = /^react to\s+"([^"]+)"\s+with\s+<:[^:]+:([^>]+)>$/i;
|
|
const pattern = /^react to\s+"([^"]+)"\s+with\s+<:[^:]+:([^>]+)>$/i;
|
|
|
|
|
|
@@ -26,54 +26,74 @@ async function getRandomEmotes(allowedTypes: ReactionType[], limit: number) {
|
|
return a;
|
|
return a;
|
|
}
|
|
}
|
|
|
|
|
|
-@CommandSet
|
|
|
|
|
|
+@Plugin
|
|
export class ReactCommands {
|
|
export class ReactCommands {
|
|
|
|
|
|
- @Command({ pattern: "react to", auth: true, documentation: {description: "React to <message> with <emote>.", example: "react to \"<message>\" with <emote>"} })
|
|
|
|
- async addReaction(msg: Message, s: string): Promise<void> {
|
|
|
|
- if (!await isAuthorisedAsync(msg.member))
|
|
|
|
|
|
+ @Command({
|
|
|
|
+ pattern: "react to",
|
|
|
|
+ auth: true,
|
|
|
|
+ documentation: {
|
|
|
|
+ description: "React to <message> with <emote>.",
|
|
|
|
+ example: "react to \"<message>\" with <emote>"
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ async addReaction({ message, contents }: ICommandData): Promise<void> {
|
|
|
|
+ if (!await isAuthorisedAsync(message.member))
|
|
return;
|
|
return;
|
|
- const contents = pattern.exec(s);
|
|
|
|
|
|
+ const reactContents = pattern.exec(contents as string);
|
|
|
|
|
|
- if (contents != null) {
|
|
|
|
- const reactable = contents[1].trim().toLowerCase();
|
|
|
|
- const reactionEmoji = contents[2];
|
|
|
|
|
|
+ if (reactContents != null) {
|
|
|
|
+ const reactable = reactContents[1].trim().toLowerCase();
|
|
|
|
+ const reactionEmoji = reactContents[2];
|
|
|
|
|
|
if (!client.bot.emojis.cache.has(reactionEmoji)) {
|
|
if (!client.bot.emojis.cache.has(reactionEmoji)) {
|
|
- msg.channel.send(`${msg.author.toString()} I cannot react with this emoji :(`);
|
|
|
|
|
|
+ message.reply("I cannot react with this emoji :(");
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
const repo = getRepository(MessageReaction);
|
|
const repo = getRepository(MessageReaction);
|
|
|
|
|
|
- const message = repo.create({
|
|
|
|
|
|
+ const msgReaction = repo.create({
|
|
message: reactable,
|
|
message: reactable,
|
|
reactionEmoteId: reactionEmoji
|
|
reactionEmoteId: reactionEmoji
|
|
});
|
|
});
|
|
- await repo.save(message);
|
|
|
|
|
|
+ await repo.save(msgReaction);
|
|
|
|
|
|
- msg.channel.send(`${msg.author.toString()} Added reaction!`);
|
|
|
|
|
|
+ message.reply("added reaction!");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- @Command({ pattern: "remove reaction to", auth: true, documentation: {description: "Stops reacting to <message>.", example: "remove reaction to <message>"} })
|
|
|
|
- async removeReaction(msg: Message, s: string): Promise<void> {
|
|
|
|
- if (!await isAuthorisedAsync(msg.member))
|
|
|
|
|
|
+ @Command({
|
|
|
|
+ pattern: "remove reaction to",
|
|
|
|
+ auth: true,
|
|
|
|
+ documentation: {
|
|
|
|
+ description: "Stops reacting to <message>.",
|
|
|
|
+ example: "remove reaction to <message>"
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ async removeReaction({message, contents}: ICommandData): Promise<void> {
|
|
|
|
+ if (!await isAuthorisedAsync(message.member))
|
|
return;
|
|
return;
|
|
|
|
|
|
- const content = s.substring("remove reaction to ".length).trim().toLowerCase();
|
|
|
|
|
|
+ const content = (contents as string).substring("remove reaction to ".length).trim().toLowerCase();
|
|
const repo = getRepository(MessageReaction);
|
|
const repo = getRepository(MessageReaction);
|
|
const result = await repo.delete({ message: content });
|
|
const result = await repo.delete({ message: content });
|
|
|
|
|
|
if (result.affected == 0) {
|
|
if (result.affected == 0) {
|
|
- msg.channel.send(`${msg.author.toString()} No such reaction available!`);
|
|
|
|
|
|
+ message.reply("no such reaction available!");
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- msg.channel.send(`${msg.author.toString()} Removed reaction!`);
|
|
|
|
|
|
+ message.reply("removed reaction!");
|
|
}
|
|
}
|
|
|
|
|
|
- @Command({ pattern: "reactions", documentation: {description: "Lists all known messages this bot can react to.", example: "reactions"} })
|
|
|
|
- async listReactions(msg: Message): Promise<void> {
|
|
|
|
|
|
+ @Command({
|
|
|
|
+ pattern: "reactions",
|
|
|
|
+ documentation: {
|
|
|
|
+ description: "Lists all known messages this bot can react to.",
|
|
|
|
+ example: "reactions"
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ async listReactions({ message }: ICommandData): Promise<void> {
|
|
const reactionsRepo = getRepository(MessageReaction);
|
|
const reactionsRepo = getRepository(MessageReaction);
|
|
|
|
|
|
const messages = await reactionsRepo.find({
|
|
const messages = await reactionsRepo.find({
|
|
@@ -81,14 +101,16 @@ export class ReactCommands {
|
|
});
|
|
});
|
|
|
|
|
|
const reactions = messages.reduce((p, c) => `${p}\n${c.message}`, "");
|
|
const reactions = messages.reduce((p, c) => `${p}\n${c.message}`, "");
|
|
- msg.channel.send(`I'll react to the following messages:\n\`\`\`${reactions}\n\`\`\``);
|
|
|
|
|
|
+ message.reply(`I'll react to the following messages:\n\`\`\`${reactions}\n\`\`\``);
|
|
}
|
|
}
|
|
|
|
|
|
- @Action(ActionType.MESSAGE)
|
|
|
|
- async reactToMentions(actionsDone: boolean, msg: Message, content: string): Promise<boolean> {
|
|
|
|
- if (actionsDone)
|
|
|
|
- return false;
|
|
|
|
-
|
|
|
|
|
|
+ // actionsDone: boolean, msg: Message, content: string
|
|
|
|
+ @Event("message")
|
|
|
|
+ async reactToMentions(data: BotEventData, msg: Message): Promise<void> {
|
|
|
|
+ if (data.actionsDone)
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ const content = msg.cleanContent.trim();
|
|
const lowerContent = content.toLowerCase();
|
|
const lowerContent = content.toLowerCase();
|
|
|
|
|
|
const reactionRepo = getRepository(MessageReaction);
|
|
const reactionRepo = getRepository(MessageReaction);
|
|
@@ -100,11 +122,12 @@ export class ReactCommands {
|
|
const emoji = client.bot.emojis.resolve(message.reactionEmoteId);
|
|
const emoji = client.bot.emojis.resolve(message.reactionEmoteId);
|
|
if (emoji)
|
|
if (emoji)
|
|
msg.react(emoji);
|
|
msg.react(emoji);
|
|
- return true;
|
|
|
|
|
|
+ data.actionsDone = true;
|
|
|
|
+ return;
|
|
}
|
|
}
|
|
|
|
|
|
if (msg.mentions.users.size == 0)
|
|
if (msg.mentions.users.size == 0)
|
|
- return false;
|
|
|
|
|
|
+ return;
|
|
|
|
|
|
const knownUsers = await usersRepo.find({
|
|
const knownUsers = await usersRepo.find({
|
|
select: ["mentionReactionType"],
|
|
select: ["mentionReactionType"],
|
|
@@ -112,7 +135,7 @@ export class ReactCommands {
|
|
});
|
|
});
|
|
|
|
|
|
if (knownUsers.length == 0)
|
|
if (knownUsers.length == 0)
|
|
- return false;
|
|
|
|
|
|
+ return;
|
|
|
|
|
|
const reactionEmoteTypes = new Set<ReactionType>();
|
|
const reactionEmoteTypes = new Set<ReactionType>();
|
|
|
|
|
|
@@ -123,12 +146,12 @@ export class ReactCommands {
|
|
}
|
|
}
|
|
|
|
|
|
if(reactionEmoteTypes.size == 0)
|
|
if(reactionEmoteTypes.size == 0)
|
|
- return false;
|
|
|
|
|
|
+ return;
|
|
|
|
|
|
const randomEmotes = await getRandomEmotes([...reactionEmoteTypes], 5);
|
|
const randomEmotes = await getRandomEmotes([...reactionEmoteTypes], 5);
|
|
|
|
|
|
if (randomEmotes.length == 0)
|
|
if (randomEmotes.length == 0)
|
|
- return false;
|
|
|
|
|
|
+ return;
|
|
|
|
|
|
for (const emote of randomEmotes) {
|
|
for (const emote of randomEmotes) {
|
|
const emoji = client.bot.emojis.resolve(emote.reactionId);
|
|
const emoji = client.bot.emojis.resolve(emote.reactionId);
|
|
@@ -136,13 +159,13 @@ export class ReactCommands {
|
|
await msg.react(emoji);
|
|
await msg.react(emoji);
|
|
}
|
|
}
|
|
|
|
|
|
- return true;
|
|
|
|
|
|
+ data.actionsDone = true;
|
|
}
|
|
}
|
|
|
|
|
|
- @Action(ActionType.INDIRECT_MENTION)
|
|
|
|
- async reactToPing(actionsDone: boolean, msg: Message): Promise<boolean> {
|
|
|
|
- if (actionsDone)
|
|
|
|
- return false;
|
|
|
|
|
|
+ @Event("indirectMention")
|
|
|
|
+ async reactToPing(data: BotEventData, msg: Message): Promise<void> {
|
|
|
|
+ if (data.actionsDone)
|
|
|
|
+ return;
|
|
let emoteType = ReactionType.ANGERY;
|
|
let emoteType = ReactionType.ANGERY;
|
|
|
|
|
|
const repo = getRepository(KnownUser);
|
|
const repo = getRepository(KnownUser);
|
|
@@ -156,14 +179,14 @@ export class ReactCommands {
|
|
|
|
|
|
if (knownUser) {
|
|
if (knownUser) {
|
|
if (knownUser.replyReactionType == ReactionType.NONE)
|
|
if (knownUser.replyReactionType == ReactionType.NONE)
|
|
- return false;
|
|
|
|
|
|
+ return;
|
|
emoteType = knownUser.replyReactionType;
|
|
emoteType = knownUser.replyReactionType;
|
|
}
|
|
}
|
|
|
|
|
|
const emotes = await getRandomEmotes([emoteType], 1);
|
|
const emotes = await getRandomEmotes([emoteType], 1);
|
|
|
|
|
|
if (emotes.length != 1)
|
|
if (emotes.length != 1)
|
|
- return false;
|
|
|
|
|
|
+ return;
|
|
|
|
|
|
const emote = client.bot.emojis.resolve(emotes[0].reactionId);
|
|
const emote = client.bot.emojis.resolve(emotes[0].reactionId);
|
|
|
|
|
|
@@ -172,10 +195,10 @@ export class ReactCommands {
|
|
|
|
|
|
const emotesRepo = getRepository(ReactionEmote);
|
|
const emotesRepo = getRepository(ReactionEmote);
|
|
await emotesRepo.delete({ reactionId: emotes[0].reactionId });
|
|
await emotesRepo.delete({ reactionId: emotes[0].reactionId });
|
|
- return false;
|
|
|
|
|
|
+ return;
|
|
}
|
|
}
|
|
|
|
|
|
msg.channel.send(emote.toString());
|
|
msg.channel.send(emote.toString());
|
|
- return true;
|
|
|
|
|
|
+ data.actionsDone = true;
|
|
}
|
|
}
|
|
}
|
|
}
|