Skip to content

Commit

Permalink
[PATCH] init custom bot slash commands on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
Larsundso committed Dec 26, 2023
1 parent d97a035 commit 246be36
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"type": "module",
"version": "1.6.56",
"version": "1.6.57",
"scripts": {
"start": "node --max-old-space-size=8192 ./dist/index.js",
"build": "npx tsc",
Expand Down
2 changes: 2 additions & 0 deletions src/Events/readyEvents/startupTasks/customBotCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import client from '../../../BaseClient/Client.js';
import * as ch from '../../../BaseClient/ClientHelper.js';

export default async () => {
if (client.shard?.mode === 'process') return;

const settings = await ch.DataBase.guildsettings.findMany({
where: {
token: { not: null },
Expand Down
71 changes: 59 additions & 12 deletions src/Events/readyEvents/startupTasks/slashCommandInitializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,68 @@ export default async () => {
await client.application?.commands.set(createCommands);
await client.application?.commands.fetch();

const guilds = await ch.DataBase.guildsettings.findMany({ where: { token: { not: null } } });
await Promise.all(
guilds.map((g) => {
const guild = client.guilds.cache.get(g.guildid);
if (!guild) return Promise.resolve();
mainBotCommands(createCommands);
customBotCommands(createCommands);
};

type ValueOf<T> = T[keyof T];

const mainBotCommands = async (createCommands: ValueOf<(typeof commands)['public']>[]) =>
(await ch.DataBase.guildsettings.findMany({ where: { token: { not: null } } })).forEach((g) => {
const guild = client.guilds.cache.get(g.guildid);
if (!guild) return;

ch.request.commands.bulkOverwriteGlobalCommands(
guild,
createCommands.map((c) => c.toJSON()),
);
});

const customBotCommands = async (createCommands: ValueOf<(typeof commands)['public']>[]) =>
(
await ch.DataBase.guildsettings.findMany({
where: {
token: { not: null },
},
})
).forEach(async (s) => {
if (!s.token) return;

const guild = client.guilds.cache.get(s.guildid);
if (!guild) return;

const globalCommands = async () => {
if (!s.token) return;

const cmds = await ch.request.commands.bulkOverwriteGlobalCommands(
guild,
createCommands.map((c) => c.toJSON()),
);

if ('message' in cmds) {
ch.error(guild, new Error(cmds.message));
return;
}

cmds.forEach((c) => ch.cache.commands.set(guild.id, c.id, c));
};

return ch.request.commands.bulkOverwriteGlobalCommands(
const guildCommands = async () => {
if (!s.token) return;

const cmds = await ch.request.commands.bulkOverwriteGuildCommands(
guild,
createCommands.map((c) => c.toJSON()),
);
}),
);

client.guilds.cache.forEach((g) => {
ch.request.commands.getGuildCommands(g);
ch.request.commands.getGlobalCommands(g);
if ('message' in cmds) {
ch.error(guild, new Error(cmds.message));
return;
}

cmds.forEach((c) => ch.cache.commands.set(guild.id, c.id, c));
};

globalCommands();
guildCommands();
});
};

0 comments on commit 246be36

Please sign in to comment.