quote.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { getRepository } from "typeorm";
  2. import { Quote } from "@shared/db/entity/Quote";
  3. import { Command, ICommandData, Plugin } from "src/model/plugin";
  4. const quotePattern = /add quote by "([^"]+)"\s*(.*)/i;
  5. @Plugin
  6. export class QuoteCommand {
  7. minify(str: string, maxLength: number): string {
  8. let result = str.replace("\n", "");
  9. if (result.length > maxLength)
  10. result = `${result.substring(0, maxLength - 3)}...`;
  11. return result;
  12. }
  13. @Command({
  14. type: "mention",
  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. const result = quotePattern.exec(contents as string);
  24. if (result == null)
  25. return;
  26. const author = result[1].trim();
  27. const msg = result[2].trim();
  28. const repo = getRepository(Quote);
  29. const newQuote = await repo.save(repo.create({
  30. author: author,
  31. message: msg
  32. }));
  33. message.reply({ content: `added quote (ID: ${newQuote.id})!`, failIfNotExists: false});
  34. }
  35. @Command({
  36. type: "mention",
  37. pattern: "random quote",
  38. documentation: {
  39. description: "Shows a random quote by someone special...",
  40. example: "random quote"
  41. }
  42. })
  43. async postRandomQuote({ message }: ICommandData): Promise<void> {
  44. const repo = getRepository(Quote);
  45. const quotes = await repo.query(` select *
  46. from quote
  47. order by random()
  48. limit 1`) as Quote[];
  49. if (quotes.length == 0) {
  50. message.reply({ content: "I have no quotes!", failIfNotExists: false });
  51. return;
  52. }
  53. const quote = quotes[0];
  54. message.channel.send(`Quote #${quote.id}:\n*"${quote.message}"*\n- ${quote.author}`);
  55. }
  56. @Command({
  57. type: "mention",
  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({ content: `removed quote #${val}!`, failIfNotExists: false });
  75. }
  76. @Command({
  77. type: "mention",
  78. pattern: "quotes",
  79. documentation: {
  80. description: "Lists all known quotes.",
  81. example: "quotes"
  82. },
  83. auth: true
  84. })
  85. async listQuotes({ message }: ICommandData): Promise<void> {
  86. const repo = getRepository(Quote);
  87. const quotes = await repo.find();
  88. if (quotes.length == 0) {
  89. message.reply({ content: "I have no quotes!", failIfNotExists: false });
  90. return;
  91. }
  92. const quotesListing = quotes.reduce((p, c) => `${p}[${c.id}] "${this.minify(c.message, 10)}" by ${c.author}\n`, "\n");
  93. message.reply({ content: `I know the following quotes:\n\`\`\`${quotesListing}\`\`\``, failIfNotExists: false });
  94. }
  95. }