util.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { GuildMember } from "discord.js";
  2. import { DocumentationSet } from "./commands/command";
  3. import { getRepository } from "typeorm";
  4. import { KnownUser } from "./entity/KnownUser";
  5. const VALID_EXTENSIONS = new Set([
  6. "png",
  7. "jpg",
  8. "jpeg",
  9. "bmp",
  10. ]);
  11. export let documentation : DocumentationSet = {};
  12. export function isDevelopment() {
  13. return process.env.NODE_ENV == "dev";
  14. }
  15. export function isValidImage(fileName: string) {
  16. let extPosition = fileName.lastIndexOf(".");
  17. if(extPosition < 0)
  18. return false;
  19. let ext = fileName.substring(extPosition + 1).toLowerCase();
  20. return VALID_EXTENSIONS.has(ext);
  21. }
  22. export async function isAuthorisedAsync(member : GuildMember) {
  23. let repo = getRepository(KnownUser);
  24. let user = await repo.findOne({
  25. where: { userID: member.id },
  26. select: [ "canModerate" ]
  27. });
  28. if (user && user.canModerate)
  29. return true;
  30. let role = await repo.createQueryBuilder()
  31. .select(["userId"])
  32. .where("userId in (:...ids)", {ids: member.roles.keyArray()})
  33. .andWhere("canModerate = 1")
  34. .getOne();
  35. if (role)
  36. return true;
  37. return false;
  38. }
  39. export type Dict<TVal> = { [key: string]: TVal };