44 ChannelType ,
55 type Client ,
66 EmbedBuilder ,
7+ PermissionsBitField ,
78} from "discord.js" ;
89import config from "../config/config" ;
910import { botHasRecentMessages , getServerData } from "../utils/helpers" ;
@@ -92,6 +93,74 @@ export const onReady = async (client: Client) => {
9293 await sendRoleMenu ( roleChannel ) ;
9394 }
9495
96+ async function cleanRoleMenuRoles ( ) {
97+ cronLog ( "CleanRoleMenuRoles" , "Fetching guild" ) ;
98+ const guild = await client . guilds . fetch ( config . guildId ) ;
99+ if ( ! guild ) {
100+ cronLog ( "CleanRoleMenuRoles" , "Guild not found, skipping" ) ;
101+ return ;
102+ }
103+
104+ try {
105+ await guild . members . fetch ( ) ;
106+ } catch ( error ) {
107+ console . error (
108+ "[Cron][CleanRoleMenuRoles] Failed to fetch guild members:" ,
109+ error ,
110+ ) ;
111+ return ;
112+ }
113+
114+ const memberIdsWithMenuRoles = new Set < string > ( ) ;
115+
116+ for ( const roleId of config . roleMenuRoleIds ) {
117+ try {
118+ const role =
119+ guild . roles . cache . get ( roleId ) ?? ( await guild . roles . fetch ( roleId ) ) ;
120+ if ( ! role ) {
121+ cronLog ( "CleanRoleMenuRoles" , `Role ${ roleId } not found, skipping` ) ;
122+ continue ;
123+ }
124+
125+ role . members . forEach ( ( member ) => {
126+ memberIdsWithMenuRoles . add ( member . id ) ;
127+ } ) ;
128+ } catch ( error ) {
129+ console . error (
130+ `[Cron][CleanRoleMenuRoles] Failed to inspect role ${ roleId } :` ,
131+ error ,
132+ ) ;
133+ }
134+ }
135+
136+ let removedCount = 0 ;
137+ for ( const memberId of memberIdsWithMenuRoles ) {
138+ const member = guild . members . cache . get ( memberId ) ;
139+ if ( ! member ) continue ;
140+
141+ const hasAccess =
142+ member . roles . cache . has ( config . roleMenuRequiredRoleId ) ||
143+ member . permissions . has ( PermissionsBitField . Flags . Administrator ) ;
144+
145+ if ( hasAccess ) continue ;
146+
147+ try {
148+ await member . roles . remove ( config . roleMenuRoleIds ) ;
149+ removedCount += 1 ;
150+ } catch ( error ) {
151+ console . error (
152+ `[Cron][CleanRoleMenuRoles] Failed to remove role menu roles from ${ member . id } :` ,
153+ error ,
154+ ) ;
155+ }
156+ }
157+
158+ cronLog (
159+ "CleanRoleMenuRoles" ,
160+ `Removed role menu roles from ${ removedCount } member(s)` ,
161+ ) ;
162+ }
163+
95164 async function sendReactionRoleMenus ( ) {
96165 const reactionRoleChannel = await client . channels . fetch (
97166 config . reactionRoleMenuId ,
@@ -181,6 +250,13 @@ Select your notifications.
181250
182251 await updateStatus ( ) ;
183252 new cron . CronJob ( "*/5 * * * *" , updateStatus , null , true , "Europe/Berlin" ) ;
253+ new cron . CronJob (
254+ "*/5 * * * *" ,
255+ cleanRoleMenuRoles ,
256+ null ,
257+ true ,
258+ "Europe/Berlin" ,
259+ ) ;
184260
185261 new cron . CronJob ( "0 10 * * *" , sendReminder , null , true , "Europe/Berlin" ) ;
186262
@@ -202,6 +278,7 @@ Select your notifications.
202278 ) ;
203279
204280 void sendRoleMenuMsg ( ) ;
281+ void cleanRoleMenuRoles ( ) ;
205282 void sendReactionRoleMenus ( ) ;
206283 void runSync ( ) ;
207284} ;
0 commit comments