Browse Source

Add stickers

ghorsington 3 years ago
parent
commit
b9fe8f73ca

+ 76 - 0
bot/src/plugins/stickers.ts

@@ -0,0 +1,76 @@
+import { Plugin, Event, BotEventData, Command, ICommandData } from "src/model/plugin";
+import { readdirSync, statSync } from "fs";
+import { join, basename, extname } from "path";
+import { Message } from "discord.js";
+import { tryDo } from "src/util";
+import { logger } from "src/logging";
+
+const STICKERS_PATH = "./stickers";
+const STICKERS_PREFIX = "!";
+const STICKERS_PER_ROW = 5;
+const ROWS_PER_MESSAGE = 30;
+
+@Plugin
+export class Stickers {
+    stickers: Record<string, string> = {};
+
+    @Event("message")
+    async onMessage(data: BotEventData, msg: Message): Promise<void> {
+        if (data.actionsDone)
+            return;
+
+        const lowerContent = msg.cleanContent.trim().toLowerCase();
+        if (!lowerContent.startsWith(STICKERS_PREFIX))
+            return;
+
+        const stickerName = lowerContent.substr(1);
+        if (!(stickerName in this.stickers))
+            return;
+
+        const deleteResult = await tryDo(msg.delete());
+        if (!deleteResult.ok) {
+            logger.error("Stickers: failed to delete message %s from user %s. Reason: %s", msg.id, msg.author.id, deleteResult.error);
+        }
+        const sendResult = await tryDo(msg.channel.send(`${msg.author.toString()} *sent a sticker:*`, {
+            files: [this.stickers[stickerName]]
+        }));
+
+        if (!sendResult.ok) {
+            logger.error("Stickers: failed to send sticker to channel %s. Reason: %s", msg.channel.id, sendResult.error);
+        }
+        data.actionsDone = true;
+    }
+
+    @Command({
+        type: "mention",
+        pattern: "stickers",
+        auth: false,
+        documentation: { description: "Lists all available stickers", example: "stickers" }
+    })
+    async listStickers({ message }: ICommandData): Promise<void> {
+        const m = Object.keys(this.stickers)
+            .filter(s => Object.prototype.hasOwnProperty.call(this.stickers, s))
+            .reduce((prev, cur, i) => `${prev}     !${cur}${i != 0 && i % STICKERS_PER_ROW == 0 ? "\n" : ""}`, "");
+        
+        let toSend = `${message.author.toString()}, I have the following stickers:\n\`\`\``;
+        const rows = m.split("\n");
+        for (let i = 0; i < rows.length; i++) {
+            toSend += `${rows[i]}\n`;
+            if (i != 0 && i % ROWS_PER_MESSAGE == 0) {
+                toSend += "```";
+                await message.channel.send(toSend);
+                toSend = "```\n";
+            }
+        }
+        toSend += "```\n";
+        toSend += "To use stickers, simply use their name. Example: !hackermaid";
+        await message.channel.send(toSend);
+    }
+
+    async start(): Promise<void> {
+        const files = readdirSync(STICKERS_PATH).filter(f => !statSync(join(STICKERS_PATH, f)).isDirectory());
+        for (const file of files)
+            this.stickers[basename(file, extname(file)).toLowerCase()] = join(STICKERS_PATH, file);
+        logger.info("Found %s stickers", Object.keys(this.stickers).length);
+    }
+}

BIN
bot/stickers/I see.png


BIN
bot/stickers/chotto.png


BIN
bot/stickers/dealfa.jpg


BIN
bot/stickers/dotdotdot.png


BIN
bot/stickers/ehehe.png


BIN
bot/stickers/fuck.png


BIN
bot/stickers/gahaha.png


BIN
bot/stickers/good job.png


BIN
bot/stickers/hackermaid.png


BIN
bot/stickers/having fun.gif


BIN
bot/stickers/hehe.png


BIN
bot/stickers/heresy.png


BIN
bot/stickers/humu.png


BIN
bot/stickers/its joke.png


BIN
bot/stickers/leave.png


BIN
bot/stickers/lewd.png


BIN
bot/stickers/muri.png


BIN
bot/stickers/naruhodo.png


BIN
bot/stickers/nishishi.png


BIN
bot/stickers/nyaa.png


BIN
bot/stickers/ohoho.png


BIN
bot/stickers/omagah.png


BIN
bot/stickers/oooh.png


BIN
bot/stickers/peek.png


BIN
bot/stickers/peek2.png


BIN
bot/stickers/pout.png


BIN
bot/stickers/readme.png


BIN
bot/stickers/shiku.png


BIN
bot/stickers/shoo.png


BIN
bot/stickers/smug.png


BIN
bot/stickers/smugfa.jpg


BIN
bot/stickers/smugger.png


BIN
bot/stickers/smugtastic.png


BIN
bot/stickers/sugoi.png


BIN
bot/stickers/triggered.jpg


BIN
bot/stickers/want lewd.png


BIN
bot/stickers/xd.png


BIN
bot/stickers/zettai muri.png


+ 2 - 0
docker-compose.yml

@@ -13,6 +13,8 @@ services:
     env_file: 
       - .env
       - db.env
+    volumes: 
+      - ./bot/stickers:/app/bot/stickers
     environment:
       NODE_ENV: production
       BOT_TOKEN: ${BOT_TOKEN}