Skip to content

fix: remove useless env variables #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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