123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- import { isAuthorisedAsync } from "../util";
- import { ICommand } from "./command";
- import { getRepository } from "typeorm";
- import { Quote } from "../entity/Quote";
- const quotePattern = /add quote by "([^"]+)"\s*(.*)/i;
- function minify(str: string, maxLength: number) {
- let result = str.replace("\n", "");
- if (result.length > maxLength)
- result = `${result.substring(0, maxLength - 3)}...`;
- return result;
- }
- export default {
- commands: [
- {
- pattern: "add quote",
- action: async (msg, c) => {
- if (!isAuthorisedAsync(msg.member))
- return;
-
- let result = quotePattern.exec(c);
-
- if (result == null)
- return;
-
- let author = result[1].trim();
- let message = result[2].trim();
-
- let repo = getRepository(Quote);
- let newQuote = await repo.save(repo.create({
- author: author,
- message: message
- }));
-
- msg.channel.send(`${msg.author.toString()} Added quote (ID: ${newQuote.id})!`);
- }
- },
- {
- pattern: "random quote",
- action: async (msg) => {
- let repo = getRepository(Quote);
- let quotes = await repo.query(` select *
- from quote
- order by random()
- limit 1`) as Quote[];
- if (quotes.length == 0) {
- msg.channel.send("I have no quotes!");
- return;
- }
- let quote = quotes[0];
- msg.channel.send(`Quote #${quote.id}:\n*"${quote.message}"*\n- ${quote.author}`);
- }
- },
- {
- pattern: "remove quote",
- action: async (msg, c) => {
- let quoteNum = c.substring("remove quote".length).trim();
- let val = parseInt(quoteNum);
- if (isNaN(val))
- return;
- let repo = getRepository(Quote);
- let res = await repo.delete({ id: val });
- if(res.affected == 0)
- return;
- msg.channel.send(`${msg.author.toString()} Removed quote #${val}!`);
- }
- },
- {
- pattern: "quotes",
- action: async msg => {
- if (!isAuthorisedAsync(msg.member)) {
- msg.channel.send(`${msg.author.toString()} To prevent spamming, only bot moderators can view all quotes!`);
- return;
- }
- let repo = getRepository(Quote);
- let quotes = await repo.find();
-
- if (quotes.length == 0) {
- msg.channel.send("I have no quotes!");
- return;
- }
-
- let quotesListing = quotes.reduce((p, c) => `${p}[${c.id}] "${minify(c.message, 10)}" by ${c.author}\n`, "\n");
- msg.channel.send(`${msg.author.toString()}I know the following quotes:\n\`\`\`${quotesListing}\`\`\``);
- }
- }
- ],
- documentation: {
- "add quote by \"<author>\" <NEWLINE> <quote>": {
- auth: true,
- description: "Adds a quote"
- },
- "remove quote <quote_index>": {
- auth: true,
- description: "Removes quote. Use \"quotes\" to get the <quote_index>!"
- },
- "quotes": {
- auth: true,
- description: "Lists all known quotes."
- },
- "random quote": {
- auth: false,
- description: "Shows a random quote by someone special..."
- }
- }
- } as ICommand;
|