From a3ff00411b2d54e38cf0277b1dd8109c0d8abaf1 Mon Sep 17 00:00:00 2001 From: rcombs Date: Sat, 19 Dec 2020 01:50:48 -0600 Subject: [PATCH 1/4] commands/quotes: add saidBy to database --- packages/aquarius/src/bot/commands/quotes.js | 8 +++++++- prisma/schema.prisma | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/aquarius/src/bot/commands/quotes.js b/packages/aquarius/src/bot/commands/quotes.js index 9d34f817..5cac9a3e 100644 --- a/packages/aquarius/src/bot/commands/quotes.js +++ b/packages/aquarius/src/bot/commands/quotes.js @@ -27,8 +27,14 @@ export const info = { function getQuoteMessage(quote) { const time = formatDistance(quote.createdAt, new Date(), { addSuffix: true }); + if (!quote.saidBy) + return dedent` + *Quote ${quote.quoteId} added by ${quote.addedBy} ${time}* + ${quote.quote} + `; + return dedent` - *Quote ${quote.quoteId} added by ${quote.addedBy} ${time}* + *Quote ${quote.quoteId} from <@${quote.saidBy}> added by ${quote.addedBy} ${time}* ${quote.quote} `; } diff --git a/prisma/schema.prisma b/prisma/schema.prisma index c5eefc3e..141bfbac 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -75,6 +75,7 @@ model Quote { guildId String quote String quoteId Int + saidBy String addedBy String channel String createdAt DateTime @default(now()) From cbfa933d4f53ba6f2e18ce9d4210a4b618ae8ad9 Mon Sep 17 00:00:00 2001 From: rcombs Date: Sat, 19 Dec 2020 01:51:51 -0600 Subject: [PATCH 2/4] commands/quotes: support adding quotes by search of recent messages --- packages/aquarius/src/bot/commands/quotes.js | 52 ++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/packages/aquarius/src/bot/commands/quotes.js b/packages/aquarius/src/bot/commands/quotes.js index 5cac9a3e..47adf9e8 100644 --- a/packages/aquarius/src/bot/commands/quotes.js +++ b/packages/aquarius/src/bot/commands/quotes.js @@ -1,6 +1,7 @@ import dateFns from 'date-fns'; import debug from 'debug'; import dedent from 'dedent-js'; +import * as regex from '@aquarius-bot/regex'; import { getInputAsNumber } from '../../core/helpers/input'; // CJS / ESM compatibility @@ -103,6 +104,57 @@ export default async ({ aquarius, analytics }) => { } ); + async function addQuote(message, text, saidBy) { + const quoteCount = await aquarius.database.quote.count({ + where: { + guildId: message.guild.id, + }, + }); + + await aquarius.database.quote.create({ + data: { + guildId: message.guild.id, + channel: message.channel.id, + addedBy: message.author.username, + saidBy, + quoteId: quoteCount + 1, + quote: text, + }, + }); + + message.channel.send(`Added quote #${quoteCount + 1}!`); + analytics.trackUsage('add', message); + } + + aquarius.onCommand( + RegExp(`^quote ${regex.MENTION_USER.source} (?[^]*)$`, 'i'), + async (message, { groups }) => { + log('Adding new quote by search'); + + const search = groups.search.toLowerCase(); + + const history = await message.channel.fetch({ before: message.id }); + const result = history.reduce((best, cur) => { + if ( + cur.author.id === groups.id && + (!best || cur.createdAt > best.createdAt) && + cur.cleanContent.toLowerCase().contains(search) + ) + return cur; + return best; + }); + + if (!result) { + message.channel.send( + `Sorry, I don't remember what <@${groups.id}> said about ${groups.search}.` + ); + return; + } + + addQuote(message, groups.id, result.cleanContent); + } + ); + aquarius.onCommand( /^quotes (?:new|add) (?[^]*)$/i, async (message, { groups }) => { From abede175e7491ffead8ac1798062e6268e64b1cf Mon Sep 17 00:00:00 2001 From: rcombs Date: Sat, 19 Dec 2020 01:53:56 -0600 Subject: [PATCH 3/4] commands/quotes: take a saidBy arg in add/new --- packages/aquarius/src/bot/commands/quotes.js | 25 ++++---------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/packages/aquarius/src/bot/commands/quotes.js b/packages/aquarius/src/bot/commands/quotes.js index 47adf9e8..53543b76 100644 --- a/packages/aquarius/src/bot/commands/quotes.js +++ b/packages/aquarius/src/bot/commands/quotes.js @@ -156,28 +156,13 @@ export default async ({ aquarius, analytics }) => { ); aquarius.onCommand( - /^quotes (?:new|add) (?[^]*)$/i, + RegExp( + `^quotes (?:new|add) ${regex.MENTION_USER.source} (?[^]*)$`, + 'i' + ), async (message, { groups }) => { log('Adding new quote'); - - const quoteCount = await aquarius.database.quote.count({ - where: { - guildId: message.guild.id, - }, - }); - - await aquarius.database.quote.create({ - data: { - guildId: message.guild.id, - channel: message.channel.name, - addedBy: message.author.username, - quoteId: quoteCount + 1, - quote: groups.quote, - }, - }); - - message.channel.send(`Added quote #${quoteCount + 1}!`); - analytics.trackUsage('add', message); + addQuote(message, groups.quote, groups.id); } ); }; From 90cb53b5f8c15983bc4959ac59756c1e2a3c0ee6 Mon Sep 17 00:00:00 2001 From: rcombs Date: Sat, 19 Dec 2020 01:54:13 -0600 Subject: [PATCH 4/4] commands/quotes: support quote editing --- packages/aquarius/src/bot/commands/quotes.js | 93 ++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/packages/aquarius/src/bot/commands/quotes.js b/packages/aquarius/src/bot/commands/quotes.js index 53543b76..9a2adc1a 100644 --- a/packages/aquarius/src/bot/commands/quotes.js +++ b/packages/aquarius/src/bot/commands/quotes.js @@ -165,4 +165,97 @@ export default async ({ aquarius, analytics }) => { addQuote(message, groups.quote, groups.id); } ); + + async function editQuote(message, quote, search, replace, modifiers) { + // TODO fix case-insensitive mode + const edited = quote.quote[ + modifiers.includes('g') ? 'replaceAll' : 'replace' + ](search, replace); + + await aquarius.database.quote.update({ + select: { + guildId_quoteId: { + quoteId: quote.quoteId, + guildId: quote.guildId, + }, + }, + data: { + quote: edited, + }, + }); + + message.channel.send(`Edited quote #${quote.quoteId}!`); + analytics.trackUsage('edit', message); + } + + aquarius.onCommand( + RegExp( + `^quotes edit ${regex.MENTION_USER.source} s(?.)(?(?:(?!\\k).)+)(?:\\k)(?.*)(?:\\k)(?[gi]+)\\s*$`, + 'i' + ), + async (message, { groups }) => { + log('Modifying existing quote'); + + const modifiers = groups.modifiers.toLowerCase(); + + const mode = modifiers.includes('i') ? 'insensitive' : 'default'; + + const quote = await aquarius.database.quote.findFirst({ + where: { + AND: [ + { guildId: message.guild.id }, + { quote: { contains: groups.search, mode } }, + ], + }, + }); + + if (!quote) { + message.channel.send( + `Sorry, I don't remember what <@${groups.id}> said about ${groups.search}.` + ); + return; + } + + editQuote(message, quote, groups.search, groups.replace, modifiers); + } + ); + + aquarius.onCommand( + RegExp( + `^quotes edit #?(?\\d+) s(?.)(?(?:(?!\\k).)+)(?:\\k)(?.*)(?:\\k)(?[gi]+)\\s*$`, + 'i' + ), + async (message, { groups }) => { + log('Modifying existing quote'); + + const quoteId = getInputAsNumber(groups.quoteId); + + if (!quoteId) { + message.channel.send("Sorry, it looks like that isn't a valid ID!"); + return; + } + + const quote = await aquarius.database.quote.findOne({ + where: { + guildId_quoteId: { + quoteId, + guildId: message.guild.id, + }, + }, + }); + + if (!quote) { + message.channel.send(`Sorry, I don't have a quote with ID ${quoteId}.`); + return; + } + + editQuote( + message, + quote, + groups.search, + groups.replace, + groups.modifiers.toLowerCase() + ); + } + ); };