|
| 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 | +} |
0 commit comments