Skip to content

Commit bb972c0

Browse files
committed
Add noteedit
1 parent f6c5f36 commit bb972c0

File tree

5 files changed

+108
-5
lines changed

5 files changed

+108
-5
lines changed

src/commands/misc/noteadd.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ export default class NoteAdd extends Command {
1111
super({
1212
name,
1313
category: "Misc",
14-
help: `Create a new note, notes are per category or user.
14+
help: `Create a new note.
1515
16-
Requires "Manage Messages" permissions to add, but anyone can see.`,
16+
Notes are either per category (when used in a guild) or per user (when used in DM's).
17+
18+
Requires "Manage Messages" permissions to add/remove/edit, but anyone can see.`,
1719
usage: "noteadd <name>",
1820
aliases: ["na", "createnote", "addnote", "newnote", "an"],
1921
options: [{

src/commands/misc/noteedit.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { CommandInteraction, Message, MessageEmbed } from "discord.js"
2+
import client from "../../main"
3+
import Command from "../../utils/Command"
4+
import { CommandSource, SendMessage } from "../../utils/Types"
5+
import { Colors, getCategory, getUser, sendMessage } from "../../utils/Utils"
6+
7+
8+
export default class NoteEdit extends Command {
9+
constructor(name: string) {
10+
super({
11+
name,
12+
category: "Misc",
13+
help: `Edits a note by ID.
14+
15+
Notes are either per category (when used in a guild) or per user (when used in DM's).
16+
17+
Requires "Manage Messages" permissions to add/remove/edit, but anyone can see.`,
18+
usage: "noteedit <id>",
19+
aliases: ["editn", "editnote", "enote"],
20+
options: [{
21+
name: "id",
22+
description: "ID to edit",
23+
type: "INTEGER",
24+
required: true
25+
}, {
26+
name: "name",
27+
description: "New name",
28+
type: "STRING",
29+
required: true
30+
}]
31+
})
32+
}
33+
34+
async runInteraction(source: CommandInteraction): Promise<SendMessage | undefined> {
35+
const { options } = source
36+
37+
const id = options.getInteger("id", true)
38+
const name = options.getString("name", true)
39+
40+
return this.run(source, id, name)
41+
}
42+
43+
async runMessage(source: Message|CommandInteraction, args: string[]): Promise<SendMessage | undefined> {
44+
if (args.length < 1) return this.sendHelp(source)
45+
46+
const id = parseInt(args[0])
47+
if (isNaN(id)) return this.sendHelp(source)
48+
49+
const name = args.slice(1).join(" ")
50+
51+
return this.run(source, id, name)
52+
}
53+
54+
async run(source: CommandSource, id: number, newSubject: string): Promise<SendMessage | undefined> {
55+
const { notesManager } = client
56+
const user = getUser(source)
57+
58+
const guild = source.guild
59+
const category = getCategory(source)
60+
const name = category?.name ?? guild?.name ?? user.username
61+
62+
const guildID = guild?.id ?? "user"
63+
const categoryID = category?.id ?? guild?.id ?? user.id
64+
65+
if (source.member == undefined || typeof source.member.permissions == "string")
66+
return sendMessage(source, "Unable to check permissions.", undefined, true)
67+
else if (!source.member.permissions.has("MANAGE_MESSAGES"))
68+
return sendMessage(source, "You do not have have \"Manage Messages\" permissions in here.", undefined, true)
69+
70+
if (newSubject.length > 256) return sendMessage(source, "Note name too long")
71+
72+
const note = notesManager.getNoteById(guildID, categoryID, id)
73+
if (note == undefined) return sendMessage(source, `Could not find note #${id} in ${name}`)
74+
75+
notesManager.editNote(guildID, categoryID, id, newSubject, user.id)
76+
const reply = sendMessage(source, new MessageEmbed()
77+
.setTitle(`Edited note #${id} in ${name}`)
78+
.setColor(Colors.ORANGE)
79+
.setDescription(`Original: \`${note.subject}\`
80+
New: \`${newSubject}\``)
81+
)
82+
83+
return reply
84+
}
85+
}

src/commands/misc/noteremove.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ export default class NoteRemove extends Command {
1010
super({
1111
name,
1212
category: "Misc",
13-
help: `Removes a new note, notes are per category or user.
13+
help: `Removes a note by ID.
1414
15-
Requires "Manage Messages" permissions to add, but anyone can see.`,
15+
Notes are either per category (when used in a guild) or per user (when used in DM's).
16+
17+
Requires "Manage Messages" permissions to add/remove/edit, but anyone can see.`,
1618
usage: "noteremove <id>",
1719
aliases: ["rn", "dn", "removenote", "deletenote", "delnote", "dnote", "rnote"],
1820
options: [{

src/commands/misc/notes.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ export default class Notes extends Command {
1111
super({
1212
name,
1313
category: "Misc",
14-
help: `View notes, notes are per category or user.
14+
help: `List of notes.
15+
16+
Notes are either per category (when used in a guild) or per user (when used in DM's).
1517
1618
Requires "Manage Messages" permissions to add, but anyone can see.`,
1719
usage: "notes",

src/utils/NotesManager.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default class NotesManager {
2424
this.getNoteByCategoryStatement = this.sql.prepare("SELECT * FROM notes WHERE guild_id = @guild_id AND category_id = @category_id")
2525
this.getNoteByIdStatement = this.sql.prepare("SELECT * FROM notes WHERE guild_id = @guild_id AND category_id = @category_id AND id = @id")
2626
this.deleteNoteStatement = this.sql.prepare("DELETE FROM notes WHERE guild_id = @guild_id AND category_id = @category_id AND id = @id")
27+
this.editNoteStatement = this.sql.prepare("UPDATE notes SET subject = @subject WHERE guild_id = @guild_id AND category_id = @category_id AND id = @id")
2728
}
2829

2930
private addNotesStatement: SQLite.Statement
@@ -68,4 +69,15 @@ export default class NotesManager {
6869
})
6970
Logger.info(`Deleted note by ${userId} in ${guildId} / ${categoryId} #${id}`)
7071
}
72+
73+
private editNoteStatement: SQLite.Statement
74+
editNote(guildId: string, categoryId: string, id: number, newSubject: string, userId: string): void {
75+
this.editNoteStatement.run({
76+
guild_id: guildId,
77+
category_id: categoryId,
78+
id,
79+
subject: newSubject
80+
})
81+
Logger.info(`Edited note by ${userId} in ${guildId} / ${categoryId} #${id}: ${newSubject}`)
82+
}
7183
}

0 commit comments

Comments
 (0)