Skip to content

Commit

Permalink
fix: remove useless env variables (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-montaigut authored and potb committed Jan 5, 2024
1 parent c4ee39f commit c60ab91
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 19 deletions.
3 changes: 0 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
# SETUP
DISCORD_TOKEN=
DISCORD_CLIENT_ID=
DISCORD_GUILD_ID=

# DB
REDIS_URL=

# CHANNELS
BLABLA_CHANNEL_ID=
COOL_LINKS_CHANNEL_ID=

# API
Expand Down
2 changes: 0 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import env from 'env-var';
export const config = {
discord: {
token: env.get('DISCORD_TOKEN').required().asString(),
clientId: env.get('DISCORD_CLIENT_ID').required().asString(),
guildId: env.get('DISCORD_GUILD_ID').required().asString(),
coolLinksChannelId: env.get('COOL_LINKS_CHANNEL_ID').required().asString(),
},
redis: {
Expand Down
15 changes: 6 additions & 9 deletions src/core/deleteExistingCommands.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { REST, Routes } from 'discord.js';

import { config } from '../config';

export const deleteExistingCommands = async (
rest: REST,
discord: typeof config.discord,
clientId: string,
guildId: string,
): Promise<void> => {
const guildCommands = (await rest.get(
Routes.applicationGuildCommands(discord.clientId, discord.guildId),
)) as { id: string }[];
const guildCommands = (await rest.get(Routes.applicationGuildCommands(clientId, guildId))) as {
id: string;
}[];

await guildCommands.reduce<Promise<void>>(async (promise, guildCommand) => {
await promise;

await rest.delete(
Routes.applicationGuildCommand(discord.clientId, discord.guildId, guildCommand.id),
);
await rest.delete(Routes.applicationGuildCommand(clientId, guildId, guildCommand.id));
}, Promise.resolve());
};
13 changes: 12 additions & 1 deletion src/core/loadModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@ export const loadModules = async (
const botCommands = Object.values(modulesToLoad).flatMap((module) => module.slashCommands ?? []);
checkUniqueSlashCommandNames(botCommands);
routeCommands(client, botCommands);
await pushCommands(botCommands.map((command) => command.schema));

const clientId = client.application?.id;
if (!clientId) throw new Error('Client id is not defined');

const { guilds } = client;

for (const guild of guilds.cache.values()) {
await pushCommands(
botCommands.map((command) => command.schema),
clientId,
guild.id,
);
}
routeHandlers(client, modulesToLoad);
};
10 changes: 7 additions & 3 deletions src/core/loaderCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ import { deleteExistingCommands } from './deleteExistingCommands';

const { discord } = config;

export const pushCommands = async (commands: RESTPostAPIChatInputApplicationCommandsJSONBody[]) => {
export const pushCommands = async (
commands: RESTPostAPIChatInputApplicationCommandsJSONBody[],
clientId: string,
guildId: string,
) => {
const rest = new REST({ version: '10' }).setToken(discord.token);
await deleteExistingCommands(rest, discord);
await rest.put(Routes.applicationGuildCommands(discord.clientId, discord.guildId), {
await deleteExistingCommands(rest, clientId, guildId);
await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
body: commands,
});
};
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { modules } from './modules/modules';
const { discord } = config;

const client = new Client({
intents: getIntentsFromModules(modules),
intents: ['Guilds', ...getIntentsFromModules(modules)],
});

await client.login(discord.token);
Expand Down

0 comments on commit c60ab91

Please sign in to comment.