diff --git a/commands/anime/abrazar.ts b/commands/anime/abrazar.ts index f6468c6..dd9b92e 100644 --- a/commands/anime/abrazar.ts +++ b/commands/anime/abrazar.ts @@ -1,7 +1,7 @@ import { commandModule, CommandType } from '@sern/handler'; import axios from 'axios'; import { ActionRowBuilder, ApplicationCommandOptionType, ButtonBuilder, ButtonStyle, ComponentType } from 'discord.js'; -import { publish } from '../../src/plugins/publish.js'; +import { publish } from '../../plugins/publish.js'; export default commandModule({ type: CommandType.Slash, diff --git a/commands/anime/cuddle.ts b/commands/anime/cuddle.ts index 98b8ce5..ded5f75 100644 --- a/commands/anime/cuddle.ts +++ b/commands/anime/cuddle.ts @@ -1,7 +1,7 @@ import { commandModule, CommandType } from '@sern/handler'; import axios from 'axios'; import { ActionRowBuilder, ApplicationCommandOptionType, ButtonBuilder, ButtonStyle, ComponentType } from 'discord.js'; -import { publish } from '../../src/plugins/publish.js'; +import { publish } from '../../plugins/publish.js'; export default commandModule({ type: CommandType.Slash, diff --git a/commands/anime/oppai.ts b/commands/anime/oppai.ts index 3e03783..e420856 100644 --- a/commands/anime/oppai.ts +++ b/commands/anime/oppai.ts @@ -1,7 +1,7 @@ import { commandModule, CommandType } from '@sern/handler'; import axios from 'axios'; import { ActionRowBuilder, ButtonBuilder, ButtonStyle, ComponentType } from 'discord.js'; -import { publish } from '../../src/plugins/publish.js'; +import { publish } from '../../plugins/publish.js'; export default commandModule({ type: CommandType.Slash, diff --git a/commands/anime/selfies.ts b/commands/anime/selfies.ts index 2ea3ece..d29af60 100644 --- a/commands/anime/selfies.ts +++ b/commands/anime/selfies.ts @@ -1,7 +1,7 @@ import { commandModule, CommandType } from '@sern/handler'; import axios from 'axios'; import { ActionRowBuilder, ButtonBuilder, ButtonStyle, ComponentType } from 'discord.js'; -import { publish } from '../../src/plugins/publish.js'; +import { publish } from '../../plugins/publish.js'; export default commandModule({ type: CommandType.Slash, diff --git a/commands/anime/waifu.ts b/commands/anime/waifu.ts index 13a4413..eeee567 100644 --- a/commands/anime/waifu.ts +++ b/commands/anime/waifu.ts @@ -1,7 +1,7 @@ import { commandModule, CommandType } from '@sern/handler'; import axios from 'axios'; import { ActionRowBuilder, ButtonBuilder, ButtonStyle, ComponentType } from 'discord.js'; -import { publish } from '../../src/plugins/publish.js'; +import { publish } from '../../plugins/publish.js'; export default commandModule({ type: CommandType.Both, diff --git a/commands/menumsg/Highlight.ts b/commands/menumsg/Highlight.ts index 594df14..7dc14ad 100644 --- a/commands/menumsg/Highlight.ts +++ b/commands/menumsg/Highlight.ts @@ -1,10 +1,10 @@ import { commandModule, CommandType } from "@sern/handler"; -import { publish } from "../../src/plugins/publish.js"; +import { publish } from "../../plugins/publish.js"; import db from "../../schemas/highlights.js"; import { Model } from "mongoose"; export default commandModule({ - type: CommandType.MenuMsg, + type: CommandType.CtxMsg, plugins: [publish()], // alias : [], execute: async (ctx) => { diff --git a/commands/menumsg/highlight lowercase.ts b/commands/menumsg/highlight lowercase.ts deleted file mode 100644 index c5b0405..0000000 --- a/commands/menumsg/highlight lowercase.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { commandModule, CommandType } from "@sern/handler"; -import { publish } from "../../src/plugins/publish.js"; -import db from "../../schemas/highlights.js"; -import { Model } from "mongoose"; - -export default commandModule({ - name: 'highlight', - type: CommandType.MenuMsg, - plugins: [publish()], - // alias : [], - execute: async (ctx) => { - await ctx.deferReply(); - const author = ctx.targetMessage.author; - const message = ctx.targetMessage.id; - db.countDocuments( - { msgid: message }, - async function (err: Error, count: number) { - if (err) throw err - if (count > 0) { - return await ctx.editReply({ - content: "Este mensaje ya ha sido guardado en la base de datos", - }); - } else { - const save = new db({ - authorid: author.id, - channelid: ctx.channel!.id, - msgid: message, - }); - await save.save(); - await ctx.editReply({ - content: `El mensaje de ${author} con ID \`${message}\` ha sido guardado correctamente en la base de datos!`, - allowedMentions: { repliedUser: false } - }); - } - } - ); - }, -}); diff --git a/commands/misc/highlight.ts b/commands/misc/highlight.ts index fa9308f..f89e4de 100644 --- a/commands/misc/highlight.ts +++ b/commands/misc/highlight.ts @@ -1,5 +1,5 @@ import { commandModule, CommandType } from '@sern/handler'; -import { publish } from '../../src/plugins/publish.js'; +import { publish } from '../../plugins/publish.js'; import db from "../../schemas/highlights.js"; import { time } from 'discord.js'; diff --git a/events/djs/ready.ts b/events/djs/ready.ts index 0d9ee22..649437c 100644 --- a/events/djs/ready.ts +++ b/events/djs/ready.ts @@ -1,8 +1,9 @@ import { EventType, eventModule } from "@sern/handler"; +import { Client } from "discord.js"; export default eventModule({ type: EventType.Discord, - execute () { - console.log('el bot c ha ensendio') + execute: async (client: Client) => { + console.log('el bot c ha ensendio'); }, }); \ No newline at end of file diff --git a/index.ts b/index.ts index 4a3e401..559ceb0 100644 --- a/index.ts +++ b/index.ts @@ -1,5 +1,5 @@ import { Client, GatewayIntentBits } from 'discord.js'; -import { Sern, SernEmitter } from '@sern/handler'; +import { DefaultLogging, Dependencies, Sern, SernEmitter, single, Singleton } from '@sern/handler'; import 'dotenv/config' import mongoose from 'mongoose'; @@ -11,11 +11,21 @@ const client = new Client({ ], }); +interface MyDependencies extends Dependencies { + '@sern/client' : Singleton; + '@sern/logger' : Singleton +} +export const useContainer = Sern.makeDependencies({ + build: root => root + .add({ '@sern/client': single(client) }) + .add({ '@sern/logger': single(new DefaultLogging()) }) +}); Sern.init({ - client, commands: './dist/commands/', events: './dist/events/', - sernEmitter: new SernEmitter() + containerConfig: { + get: useContainer + } }); mongoose.connect(process.env.MONGODB!) diff --git a/package-lock.json b/package-lock.json index 9eac5ce..b8a43a9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "1.0.0", "license": "UNLICENSED", "dependencies": { - "@sern/handler": "^1.0.0", + "@sern/handler": "^2.0.0", "axios": "^0.27.2", "discord.js": "^14.2.0", "dotenv": "^16.0.3", @@ -1137,16 +1137,14 @@ } }, "node_modules/@sern/handler": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/@sern/handler/-/handler-1.0.1.tgz", - "integrity": "sha512-+bwGMXF/QeCYxSmf1jtn22/7BZ5+BoSZS464HDzkCWdIBU1jaav38gDo17I9zUJl4fsukXgx9TMFqH1V69Xozw==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@sern/handler/-/handler-2.0.0.tgz", + "integrity": "sha512-i9BbUxwOYX1rn1FUfVFT+c8ltAloEfIMUznOt2T4h1xbWyXm/aKocmcgt5DkBHRU8yGoCfDiFF3E3I1mHln3UQ==", "dependencies": { + "iti": "^0.5.0", "rxjs": "^7.5.6", "ts-pattern": "^4.0.2", "ts-results-es": "^3.5.0" - }, - "peerDependencies": { - "discord.js": "^14.2.x" } }, "node_modules/@tokenizer/token": { @@ -1490,6 +1488,17 @@ "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", "dev": true }, + "node_modules/iti": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/iti/-/iti-0.5.0.tgz", + "integrity": "sha512-ZiwdEIhXAxyb6/+j2didONRa3q73y9h8oHMRXAIkXv7k851cm8H7fyZZyszr514TdzgoVQl7Z6hvsdTogjgc0w==", + "dependencies": { + "utility-types": "^3.10.0" + }, + "engines": { + "node": ">=12" + } + }, "node_modules/kareem": { "version": "2.4.1", "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.4.1.tgz", @@ -1964,6 +1973,14 @@ "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" }, + "node_modules/utility-types": { + "version": "3.10.0", + "resolved": "https://registry.npmjs.org/utility-types/-/utility-types-3.10.0.tgz", + "integrity": "sha512-O11mqxmi7wMKCo6HKFt5AhO4BwY3VV68YU07tgxfz8zJTIxr4BpsezN49Ffwy9j3ZpwwJp4fkRwjRzq3uWE6Rg==", + "engines": { + "node": ">= 4" + } + }, "node_modules/uuid": { "version": "8.3.2", "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", @@ -2968,10 +2985,11 @@ "integrity": "sha512-ula2O0kpSZtX9rKXNeQMrHwNd7E4jPDJYUXmEGTFdMRfyfMw+FPyh04oKMjAiDuOi64bYgVkOV3MjK+loImFhQ==" }, "@sern/handler": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/@sern/handler/-/handler-1.0.1.tgz", - "integrity": "sha512-+bwGMXF/QeCYxSmf1jtn22/7BZ5+BoSZS464HDzkCWdIBU1jaav38gDo17I9zUJl4fsukXgx9TMFqH1V69Xozw==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@sern/handler/-/handler-2.0.0.tgz", + "integrity": "sha512-i9BbUxwOYX1rn1FUfVFT+c8ltAloEfIMUznOt2T4h1xbWyXm/aKocmcgt5DkBHRU8yGoCfDiFF3E3I1mHln3UQ==", "requires": { + "iti": "^0.5.0", "rxjs": "^7.5.6", "ts-pattern": "^4.0.2", "ts-results-es": "^3.5.0" @@ -3216,6 +3234,14 @@ "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", "dev": true }, + "iti": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/iti/-/iti-0.5.0.tgz", + "integrity": "sha512-ZiwdEIhXAxyb6/+j2didONRa3q73y9h8oHMRXAIkXv7k851cm8H7fyZZyszr514TdzgoVQl7Z6hvsdTogjgc0w==", + "requires": { + "utility-types": "^3.10.0" + } + }, "kareem": { "version": "2.4.1", "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.4.1.tgz", @@ -3561,6 +3587,11 @@ "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" }, + "utility-types": { + "version": "3.10.0", + "resolved": "https://registry.npmjs.org/utility-types/-/utility-types-3.10.0.tgz", + "integrity": "sha512-O11mqxmi7wMKCo6HKFt5AhO4BwY3VV68YU07tgxfz8zJTIxr4BpsezN49Ffwy9j3ZpwwJp4fkRwjRzq3uWE6Rg==" + }, "uuid": { "version": "8.3.2", "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", diff --git a/package.json b/package.json index cb7b71a..d849968 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ ], "license": "UNLICENSED", "dependencies": { - "@sern/handler": "^1.0.0", + "@sern/handler": "^2.0.0", "axios": "^0.27.2", "discord.js": "^14.2.0", "dotenv": "^16.0.3", diff --git a/src/plugins/nsfwOnly.ts b/plugins/nsfwOnly.ts similarity index 100% rename from src/plugins/nsfwOnly.ts rename to plugins/nsfwOnly.ts diff --git a/src/plugins/publish.ts b/plugins/publish.ts similarity index 61% rename from src/plugins/publish.ts rename to plugins/publish.ts index da282cd..95a4a97 100644 --- a/src/plugins/publish.ts +++ b/plugins/publish.ts @@ -1,9 +1,9 @@ // @ts-nocheck /** - * This is publish plugin, it allows you to publish your application commands with ease. + * This is publish plugin, it allows you to publish your application commands using the discord.js library with ease. * * @author @EvolutionX-10 [<@697795666373640213>] - * @version 1.3.0 + * @version 2.0.0 * @example * ```ts * import { publish } from "../plugins/publish"; @@ -29,20 +29,27 @@ import { ApplicationCommandType, PermissionResolvable, } from "discord.js"; +/** + * This is the dependency getter that is created from Sern.makeDependencies. + * import it here so that this plugin has access to your bot's dependencies + */ +import { useContainer } from "../index.js"; export function publish( options?: PublishOptions ): CommandPlugin< | CommandType.Slash | CommandType.Both - | CommandType.MenuMsg - | CommandType.MenuUser + | CommandType.CtxUser + | CommandType.CtxMsg > { return { type: PluginType.Command, - description: "Manage Slash Commands", + description: "Manage Application Commands", name: "slash-auto-publish", - async execute({ client }, { mod: module }, controller) { + async execute({ mod: module }, controller) { + // Users need to provide their own useContainer function. + const [client] = useContainer("@sern/client"); const defaultOptions = { guildIds: [], dmPermission: undefined, @@ -55,55 +62,69 @@ export function publish( options as unknown as ValidPublishOptions; function c(e: unknown) { - console.error("publish command didnt work for", module.name!); + console.error("publish command didnt work for", module.name); console.error(e); } - try { - const commandData = { - type: CommandTypeRaw[module.type], - name: module.name!, - description: [CommandType.Slash, CommandType.Both].includes( - module.type - ) - ? module.description - : undefined, - options: [CommandType.Slash, CommandType.Both].includes( - module.type - ) - ? optionsTransformer( - (module as SlashCommand).options ?? [] - ) - : [], + const log = + (...message: any[]) => + () => + console.log(...message); + const logged = (...message: any[]) => log(message); + /** + * a local function that returns either one value or the other, + * depending on {t}'s CommandType. If the commandtype of + * this module is CommandType.Both or CommandType.Text or CommandType.Slash, + * return 'is', else return 'els' + * @param t + * @returns S | T + */ + const appCmd = (t: V) => { + return (is: S, els: T) => + (t & CommandType.Both) !== 0 ? is : els; + }; + const curAppType = CommandTypeRaw[module.type]; + const createCommandData = () => { + const cmd = appCmd(module.type); + return { + name: module.name, + type: curAppType, + description: cmd(module.description, ""), + options: cmd( + optionsTransformer( + (module as SlashCommand).options ?? [] + ), + [] + ), defaultMemberPermissions, dmPermission, } as ApplicationCommandData; + }; + + try { + const commandData = createCommandData(); if (!guildIds.length) { const cmd = ( await client.application!.commands.fetch() ).find( - (c) => - c.name === module.name && - c.type === CommandTypeRaw[module.type] + (c) => c.name === module.name && c.type === curAppType ); if (cmd) { if (!cmd.equals(commandData, true)) { - console.log( + logged( `Found differences in global command ${module.name}` ); - cmd.edit(commandData).then(() => { - console.log( + cmd.edit(commandData).then( + log( `${module.name} updated with new data successfully!` - ); - }); + ) + ); } return controller.next(); } client .application!.commands.create(commandData) - .then(() => { - console.log("Command created", module.name!); - }) + .then(log("Command created", module.name)) .catch(c); return controller.next(); } @@ -111,20 +132,18 @@ export function publish( for (const id of guildIds) { const guild = await client.guilds.fetch(id).catch(c); if (!guild) continue; - const guildcmd = (await guild.commands.fetch()).find( - (c) => - c.name === module.name && - c.type === CommandTypeRaw[module.type] + const guildCmd = (await guild.commands.fetch()).find( + (c) => c.name === module.name && c.type === curAppType ); - if (guildcmd) { - if (!guildcmd.equals(commandData, true)) { - console.log( + if (guildCmd) { + if (!guildCmd.equals(commandData, true)) { + logged( `Found differences in command ${module.name}` ); - guildcmd + guildCmd .edit(commandData) - .then(() => - console.log( + .then( + log( `${module.name} updated with new data successfully!` ) ) @@ -135,10 +154,10 @@ export function publish( } guild.commands .create(commandData) - .then(() => - console.log( + .then( + log( "Guild Command created", - module.name!, + module.name, guild.name ) ) @@ -146,8 +165,8 @@ export function publish( } return controller.next(); } catch (e) { - console.log("Command did not register" + module.name!); - console.log(e); + logged("Command did not register" + module.name); + logged(e); return controller.stop(); } }, @@ -162,8 +181,8 @@ export function optionsTransformer(ops: Array) { export const CommandTypeRaw = { [CommandType.Both]: ApplicationCommandType.ChatInput, - [CommandType.MenuMsg]: ApplicationCommandType.Message, - [CommandType.MenuUser]: ApplicationCommandType.User, + [CommandType.CtxUser]: ApplicationCommandType.Message, + [CommandType.CtxMsg]: ApplicationCommandType.User, [CommandType.Slash]: ApplicationCommandType.ChatInput, } as const;