Skip to content

Commit

Permalink
add shared notes system
Browse files Browse the repository at this point in the history
  • Loading branch information
Enriath committed Feb 27, 2022
1 parent 3351e71 commit 15a237b
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ db.json
gh_users.json
filtered_words.json
roles.json
notes.json
20 changes: 20 additions & 0 deletions lib/commands/notemenu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ContextMenuCommandBuilder } from '@discordjs/builders'
import notes from '../notes.js'
import { ApplicationCommandType } from 'discord-api-types/v9'

export default {
ephemeral: true,
data: new ContextMenuCommandBuilder()
.setName('Get stored note for user')
.setDefaultPermission(true)
.setType(ApplicationCommandType.User),
permissions: ['moderator', 'helper'],
async execute (interaction) {
const guild = interaction.guild
const member = await guild.members.fetch(interaction.targetId)
if (!member) {
return
}
return interaction.followUp(notes.getNote(member.user))
}
}
52 changes: 52 additions & 0 deletions lib/commands/notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { SlashCommandBuilder } from '@discordjs/builders'
import notes from '../notes.js'

export default {
ephemeral: true,
data: new SlashCommandBuilder()
.setName('note')
.setDescription('Manage shared notes for users')
.addSubcommand(sub => sub
.setName('get')
.setDescription('Get the shared note for a user')
.addUserOption(option => option
.setName('user')
.setDescription('The user to get the note for')
.setRequired(true))
)
.addSubcommand(sub => sub
.setName('set')
.setDescription('Set the shared note for a user')
.addUserOption(option => option
.setName('user')
.setDescription('The user to set the note for')
.setRequired(true))
.addStringOption(option => option
.setName('value')
.setDescription('New note for the user')
.setRequired(true))
)
.addSubcommand(sub => sub
.setName('del')
.setDescription('Delete the shared note for a user')
.addUserOption(option => option
.setName('user')
.setDescription('The user to delete the note from')
.setRequired(true))
),
permissions: ['moderator', 'helper'],
async execute (interaction) {
const user = interaction.options.getUser('user')

switch (interaction.options.getSubcommand()) {
case 'get':
return interaction.followUp(notes.getNote(user))
case 'set': {
const value = interaction.options.getString('value')
return interaction.followUp(await notes.setNote(user, value, interaction.guild, interaction.user.tag))
}
case 'del':
return interaction.followUp(await notes.setNote(user, null, interaction.guild, interaction.user.tag))
}
}
}
4 changes: 3 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export default {
// Path to database holding list of filtered words
filteredWords: './filtered_words.json',
// Path to database holding list of persisted roles for users
roles: './roles.json'
roles: './roles.json',
// Path to database for holding shared user notes
notes: './notes.json'
},
// GitHub API token used to avoid rejection from GitHub API when request limit is reached
githubToken: process.env.GITHUB_TOKEN,
Expand Down
4 changes: 3 additions & 1 deletion lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ const commandsDb = lDB(config.databases.commands)
const githubUserDb = lDB(config.databases.githubUsers)
const filteredWordsDb = lDB(config.databases.filteredWords)
const roleDb = lDB(config.databases.roles)
const noteDb = lDB(config.databases.notes)

export {
commandsDb,
githubUserDb,
filteredWordsDb,
roleDb
roleDb,
noteDb
}
42 changes: 42 additions & 0 deletions lib/notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { noteDb } from './db.js'
import { buildUserDetail } from './security.js'
import config from './config.js'

function getNote (user) {
const note = noteDb.get(user.id)

if (!note) {
return `No stored note for ${buildUserDetail(user)}`
} else {
return `Stored note for ${buildUserDetail(user)}:\n\`${note}\``
}
}

async function setNote (user, value, guild, setter) {
const oldNote = noteDb.get(user.id)
if (!oldNote && !value) {
return 'There was no note to remove'
}
noteDb.put(user.id, value)

let message

if (!oldNote) {
message = `Set note for ${buildUserDetail(user)} to:\n\`${value}\``
} else if (!value) {
message = `Deleted note for ${buildUserDetail(user)}:\n\`${oldNote}\``
} else {
message = `Updated note for ${buildUserDetail(user)}:\nOld: \`${oldNote}\`\nNew: \`${value}\``
}

const logs = guild.channels.cache.find(c => c.name === config.channels.moderationLogs)
if (logs) {
await logs.send(`:pencil2: **${setter}** ${message}`)
}
return message
}

export default {
getNote,
setNote
}

0 comments on commit 15a237b

Please sign in to comment.