quote.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { isAuthorisedAsync } from "../util";
  2. import { getRepository } from "typeorm";
  3. import { Quote } from "@shared/db/entity/Quote";
  4. import { Command, ICommandData, Plugin } from "src/model/plugin";
  5. const quotePattern = /add quote by "([^"]+)"\s*(.*)/i;
  6. @Plugin
  7. export class QuoteCommand {
  8. minify(str: string, maxLength: number): string {
  9. let result = str.replace("\n", "");
  10. if (result.length > maxLength)
  11. result = `${result.substring(0, maxLength - 3)}...`;
  12. return result;
  13. }
  14. @Command({
  15. pattern: "add quote",
  16. auth: true,
  17. documentation: {
  18. description: "Adds a quote",
  19. example: "add quote by \"<NAME>\" <NEWLINE> <QUOTE>"
  20. }
  21. })
  22. async addQuote({ message, contents }: ICommandData): Promise<void> {
  23. if (!isAuthorisedAsync(message.member))
  24. return;
  25. const result = quotePattern.exec(contents as string);
  26. if (result == null)
  27. return;
  28. const author = result[1].trim();
  29. const msg = result[2].trim();
  30. const repo = getRepository(Quote);
  31. const newQuote = await repo.save(repo.create({
  32. author: author,
  33. message: msg
  34. }));
  35. message.reply(`added quote (ID: ${newQuote.id})!`);
  36. }
  37. @Command({
  38. pattern: "random quote",
  39. documentation: {
  40. description: "Shows a random quote by someone special...",
  41. example: "random quote"
  42. }
  43. })
  44. async postRandomQuote({ message }: ICommandData): Promise<void> {
  45. const repo = getRepository(Quote);
  46. const quotes = await repo.query(` select *
  47. from quote
  48. order by random()
  49. limit 1`) as Quote[];
  50. if (quotes.length == 0) {
  51. message.reply("I have no quotes!");
  52. return;
  53. }
  54. const quote = quotes[0];
  55. message.channel.send(`Quote #${quote.id}:\n*"${quote.message}"*\n- ${quote.author}`);
  56. }
  57. @Command({
  58. pattern: "remove quote",
  59. auth: true,
  60. documentation: {
  61. description: "Removes quote. Use \"quotes\" to get the <quote_index>!",
  62. example: "remove quote <quote_index>"
  63. }
  64. })
  65. async removeQuote({ message, contents }: ICommandData): Promise<void> {
  66. const quoteNum = (contents as string).substring("remove quote".length).trim();
  67. const val = parseInt(quoteNum);
  68. if (isNaN(val))
  69. return;
  70. const repo = getRepository(Quote);
  71. const res = await repo.delete({ id: val });
  72. if (res.affected == 0)
  73. return;
  74. message.reply(`removed quote #${val}!`);
  75. }
  76. @Command({
  77. pattern: "quotes",
  78. documentation: {
  79. description: "Lists all known quotes.",
  80. example: "quotes"
  81. },
  82. auth: true
  83. })
  84. async listQuotes({ message }: ICommandData): Promise<void> {
  85. if (!isAuthorisedAsync(message.member)) {
  86. message.reply("to prevent spamming, only bot moderators can view all quotes!");
  87. return;
  88. }
  89. const repo = getRepository(Quote);
  90. const quotes = await repo.find();
  91. if (quotes.length == 0) {
  92. message.reply("I have no quotes!");
  93. return;
  94. }
  95. const quotesListing = quotes.reduce((p, c) => `${p}[${c.id}] "${this.minify(c.message, 10)}" by ${c.author}\n`, "\n");
  96. message.reply(`I know the following quotes:\n\`\`\`${quotesListing}\`\`\``);
  97. }
  98. }