|
| 1 | +import { CommandInteraction, Message, MessageEmbed } from "discord.js" |
| 2 | +import config from "../../data/config.json" |
| 3 | +import client from "../../main" |
| 4 | +import Command from "../../utils/Command" |
| 5 | +import { CommandSource, SendMessage } from "../../utils/Types" |
| 6 | +import { Colors, getCategory, getUser, sendMessage } from "../../utils/Utils" |
| 7 | + |
| 8 | + |
| 9 | +export default class NoteAdd extends Command { |
| 10 | + constructor(name: string) { |
| 11 | + super({ |
| 12 | + name, |
| 13 | + category: "Misc", |
| 14 | + help: `Create a new note, notes are per category or user. |
| 15 | +
|
| 16 | +Requires "Manage Messages" permissions to add, but anyone can see.`, |
| 17 | + usage: "noteadd <name>", |
| 18 | + aliases: ["na", "createnote", "addnote", "newnote", "an"], |
| 19 | + options: [{ |
| 20 | + name: "name", |
| 21 | + description: "Name to show", |
| 22 | + type: "STRING", |
| 23 | + required: true |
| 24 | + }] |
| 25 | + }) |
| 26 | + } |
| 27 | + |
| 28 | + async runInteraction(source: CommandInteraction): Promise<SendMessage | undefined> { |
| 29 | + const { options } = source |
| 30 | + |
| 31 | + const name = options.getString("name", true) |
| 32 | + |
| 33 | + return this.run(source, name) |
| 34 | + } |
| 35 | + |
| 36 | + async runMessage(source: Message|CommandInteraction, args: string[]): Promise<SendMessage | undefined> { |
| 37 | + if (args.length < 1) return this.sendHelp(source) |
| 38 | + |
| 39 | + const name = args.join(" ") |
| 40 | + |
| 41 | + return this.run(source, name) |
| 42 | + } |
| 43 | + |
| 44 | + async run(source: CommandSource, subject: string): Promise<SendMessage | undefined> { |
| 45 | + const { notesManager } = client |
| 46 | + const user = getUser(source) |
| 47 | + |
| 48 | + const guild = source.guild |
| 49 | + const category = getCategory(source) |
| 50 | + const name = category?.name ?? guild?.name ?? user.username |
| 51 | + |
| 52 | + const guildID = guild?.id ?? "user" |
| 53 | + const categoryID = category?.id ?? guild?.id ?? user.id |
| 54 | + |
| 55 | + if (guild) |
| 56 | + if (source.member == undefined || typeof source.member.permissions == "string") |
| 57 | + return sendMessage(source, "Unable to check permissions.", undefined, true) |
| 58 | + else if (!source.member.permissions.has("MANAGE_MESSAGES")) |
| 59 | + return sendMessage(source, "You do not have have \"Manage Messages\" permissions in here.", undefined, true) |
| 60 | + |
| 61 | + const notes = notesManager.getNotes(guildID, categoryID) |
| 62 | + if (notes.length >= 50) return sendMessage(source, `You can only have up to 50 notes in ${name}, see \`${config.prefix}notes\` for which you have`) |
| 63 | + |
| 64 | + if (subject.length > 256) return sendMessage(source, "Note name too long") |
| 65 | + |
| 66 | + |
| 67 | + let id = 1 |
| 68 | + while (notes.some(r => r.id == id) || notesManager.getNoteById(guildID, categoryID, id)) |
| 69 | + id++ |
| 70 | + |
| 71 | + notesManager.addNote(guildID, categoryID, id, subject, user.id) |
| 72 | + const reply = sendMessage(source, new MessageEmbed() |
| 73 | + .setTitle(`Created note #${id} in ${name}`) |
| 74 | + .setColor(Colors.GREEN) |
| 75 | + .setDescription(`The note #${id}: \`${subject}\` has been created`) |
| 76 | + ) |
| 77 | + |
| 78 | + return reply |
| 79 | + } |
| 80 | +} |
0 commit comments