File tree Expand file tree Collapse file tree 5 files changed +61
-1
lines changed Expand file tree Collapse file tree 5 files changed +61
-1
lines changed Original file line number Diff line number Diff line change 1+ import { Events } from 'discord.js' ;
2+ import { createEvent } from '../util/events.js' ;
3+ import { leaveIfNotAllowedServer } from '../util/server-guard.js' ;
4+
5+ export const guildCreateEvent = createEvent (
6+ {
7+ name : Events . GuildCreate ,
8+ } ,
9+ async ( guild ) => {
10+ // Leave the server if it's not the allowed one
11+ await leaveIfNotAllowedServer ( guild ) ;
12+ }
13+ ) ;
Original file line number Diff line number Diff line change 1+ import { guildCreateEvent } from './guild-create.js' ;
12import { hasVarEvent } from './has-var.js' ;
23import { interactionCreateEvent } from './interaction-create.js' ;
34import { justAskEvent } from './just-ask.js' ;
@@ -6,6 +7,7 @@ import type { DiscordEvent } from './types.js';
67
78export const events = [
89 readyEvent ,
10+ guildCreateEvent ,
911 justAskEvent ,
1012 hasVarEvent ,
1113 interactionCreateEvent ,
Original file line number Diff line number Diff line change 1- import { Events } from 'discord.js' ;
1+ import { Events , MessageFlags } from 'discord.js' ;
22import { commands } from '../commands/index.js' ;
33import { createEvent } from '../util/events.js' ;
4+ import { isAllowedServer } from '../util/server-guard.js' ;
45
56export const interactionCreateEvent = createEvent (
67 {
78 name : Events . InteractionCreate ,
89 } ,
910 async ( interaction ) => {
1011 if ( interaction . isChatInputCommand ( ) || interaction . isMessageContextMenuCommand ( ) ) {
12+ // Block commands from unauthorized servers
13+ if ( ! interaction . guildId || ! isAllowedServer ( interaction . guildId ) ) {
14+ console . log ( `⚠️ Command blocked from unauthorized server: ${ interaction . guildId } ` ) ;
15+ if ( interaction . isRepliable ( ) ) {
16+ await interaction . reply ( {
17+ content : '❌ This bot is not authorized to operate in this server.' ,
18+ flags : MessageFlags . Ephemeral ,
19+ } ) ;
20+ }
21+ return ;
22+ }
23+
1124 console . log ( `Interaction received: ${ interaction . commandName } ` ) ;
1225 const command = commands . get ( interaction . commandName ) ;
1326
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import { config } from '../env.js';
33import { fetchAndCachePublicChannelsMessages } from '../util/cache.js' ;
44import { createEvent } from '../util/events.js' ;
55import { syncGuidesToChannel } from '../util/post-guides.js' ;
6+ import { leaveIfNotAllowedServer } from '../util/server-guard.js' ;
67
78export const readyEvent = createEvent (
89 {
@@ -11,6 +12,13 @@ export const readyEvent = createEvent(
1112 } ,
1213 async ( client ) => {
1314 console . log ( `Ready! Logged in as ${ client . user . tag } ` ) ;
15+
16+ // Check all guilds and leave any unauthorized ones
17+ console . log ( `🔍 Checking ${ client . guilds . cache . size } guild(s)...` ) ;
18+ for ( const guild of client . guilds . cache . values ( ) ) {
19+ await leaveIfNotAllowedServer ( guild ) ;
20+ }
21+
1422 if ( config . fetchAndSyncMessages ) {
1523 const guild = client . guilds . cache . get ( config . serverId ) ;
1624 if ( guild ) {
Original file line number Diff line number Diff line change 1+ import type { Guild } from 'discord.js' ;
2+ import { config } from '../env.js' ;
3+
4+ /**
5+ * Checks if a guild is the allowed server
6+ */
7+ export function isAllowedServer ( guildId : string ) : boolean {
8+ return guildId === config . serverId ;
9+ }
10+
11+ /**
12+ * Leaves a guild if it's not the allowed server
13+ * @returns true if the bot left the guild, false if it stayed
14+ */
15+ export async function leaveIfNotAllowedServer ( guild : Guild ) : Promise < boolean > {
16+ if ( ! isAllowedServer ( guild . id ) ) {
17+ console . log ( `⚠️ Bot added to unauthorized server: ${ guild . name } (${ guild . id } )` ) ;
18+ console . log ( `🚪 Leaving server...` ) ;
19+ await guild . leave ( ) ;
20+ console . log ( `✅ Left unauthorized server: ${ guild . name } ` ) ;
21+ return true ;
22+ }
23+ return false ;
24+ }
You can’t perform that action at this time.
0 commit comments