quote.ts 3.8 KB

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