chore: update sern

This commit is contained in:
2022-12-29 17:05:39 +01:00
parent 46adf3f81a
commit 7342b58e6e
14 changed files with 136 additions and 113 deletions

62
plugins/nsfwOnly.ts Normal file
View File

@@ -0,0 +1,62 @@
//@ts-nocheck
/**
* This plugin checks if the channel is nsfw and responds to user with a specified response if not nsfw
*
* @author @NeoYaBoi [<@762918086349029386>]
* @version 1.0.0
* @example
* ```ts
* import { nsfwOnly } from "../plugins/nsfwOnly";
* import { commandModule } from "@sern/handler";
* export default commandModule({
* plugins: [ nsfwOnly('response', true) ],
* execute: (ctx) => {
* //your code here
* }
* })
* ```
*/
import {
ChannelType,
GuildTextBasedChannel,
TextBasedChannel,
TextChannel,
} from "discord.js";
import { CommandType, EventPlugin, Nullish, PluginType } from "@sern/handler";
function isGuildText(
channel: Nullish<TextBasedChannel>
): channel is GuildTextBasedChannel {
return (
channel?.type == ChannelType.GuildPublicThread ||
channel?.type == ChannelType.GuildPrivateThread
);
}
export function nsfwOnly(
onFail: string,
ephemeral: boolean
): EventPlugin<CommandType.Both> {
return {
type: PluginType.Event,
description: "Checks if the channel is nsfw or not.",
async execute(event, controller) {
const [ctx] = event;
//checking if command was executed in dms
if (ctx.guild === null) {
await ctx.reply({ content: onFail, ephemeral });
return controller.stop();
}
//channel is thread (not supported by nsfw)
if (isGuildText(ctx.channel) == true) {
await ctx.reply({ content: onFail, ephemeral });
return controller.stop();
}
if (!(ctx.channel! as TextChannel).nsfw) {
//channel is not nsfw
await ctx.reply({ content: onFail, ephemeral });
return controller.stop();
}
//continues to command if nsfw
return controller.next();
},
};
}

216
plugins/publish.ts Normal file
View File

@@ -0,0 +1,216 @@
// @ts-nocheck
/**
* This is publish plugin, it allows you to publish your application commands using the discord.js library with ease.
*
* @author @EvolutionX-10 [<@697795666373640213>]
* @version 2.0.0
* @example
* ```ts
* import { publish } from "../plugins/publish";
* import { commandModule } from "@sern/handler";
* export default commandModule({
* plugins: [ publish() ], // put an object containing permissions, ids for guild commands, boolean for dmPermission
* // plugins: [ publish({ guildIds: ['guildId'], defaultMemberPermissions: 'Administrator'})]
* execute: (ctx) => {
* //your code here
* }
* })
* ```
*/
import {
CommandPlugin,
CommandType,
PluginType,
SernOptionsData,
SlashCommand,
} from "@sern/handler";
import {
ApplicationCommandData,
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.CtxUser
| CommandType.CtxMsg
> {
return {
type: PluginType.Command,
description: "Manage Application Commands",
name: "slash-auto-publish",
async execute({ mod: module }, controller) {
// Users need to provide their own useContainer function.
const [client] = useContainer("@sern/client");
const defaultOptions = {
guildIds: [],
dmPermission: undefined,
defaultMemberPermissions: null,
};
options = { ...defaultOptions, ...options } as PublishOptions &
ValidPublishOptions;
let { defaultMemberPermissions, dmPermission, guildIds } =
options as unknown as ValidPublishOptions;
function c(e: unknown) {
console.error("publish command didnt work for", module.name);
console.error(e);
}
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 = <V extends CommandType, S, T>(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 === curAppType
);
if (cmd) {
if (!cmd.equals(commandData, true)) {
logged(
`Found differences in global command ${module.name}`
);
cmd.edit(commandData).then(
log(
`${module.name} updated with new data successfully!`
)
);
}
return controller.next();
}
client
.application!.commands.create(commandData)
.then(log("Command created", module.name))
.catch(c);
return controller.next();
}
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 === curAppType
);
if (guildCmd) {
if (!guildCmd.equals(commandData, true)) {
logged(
`Found differences in command ${module.name}`
);
guildCmd
.edit(commandData)
.then(
log(
`${module.name} updated with new data successfully!`
)
)
.catch(c);
continue;
}
continue;
}
guild.commands
.create(commandData)
.then(
log(
"Guild Command created",
module.name,
guild.name
)
)
.catch(c);
}
return controller.next();
} catch (e) {
logged("Command did not register" + module.name);
logged(e);
return controller.stop();
}
},
};
}
export function optionsTransformer(ops: Array<SernOptionsData>) {
return ops.map((el) =>
el.autocomplete ? (({ command, ...el }) => el)(el) : el
);
}
export const CommandTypeRaw = {
[CommandType.Both]: ApplicationCommandType.ChatInput,
[CommandType.CtxUser]: ApplicationCommandType.Message,
[CommandType.CtxMsg]: ApplicationCommandType.User,
[CommandType.Slash]: ApplicationCommandType.ChatInput,
} as const;
export type NonEmptyArray<T extends `${number}` = `${number}`> = [T, ...T[]];
export interface ValidPublishOptions {
guildIds: string[];
dmPermission: boolean;
defaultMemberPermissions: PermissionResolvable;
}
interface GuildPublishOptions {
guildIds?: NonEmptyArray;
defaultMemberPermissions?: PermissionResolvable;
dmPermission?: never;
}
interface GlobalPublishOptions {
defaultMemberPermissions?: PermissionResolvable;
dmPermission?: false;
guildIds?: never;
}
type BasePublishOptions = GuildPublishOptions | GlobalPublishOptions;
export type PublishOptions = BasePublishOptions &
(
| Required<Pick<BasePublishOptions, "defaultMemberPermissions">>
| (
| Required<Pick<BasePublishOptions, "dmPermission">>
| Required<Pick<BasePublishOptions, "guildIds">>
)
);