mirror of
https://github.com/sern-handler/awesome-plugins
synced 2026-06-18 22:02:20 +00:00
chore: Update JavaScript plugins (#69)
chore: update JavaScript plugins Co-authored-by: EvolutionX-10 <EvolutionX-10@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
98ab449203
commit
b2ee0996f1
@@ -18,156 +18,135 @@
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
import { CommandType, PluginType } from "@sern/handler";
|
||||
import { CommandInitPlugin, CommandType, controller } from "@sern/handler";
|
||||
import { ApplicationCommandType } 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) {
|
||||
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 };
|
||||
let { defaultMemberPermissions, dmPermission, guildIds } = options;
|
||||
|
||||
function c(e) {
|
||||
console.error("publish command didnt work for", module.name);
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
const log =
|
||||
(...message) =>
|
||||
() =>
|
||||
console.log(...message);
|
||||
|
||||
const logged = (...message) => 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) => {
|
||||
return (is, els) => ((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.options ?? []), []),
|
||||
defaultMemberPermissions,
|
||||
dmPermission,
|
||||
};
|
||||
};
|
||||
|
||||
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) {
|
||||
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,
|
||||
};
|
||||
export function publish(options) {
|
||||
return CommandInitPlugin(async ({ module }) => {
|
||||
// Users need to provide their own useContainer function.
|
||||
const [client] = useContainer("@sern/client");
|
||||
const defaultOptions = {
|
||||
guildIds: [],
|
||||
dmPermission: undefined,
|
||||
defaultMemberPermissions: null,
|
||||
};
|
||||
options = { ...defaultOptions, ...options };
|
||||
let { defaultMemberPermissions, dmPermission, guildIds } = options;
|
||||
|
||||
function c(e) {
|
||||
console.error("publish command didnt work for", module.name);
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
const log =
|
||||
(...message) =>
|
||||
() =>
|
||||
console.log(...message);
|
||||
|
||||
const logged = (...message) => 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) => {
|
||||
return (is, els) => ((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.options ?? []), []),
|
||||
defaultMemberPermissions,
|
||||
dmPermission,
|
||||
};
|
||||
};
|
||||
|
||||
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) {
|
||||
return ops.map((el) =>
|
||||
el.autocomplete ? (({ command, ...el }) => el)(el) : el
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user