Skip to content
Closed

test #17

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
2 changes: 0 additions & 2 deletions src/events/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { guildCreateEvent } from './guild-create.js';
import { hasVarEvent } from './has-var.js';
import { interactionCreateEvent } from './interaction-create.js';
import { justAskEvent } from './just-ask.js';
Expand All @@ -7,7 +6,6 @@ import type { DiscordEvent } from './types.js';

export const events = [
readyEvent,
guildCreateEvent,
justAskEvent,
hasVarEvent,
interactionCreateEvent,
Expand Down
30 changes: 14 additions & 16 deletions src/events/interaction-create.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
import { Events, MessageFlags } from 'discord.js';
import { commands } from '../commands/index.js';
import { createEvent } from '../util/events.js';
import { isAllowedServer } from '../util/server-guard.js';
import { Events } from "discord.js";
import { commands } from "../commands/index.js";
import { config } from "../env.js";
import { createEvent } from "../util/events.js";

export const interactionCreateEvent = createEvent(
{
name: Events.InteractionCreate,
},
async (interaction) => {
if (interaction.isChatInputCommand() || interaction.isMessageContextMenuCommand()) {
// Block commands from unauthorized servers
if (!interaction.guildId || !isAllowedServer(interaction.guildId)) {
console.log(`⚠️ Command blocked from unauthorized server: ${interaction.guildId}`);
if (interaction.isRepliable()) {
await interaction.reply({
content: '❌ This bot is not authorized to operate in this server.',
flags: MessageFlags.Ephemeral,
});
}
if (
interaction.isChatInputCommand() ||
interaction.isMessageContextMenuCommand()
) {
// Skip processing if not in the intended server
if (interaction.guild?.id !== config.serverId) {
return;
}

console.log(`Interaction received: ${interaction.commandName}`);
const command = commands.get(interaction.commandName);

if (!command) {
throw new Error(`No command matching ${interaction.commandName} was found.`);
throw new Error(
`No command matching ${interaction.commandName} was found.`,
);
}

await command.execute(interaction);
}
}
},
);
8 changes: 0 additions & 8 deletions src/events/ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { config } from '../env.js';
import { fetchAndCachePublicChannelsMessages } from '../util/cache.js';
import { createEvent } from '../util/events.js';
import { syncGuidesToChannel } from '../util/post-guides.js';
import { leaveIfNotAllowedServer } from '../util/server-guard.js';

export const readyEvent = createEvent(
{
Expand All @@ -12,13 +11,6 @@ export const readyEvent = createEvent(
},
async (client) => {
console.log(`Ready! Logged in as ${client.user.tag}`);

// Check all guilds and leave any unauthorized ones
console.log(`🔍 Checking ${client.guilds.cache.size} guild(s)...`);
for (const guild of client.guilds.cache.values()) {
await leaveIfNotAllowedServer(guild);
}

if (config.fetchAndSyncMessages) {
const guild = client.guilds.cache.get(config.serverId);
if (guild) {
Expand Down
20 changes: 14 additions & 6 deletions src/util/events.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { Client, ClientEvents } from 'discord.js';
import type { DiscordEvent } from '../events/types.js';
import type { Client, ClientEvents } from "discord.js";
import type { DiscordEvent } from "../events/types.js";
import { config } from "../env.js";

export const createEvent = <T extends keyof ClientEvents = keyof ClientEvents>(
data: {
name: T;
once?: boolean;
},
execute: (...args: ClientEvents[T]) => Promise<void> | void
execute: (...args: ClientEvents[T]) => Promise<void> | void,
): DiscordEvent<T> => {
return { ...data, execute };
};
Expand All @@ -18,16 +19,23 @@ export const createEvents = <T extends keyof ClientEvents = keyof ClientEvents>(
once?: boolean;
};
execute: (...args: ClientEvents[T]) => Promise<void> | void;
}>
}>,
): DiscordEvent<T>[] => {
return events.map(({ data, execute }) => createEvent(data, execute));
};

export const registerEvents = async (client: Client, events: DiscordEvent[]): Promise<void> => {
export const registerEvents = async (
client: Client,
events: DiscordEvent[],
): Promise<void> => {
for (const event of events) {
console.log(`Loading event: ${event.name}`);
client[event.once ? 'once' : 'on'](event.name, async (...args) => {
client[event.once ? "once" : "on"](event.name, async (...args) => {
try {
// Skip processing if not in the intended server
if (args[0]?.guild?.id !== config.serverId) {
return;
}
await event.execute(...args);
} catch (error) {
console.error(`Error executing event ${event.name}:`, error);
Expand Down