Creates a `!note <@user>` command to get, update, and delete notes for a specific user. `!note @user` gets the note. `!note @user <reason>` saves a note. `!note @user clear` remove a note.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const event = context.params.event;
const {channel_id, content} = event;
const command = '!note';
const clear = 'clear'
const mention = event.mentions?.[0]?.id;
const note = content.replace(command, '').replace(`<@!${mention}>`, '').trim();
// Is this our command?
if (!content.startsWith(command)) return;
// Does it mention someone?
if (!mention)
return lib.discord.channels['@0.2.0'].messages.create({
channel_id,
content: `Please mention a user`,
});
let key = `note_${mention}`;
// Is it the clear command? (like !note @user clear)
if (note === clear) {
await lib.utils.kv['@0.1.16'].clear({key});
return lib.discord.channels['@0.1.1'].messages.create({
channel_id,
content: 'Note cleared!'
});
}
// Is there a note?
if (note) {
// Update note
await lib.utils.kv['@0.1.16'].set({key, value: note});
return lib.discord.channels['@0.1.1'].messages.create({
channel_id,
content: 'Note updated!',
embed: {
fields: [
{ name: 'Who?', value: `<@!${mention}>` },
{ name: `Note`, value: note },
]
}
});
} else {
// Fetch note
const noteSaved = await lib.utils.kv['@0.1.16'].get({key});
const content = noteSaved ?? `No note found`
return lib.discord.channels['@0.1.1'].messages.create({
channel_id, content
});
}