stickers.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { Plugin, Event, BotEventData, Command, ICommandData } from "src/model/plugin";
  2. import { readdirSync, statSync } from "fs";
  3. import { join, basename, extname } from "path";
  4. import { Message } from "discord.js";
  5. import { tryDo } from "src/util";
  6. import { logger } from "src/logging";
  7. const STICKERS_PATH = "./stickers";
  8. const STICKERS_PREFIX = "!";
  9. const STICKERS_PER_ROW = 5;
  10. const ROWS_PER_MESSAGE = 30;
  11. @Plugin
  12. export class Stickers {
  13. stickers: Record<string, string> = {};
  14. @Event("message")
  15. async onMessage(data: BotEventData, msg: Message): Promise<void> {
  16. if (data.actionsDone)
  17. return;
  18. const lowerContent = msg.cleanContent.trim().toLowerCase();
  19. if (!lowerContent.startsWith(STICKERS_PREFIX))
  20. return;
  21. const stickerName = lowerContent.substr(1);
  22. if (!(stickerName in this.stickers))
  23. return;
  24. const deleteResult = await tryDo(msg.delete());
  25. if (!deleteResult.ok) {
  26. logger.error("Stickers: failed to delete message %s from user %s. Reason: %s", msg.id, msg.author.id, deleteResult.error);
  27. }
  28. const sendResult = await tryDo(msg.channel.send(`${msg.author.toString()} *sent a sticker:*`, {
  29. files: [this.stickers[stickerName]]
  30. }));
  31. if (!sendResult.ok) {
  32. logger.error("Stickers: failed to send sticker to channel %s. Reason: %s", msg.channel.id, sendResult.error);
  33. }
  34. data.actionsDone = true;
  35. }
  36. @Command({
  37. type: "mention",
  38. pattern: "stickers",
  39. auth: false,
  40. documentation: { description: "Lists all available stickers", example: "stickers" }
  41. })
  42. async listStickers({ message }: ICommandData): Promise<void> {
  43. const m = Object.keys(this.stickers)
  44. .filter(s => Object.prototype.hasOwnProperty.call(this.stickers, s))
  45. .reduce((prev, cur, i) => `${prev} !${cur}${i != 0 && i % STICKERS_PER_ROW == 0 ? "\n" : ""}`, "");
  46. let toSend = `${message.author.toString()}, I have the following stickers:\n\`\`\``;
  47. const rows = m.split("\n");
  48. for (let i = 0; i < rows.length; i++) {
  49. toSend += `${rows[i]}\n`;
  50. if (i != 0 && i % ROWS_PER_MESSAGE == 0) {
  51. toSend += "```";
  52. await message.channel.send(toSend);
  53. toSend = "```\n";
  54. }
  55. }
  56. toSend += "```\n";
  57. toSend += "To use stickers, simply use their name. Example: !hackermaid";
  58. await message.channel.send(toSend);
  59. }
  60. async start(): Promise<void> {
  61. const files = readdirSync(STICKERS_PATH).filter(f => !statSync(join(STICKERS_PATH, f)).isDirectory());
  62. for (const file of files)
  63. this.stickers[basename(file, extname(file)).toLowerCase()] = join(STICKERS_PATH, file);
  64. logger.info("Found %s stickers", Object.keys(this.stickers).length);
  65. }
  66. }