util.ts 1.3 KB

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