file_only_channel_checker.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { CommandSet, Action, ActionType, Command } from "src/model/command";
  2. import { Message, TextChannel } from "discord.js";
  3. import { getRepository } from "typeorm";
  4. import { FileOnlyChannel } from "@shared/db/entity/FileOnlyChannel";
  5. @CommandSet
  6. export class FileOnlyChannelChecker {
  7. private urlPattern: RegExp = new RegExp("(?:((?:https?|ftp):\/\/)|ww)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?");
  8. @Action(ActionType.MESSAGE)
  9. async processMessage(actionsDone: boolean, msg: Message, content: string) {
  10. if (actionsDone)
  11. return false;
  12. let repo = getRepository(FileOnlyChannel);
  13. let entry = await repo.findOne(msg.channel.id);
  14. if (!entry)
  15. return false;
  16. // Has attachments; is fine
  17. if (msg.attachments.size > 0)
  18. return false;
  19. // Has a link
  20. if(this.urlPattern.test(msg.content))
  21. return false;
  22. msg.delete();
  23. let ch = msg.guild?.channels.resolve(entry.warningMessageChannelId);
  24. if(ch instanceof TextChannel)
  25. ch.send(`> ${msg.content.replace(/\n/g, "\n> ")}\n\n${msg.author.toString()} Channel ${msg.channel.toString()} is only meant for sharing links and files! If you want to post text-only messages, do so in ${ch.toString()}!`);
  26. return true;
  27. }
  28. };