Refactor to more consistent structure (e.g. "plugins")
This commit is contained in:
92
plugins/nag/nag.ts
Normal file
92
plugins/nag/nag.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import {Chrono} from 'chrono-node';
|
||||
import {
|
||||
type ChatInputCommandInteraction,
|
||||
SlashCommandBuilder,
|
||||
} from 'discord.js';
|
||||
import type { Settings } from '../../plugin'
|
||||
import {CheckIn, Nag} from './models';
|
||||
|
||||
async function execute(interaction: ChatInputCommandInteraction) {
|
||||
const text = interaction.options.getString('text');
|
||||
if (text === null || text === undefined) {
|
||||
await interaction.reply("Nag can't have a blank `text`, try again.");
|
||||
return;
|
||||
}
|
||||
// Check if we already have an existing nag. In theory, this should be supported entirely, however
|
||||
// I want to keep things simple for now.
|
||||
const existingNags = await Nag.findAll({
|
||||
where: {
|
||||
userId: interaction.user.id,
|
||||
},
|
||||
// order: [["createdAt", "ASC"]],
|
||||
});
|
||||
console.log('Successfully looked for checkIns');
|
||||
if (existingNags && existingNags.length > 0) {
|
||||
// TODO: Hmm... For now, I guess we can just update the database.
|
||||
for (const nag of existingNags) {
|
||||
nag.text = text;
|
||||
nag.failText = interaction.options.getString('failtext') ?? undefined;
|
||||
await nag.save();
|
||||
break;
|
||||
}
|
||||
await interaction.reply(
|
||||
`I'll check every day at 9AM if you've completed '${text}'. If not, I'll nag you! Use /checkin to prevent a shameful callout, and /unnag to cancel.`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
// Otherwise, we need to create a new nag.
|
||||
const nag = await Nag.create({
|
||||
userId: interaction.user.id,
|
||||
guildId: interaction.guild?.id,
|
||||
channelId: interaction.channel?.id,
|
||||
messageId: interaction.id,
|
||||
text: text,
|
||||
failText: interaction.options.getString('failtext'),
|
||||
mentionHere: interaction.options.getBoolean('mentionhere') ?? false,
|
||||
});
|
||||
await nag.save();
|
||||
const chrono = new Chrono();
|
||||
const checkIn = chrono.parseDate('today at 9AM');
|
||||
if (!checkIn) {
|
||||
await interaction.reply(
|
||||
'Internal error while saving your nag. Tell Drew the bot is broken!!!',
|
||||
);
|
||||
return;
|
||||
}
|
||||
await CheckIn.create({
|
||||
nag: {
|
||||
id: nag.id,
|
||||
},
|
||||
lastCheckIn: new Date(Date.now()),
|
||||
});
|
||||
await interaction.reply(
|
||||
`I'll check every day at 9AM if you've completed '${text}'. If not, I'll nag you! Use /checkin to prevent a shameful callout, and /unnag to cancel.`,
|
||||
);
|
||||
}
|
||||
|
||||
export default function (_settings: Settings) {
|
||||
return {
|
||||
data: new SlashCommandBuilder()
|
||||
.setName('nag')
|
||||
.setDescription('Let Blitzcrank nag you every day about something')
|
||||
.addStringOption(option =>
|
||||
option
|
||||
.setRequired(true)
|
||||
.setName('text')
|
||||
.setDescription('What you have to do every day'),
|
||||
)
|
||||
.addStringOption(option =>
|
||||
option
|
||||
.setName('failtext')
|
||||
.setDescription('Custom message to be broadcast on failure')
|
||||
.setRequired(false),
|
||||
)
|
||||
.addBooleanOption(option =>
|
||||
option
|
||||
.setName('mentionhere')
|
||||
.setDescription('Whether to DM you or @ this channel')
|
||||
.setRequired(false),
|
||||
),
|
||||
execute,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user