Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quotes improvements #741

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 149 additions & 13 deletions packages/aquarius/src/bot/commands/quotes.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -27,8 +28,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}
`;
}
Expand Down Expand Up @@ -97,29 +104,158 @@ 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} (?<search>[^]*)$`, '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) (?<quote>[^]*)$/i,
RegExp(
`^quotes (?:new|add) ${regex.MENTION_USER.source} (?<quote>[^]*)$`,
'i'
),
async (message, { groups }) => {
log('Adding new quote');
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(?<delimeter>.)(?<search>(?:(?!\\k<delimeter>).)+)(?:\\k<delimeter>)(?<replace>.*)(?:\\k<delimeter>)(?<modifiers>[gi]+)\\s*$`,
'i'
),
async (message, { groups }) => {
log('Modifying existing quote');

const modifiers = groups.modifiers.toLowerCase();

const quoteCount = await aquarius.database.quote.count({
const mode = modifiers.includes('i') ? 'insensitive' : 'default';

const quote = await aquarius.database.quote.findFirst({
where: {
guildId: message.guild.id,
AND: [
{ guildId: message.guild.id },
{ quote: { contains: groups.search, mode } },
],
},
});

await aquarius.database.quote.create({
data: {
guildId: message.guild.id,
channel: message.channel.name,
addedBy: message.author.username,
quoteId: quoteCount + 1,
quote: groups.quote,
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 #?(?<quoteId>\\d+) s(?<delimeter>.)(?<search>(?:(?!\\k<delimeter>).)+)(?:\\k<delimeter>)(?<replace>.*)(?:\\k<delimeter>)(?<modifiers>[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,
},
},
});

message.channel.send(`Added quote #${quoteCount + 1}!`);
analytics.trackUsage('add', message);
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()
);
}
);
};
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ model Quote {
guildId String
quote String
quoteId Int
saidBy String
addedBy String
channel String
createdAt DateTime @default(now())
Expand Down