Skip to content

Commit

Permalink
dev register split
Browse files Browse the repository at this point in the history
  • Loading branch information
Larsundso committed Feb 2, 2025
1 parent 969312e commit 3a1fe41
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
18 changes: 18 additions & 0 deletions src/register-commands-dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-disable no-console */
import * as DiscordCore from '@discordjs/core';
import * as DiscordRest from '@discordjs/rest';
import 'dotenv/config';
import commands from './SlashCommands/index.js';

const createCommands = Object.values(commands.public);
const token = process.env.DevToken ?? '';

const requestHandler = (t: string) =>
new DiscordCore.API(new DiscordRest.REST({ version: '10' }).setToken(t.replace('Bot ', '')));

await requestHandler(token)
.applicationCommands.bulkOverwriteGlobalCommands(
Buffer.from(token.replace('Bot ', '')?.split('.')[0] ?? '0', 'base64').toString() ?? '',
createCommands.map((c) => c.toJSON()),
)
.then((r) => console.log(`[DEV] Registered ${r.length} Global Commands`));
52 changes: 25 additions & 27 deletions src/register-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import DataBase from './BaseClient/Bot/DataBase.js';
import commands from './SlashCommands/index.js';

const createCommands = Object.values(commands.public);
const token = (process.argv.includes('--dev') ? process.env.DevToken : process.env.Token) ?? '';
const token = process.env.Token ?? '';

const requestHandler = (t: string) =>
new DiscordCore.API(new DiscordRest.REST({ version: '10' }).setToken(t.replace('Bot ', '')));
Expand All @@ -28,32 +28,30 @@ await fetch(`https://discordbotlist.com/api/v1/bots/${process.env.mainId}/comman
body: JSON.stringify(createCommands.map((c) => c.toJSON())),
});

if (!process.argv.includes('--dev')) {
(
await DataBase.customclients.findMany({ where: { token: { not: null }, appid: { not: null } } })
).forEach(async (s) => {
const api = requestHandler(s.token ?? '');

await api.applicationCommands
.bulkOverwriteGlobalCommands(
s.appid ?? '',
createCommands
.filter((c) => !c.name.includes('action'))
.map((c) =>
c
.setContexts(Discord.InteractionContextType.Guild)
.setIntegrationTypes(Discord.ApplicationIntegrationType.GuildInstall)
.toJSON(),
),
)
.then((r) => console.log(`[CUSTOM] Registered ${r.length} Global Commands`))
.catch((e: Error) => {
if (!e.message.includes('401')) return;
console.log(`Unauthorized for ${s.appid}`, e);
DataBase.customclients.delete({ where: { guildid: s.guildid } }).then();
});
});
}
(
await DataBase.customclients.findMany({ where: { token: { not: null }, appid: { not: null } } })
).forEach(async (s) => {
const api = requestHandler(s.token ?? '');

await api.applicationCommands
.bulkOverwriteGlobalCommands(
s.appid ?? '',
createCommands
.filter((c) => !c.name.includes('action'))
.map((c) =>
c
.setContexts(Discord.InteractionContextType.Guild)
.setIntegrationTypes(Discord.ApplicationIntegrationType.GuildInstall)
.toJSON(),
),
)
.then((r) => console.log(`[CUSTOM] Registered ${r.length} Global Commands`))
.catch((e: Error) => {
if (!e.message.includes('401')) return;
console.log(`Unauthorized for ${s.appid}`, e);
DataBase.customclients.delete({ where: { guildid: s.guildid } }).then();
});
});

setTimeout(async () => {
console.log('Finished. Exiting...');
Expand Down

0 comments on commit 3a1fe41

Please sign in to comment.