diff --git a/.vscode/settings.json b/.vscode/settings.json index ed785b5..b310cd8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,3 @@ { - "dotenv.enableAutocloaking": false + "dotenv.enableAutocloaking": true } \ No newline at end of file diff --git a/commands/nowplaying.ts b/commands/nowplaying.ts index f42645b..407375c 100644 --- a/commands/nowplaying.ts +++ b/commands/nowplaying.ts @@ -1,5 +1,5 @@ import { commandModule, CommandType } from '@sern/handler'; -import { publish } from '../plugins/index.js'; +import { publish } from '../plugins/publish.js'; import { ActionRowBuilder, ComponentType, EmbedBuilder, StringSelectMenuBuilder } from 'discord.js'; import axios from 'axios'; import progressbar from 'string-progressbar' diff --git a/commands/play.ts b/commands/play.ts index 6f41a18..4e6f234 100644 --- a/commands/play.ts +++ b/commands/play.ts @@ -1,5 +1,5 @@ import { commandModule, CommandType } from '@sern/handler'; -import { publish } from '../plugins/index.js'; +import { publish } from '../plugins/publish.js'; import { createAudioPlayer, createAudioResource, @@ -45,7 +45,7 @@ export default commandModule({ execute: async (ctx, options) => { const radioname = options[1].getString('radio', true); const fetchUser = (await ( - await ctx.client.guilds.fetch(ctx.guild.id) + await ctx.client.guilds.fetch(ctx.guild!.id) ).members.fetch(ctx.user.id)) as GuildMember; if (!fetchUser.voice.channel?.id) @@ -88,7 +88,7 @@ export default commandModule({ const connection = joinVoiceChannel({ adapterCreator: ctx.interaction.guild!.voiceAdapterCreator, - guildId: ctx.guild.id, + guildId: ctx.guild!.id, channelId: fetchUser.voice.channelId!, }); const resource = createAudioResource(stream); @@ -104,7 +104,7 @@ export default commandModule({ const connection = joinVoiceChannel({ adapterCreator: ctx.interaction.guild!.voiceAdapterCreator, - guildId: ctx.guild.id, + guildId: ctx.guild!.id, channelId: fetchUser.voice.channelId!, }); const resource = createAudioResource(stream); diff --git a/commands/stick.ts b/commands/stick.ts index 1e99cde..3f38c0c 100644 --- a/commands/stick.ts +++ b/commands/stick.ts @@ -1,7 +1,7 @@ import { commandModule, CommandType } from '@sern/handler'; import { ApplicationCommandOptionType, GuildMember } from 'discord.js'; import { prisma } from '../index.js'; -import { publish } from '../plugins/index.js'; +import { publish } from '../plugins/publish.js'; export default commandModule({ type: CommandType.Slash, @@ -31,13 +31,13 @@ export default commandModule({ ], execute: async (ctx, options) => { const radioname = options[1].getString('radio', true); - const fetchUser = await (await ctx.client.guilds.fetch(ctx.guild.id)).members.fetch(ctx.user.id) as GuildMember + const fetchUser = await (await ctx.client.guilds.fetch(ctx.guild!.id)).members.fetch(ctx.user.id) as GuildMember async function createDoc(radio: string) { return await prisma.stick.create({ data: { channelid: fetchUser.voice.channel?.id!, - guildid: ctx.guild.id, + guildid: ctx.guild!.id, radio: radio } }) @@ -56,7 +56,7 @@ export default commandModule({ const countDocs = await prisma.stick.count({ where: { - guildid: ctx.guild.id + guildid: ctx.guild!.id } }) if (countDocs !== 0) diff --git a/commands/stop.ts b/commands/stop.ts index f467459..e613648 100644 --- a/commands/stop.ts +++ b/commands/stop.ts @@ -1,5 +1,5 @@ import { commandModule, CommandType } from '@sern/handler'; -import { publish } from '../plugins/index.js'; +import { publish } from '../plugins/publish.js'; import { getVoiceConnection } from '@discordjs/voice' import type { GuildMember } from 'discord.js'; @@ -9,8 +9,8 @@ export default commandModule({ description: 'Stop the radio of that guild', options: [], execute: async (ctx, options) => { - const fetchUser = await (await ctx.client.guilds.fetch(ctx.guild.id)).members.fetch(ctx.user.id) as GuildMember - const fetchBot = await (await ctx.client.guilds.fetch(ctx.guild.id)).members.fetch(ctx.client.user!.id) as GuildMember + const fetchUser = await (await ctx.client.guilds.fetch(ctx.guild!.id)).members.fetch(ctx.user.id) as GuildMember + const fetchBot = await (await ctx.client.guilds.fetch(ctx.guild!.id)).members.fetch(ctx.client.user!.id) as GuildMember if (fetchUser.voice.channel?.id !== fetchBot.voice.channel?.id) return await ctx.reply({ @@ -18,7 +18,7 @@ export default commandModule({ ephemeral: true }) - const connection = getVoiceConnection(ctx.guild.id) + const connection = getVoiceConnection(ctx.guild!.id) connection!.destroy() await ctx.reply({ diff --git a/commands/unstick.ts b/commands/unstick.ts index 0d94919..c8544a1 100644 --- a/commands/unstick.ts +++ b/commands/unstick.ts @@ -1,5 +1,5 @@ import { commandModule, CommandType } from '@sern/handler'; -import { publish } from '../plugins/index.js'; +import { publish } from '../plugins/publish.js'; import { prisma } from '../index.js'; import { getVoiceConnection } from '@discordjs/voice' @@ -11,7 +11,7 @@ export default commandModule({ execute: async (ctx, options) => { const countDocs = await prisma.stick.count({ where: { - guildid: ctx.guild.id + guildid: ctx.guild!.id } }) if (countDocs === 0) @@ -22,10 +22,10 @@ export default commandModule({ await prisma.stick.deleteMany({ where: { - guildid: ctx.guild.id + guildid: ctx.guild!.id } }) - const connection = getVoiceConnection(ctx.guild.id) + const connection = getVoiceConnection(ctx.guild!.id) connection!.destroy() await ctx.reply({ diff --git a/index.ts b/index.ts index c7d894f..caee6fb 100644 --- a/index.ts +++ b/index.ts @@ -1,7 +1,8 @@ import { Client, GatewayIntentBits } from 'discord.js'; -import { Sern, SernEmitter } from '@sern/handler'; + import { PrismaClient } from '@prisma/client'; import 'dotenv/config'; +import { type Dependencies, type Singleton, DefaultLogging, Sern, single } from '@sern/handler'; export const prisma = new PrismaClient() @@ -12,12 +13,22 @@ const client = new Client({ GatewayIntentBits.GuildVoiceStates, ], }); -//View docs for all options + +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 + } }); client.login(process.env.TOKEN); diff --git a/package.json b/package.json index beaa83b..c218bd2 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "@discordjs/opus": "^0.9.0", "@discordjs/voice": "^0.14.0", "@prisma/client": "^4.7.1", - "@sern/handler": "^1.0.0", + "@sern/handler": "^2.0.0", "axios": "^1.2.1", "discord.js": "^14.7.1", "dotenv": "^16.0.3", diff --git a/plugins/index.ts b/plugins/index.ts deleted file mode 100644 index 6558aff..0000000 --- a/plugins/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './publish.js' \ No newline at end of file diff --git a/plugins/publish.ts b/plugins/publish.ts index da282cd..95a4a97 100644 --- a/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; diff --git a/yarn.lock b/yarn.lock index cbe2889..54beb46 100644 --- a/yarn.lock +++ b/yarn.lock @@ -107,11 +107,12 @@ resolved "https://registry.npmjs.org/@sapphire/snowflake/-/snowflake-3.3.0.tgz" integrity sha512-Hec5N6zEkZuZFLybVKyLFLlcSgYmR6C1/+9NkIhxPwOf6tgX52ndJCSz8ADejmbrNE0VuNCNkpzhRZzenEC9vA== -"@sern/handler@^1.0.0": - version "1.2.1" - resolved "https://registry.npmjs.org/@sern/handler/-/handler-1.2.1.tgz" - integrity sha512-ElXlJC/tS5T++QCUCXw1uCCfMu4sVSg6UN6uD+Y9TigynpQsFpzHAKuOtyQwUcwIBbiB+NZ2xAbOiyVy0YpPvw== +"@sern/handler@^2.0.0": + version "2.0.0" + resolved "https://registry.yarnpkg.com/@sern/handler/-/handler-2.0.0.tgz#06229074b810349588973b6a4714e624d081d09d" + 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" @@ -506,6 +507,13 @@ isexe@^2.0.0: resolved "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz" integrity sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw== +iti@^0.5.0: + version "0.5.0" + resolved "https://registry.yarnpkg.com/iti/-/iti-0.5.0.tgz#fb7bcd32f60d71d9232e8b936608025f2ce106f7" + integrity sha512-ZiwdEIhXAxyb6/+j2didONRa3q73y9h8oHMRXAIkXv7k851cm8H7fyZZyszr514TdzgoVQl7Z6hvsdTogjgc0w== + dependencies: + utility-types "^3.10.0" + json-buffer@3.0.1: version "3.0.1" resolved "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.1.tgz" @@ -955,6 +963,11 @@ util-deprecate@^1.0.1: resolved "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz" integrity sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw== +utility-types@^3.10.0: + version "3.10.0" + resolved "https://registry.yarnpkg.com/utility-types/-/utility-types-3.10.0.tgz#ea4148f9a741015f05ed74fd615e1d20e6bed82b" + integrity sha512-O11mqxmi7wMKCo6HKFt5AhO4BwY3VV68YU07tgxfz8zJTIxr4BpsezN49Ffwy9j3ZpwwJp4fkRwjRzq3uWE6Rg== + webidl-conversions@^3.0.0: version "3.0.1" resolved "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz"