1+ /*
2+ Copyright (C) 2025 Alexander Emanuelsson (alexemanuelol)
3+
4+ This program is free software: you can redistribute it and/or modify
5+ it under the terms of the GNU General Public License as published by
6+ the Free Software Foundation, either version 3 of the License, or
7+ (at your option) any later version.
8+
9+ This program is distributed in the hope that it will be useful,
10+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ GNU General Public License for more details.
13+
14+ You should have received a copy of the GNU General Public License
15+ along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+ https://github.com/alexemanuelol/rustplusplus
18+
19+ */
20+
21+ import * as discordjs from 'discord.js' ;
22+
23+ import { log , config , localeManager as lm , guildInstanceManager as gim } from '../../index' ;
24+ import * as discordMessages from '../discordUtils/discordMessages' ;
25+ import { DiscordManager } from '../managers/discordManager' ;
26+ import * as types from '../utils/types' ;
27+ import { Languages } from '../managers/LocaleManager' ;
28+ import { GuildChannelIds , GuildInstance } from '../managers/guildInstanceManager' ;
29+
30+ const channelChoices = [
31+ 'category' ,
32+ 'settings' ,
33+ 'servers' ,
34+ 'information' ,
35+ 'events' ,
36+ 'activity' ,
37+ 'teamchat' ,
38+ 'commands' ,
39+ 'smartSwitches' ,
40+ 'smartSwitchGroups' ,
41+ 'smartAlarms' ,
42+ 'storageMonitors' ,
43+ 'trackers' ,
44+ ] as const satisfies readonly ( keyof GuildChannelIds ) [ ] ;
45+
46+ export default {
47+ name : 'reset' ,
48+
49+ getData ( language : Languages ) {
50+ return new discordjs . SlashCommandBuilder ( )
51+ . setName ( 'reset' )
52+ . setDescription ( lm . getIntl ( language , 'slashCommandDescRole' ) )
53+ . addSubcommand ( subcommand => subcommand
54+ . setName ( 'missing_channels' )
55+ . setDescription ( lm . getIntl ( language , 'slashCommandDescResetMissingChannels' ) ) )
56+ . addSubcommand ( subcommand => subcommand
57+ . setName ( 'channel' )
58+ . setDescription ( lm . getIntl ( language , 'slashCommandDescResetChannel' ) )
59+ . addStringOption ( option => option
60+ . setName ( 'channel' )
61+ . setDescription ( lm . getIntl ( language , 'slashCommandDescResetChannelChannel' ) )
62+ . setRequired ( true )
63+ . addChoices (
64+ ...channelChoices . map ( key => ( {
65+ name : key ,
66+ value : key ,
67+ } ) ) ) ) ) ;
68+ } ,
69+
70+ async execute ( dm : DiscordManager , interaction : discordjs . ChatInputCommandInteraction ) : Promise < boolean > {
71+ const fn = '[SlashCommand: reset]' ;
72+ const logParam = {
73+ guildId : interaction . guildId
74+ } ;
75+
76+ const id = `Interaction ID: ${ interaction . id } -`
77+ await interaction . deferReply ( { flags : discordjs . MessageFlags . Ephemeral } ) ;
78+
79+ if ( ! interaction . guild ) {
80+ await discordMessages . sendDefaultMessage ( dm , interaction , 'errorTitleUnknownError' ,
81+ 'errorDescUnknownError' ) ;
82+ log . warn ( `${ fn } ${ id } Unknown Error: interaction.guild is not valid.` , logParam ) ;
83+ return false ;
84+ }
85+
86+ if ( ! dm . validPermissions ( interaction , true ) ) {
87+ await discordMessages . sendDefaultMessage ( dm , interaction , 'errorTitleMissingPermission' ,
88+ 'errorDescMissingPermission' ) ;
89+ log . warn ( `${ fn } ${ id } ${ lm . getIntl ( config . general . language , 'errorDescMissingPermission' ) } ` ,
90+ logParam ) ;
91+ return false ;
92+ }
93+
94+ let result = false ;
95+ switch ( interaction . options . getSubcommand ( ) ) {
96+ case 'missing_channels' : {
97+ result = await executeMissingChannels ( dm , interaction ) ;
98+ } break ;
99+
100+ case 'channel' : {
101+ result = await executeChannel ( dm , interaction ) ;
102+ } break ;
103+
104+ default : {
105+ const parameters = {
106+ subcommand : interaction . options . getSubcommand ( )
107+ }
108+ await discordMessages . sendDefaultMessage ( dm , interaction , 'errorTitleInvalidSubcommand' ,
109+ 'errorDescInvalidSubcommand' , parameters ) ;
110+ log . warn ( `${ fn } ${ id } ${ lm . getIntl ( config . general . language , 'errorDescInvalidSubcommand' ) } ` , logParam ) ;
111+ result = false ;
112+ } break ;
113+ }
114+
115+ return result ;
116+ }
117+ } ;
118+
119+ async function executeMissingChannels ( dm : DiscordManager , interaction : discordjs . ChatInputCommandInteraction ) :
120+ Promise < boolean > {
121+ const fn = '[SlashCommand: reset: missing_channels]' ;
122+ const guildId = interaction . guildId as types . GuildId ;
123+ const logParam = {
124+ guildId : guildId
125+ } ;
126+
127+ const id = `Interaction ID: ${ interaction . id } -`
128+
129+ const gInstance = gim . getGuildInstance ( guildId ) as GuildInstance ;
130+ const resetChannels : string [ ] = [ ] ;
131+ for ( const [ channelName , channelId ] of Object . entries ( gInstance . guildChannelIds ) ) {
132+ if ( channelId !== null ) {
133+ const channel = await dm . getChannel ( guildId , channelId as types . ChannelId )
134+ if ( channel ) {
135+ continue ;
136+ }
137+ }
138+
139+ resetChannels . push ( channelName ) ;
140+ await dm . setupGuildChannel ( interaction . guild as discordjs . Guild , channelName as keyof GuildChannelIds , true ) ;
141+ await setupChannel ( dm , interaction , channelName as keyof GuildChannelIds ) ;
142+ }
143+
144+ const parameters = {
145+ channels : resetChannels . join ( ', ' )
146+ } ;
147+ await discordMessages . sendDefaultMessage ( dm , interaction , 'slashCommandSuccessTitleResetMissingChannels' ,
148+ 'slashCommandSuccessDescResetMissingChannels' , parameters ) ;
149+ log . info ( `${ fn } ${ id } ${ lm . getIntl ( config . general . language , 'slashCommandSuccessDescResetMissingChannels' ,
150+ parameters ) } `, logParam ) ;
151+
152+ return true ;
153+ }
154+
155+ async function executeChannel ( dm : DiscordManager , interaction : discordjs . ChatInputCommandInteraction ) :
156+ Promise < boolean > {
157+ const fn = '[SlashCommand: reset: channel]' ;
158+ const guildId = interaction . guildId as types . GuildId ;
159+ const logParam = {
160+ guildId : guildId
161+ } ;
162+
163+ const id = `Interaction ID: ${ interaction . id } -`
164+ const channelName = interaction . options . getString ( 'channel' , true ) as keyof GuildChannelIds ;
165+
166+ const gInstance = gim . getGuildInstance ( guildId ) as GuildInstance ;
167+ if ( gInstance . guildChannelIds [ channelName ] !== null ) {
168+ const channelId = gInstance . guildChannelIds [ channelName ] as types . ChannelId ;
169+ const channel = await dm . getChannel ( guildId , channelId )
170+ if ( channel ) {
171+ await dm . deleteChannel ( guildId , channelId ) ;
172+ }
173+
174+ gInstance . guildChannelIds [ channelName ] = null ;
175+ gim . updateGuildInstance ( guildId ) ;
176+ }
177+
178+ await dm . setupGuildChannel ( interaction . guild as discordjs . Guild , channelName , true ) ;
179+ await setupChannel ( dm , interaction , channelName ) ;
180+
181+ const parameters = {
182+ channel : channelName
183+ } ;
184+ await discordMessages . sendDefaultMessage ( dm , interaction , 'slashCommandSuccessTitleResetChannel' ,
185+ 'slashCommandSuccessDescResetChannel' , parameters ) ;
186+ log . info ( `${ fn } ${ id } ${ lm . getIntl ( config . general . language , 'slashCommandSuccessDescResetChannel' ,
187+ parameters ) } `, logParam ) ;
188+
189+ return true ;
190+ }
191+
192+ async function setupChannel ( dm : DiscordManager , interaction : discordjs . ChatInputCommandInteraction ,
193+ channelName : keyof GuildChannelIds ) {
194+ const guild = interaction . guild as discordjs . Guild ;
195+
196+ if ( channelName === 'settings' ) {
197+ await dm . setupGuildSettingsChannel ( guild , true , true ) ;
198+ }
199+ else if ( channelName === 'servers' ) {
200+ await dm . setupGuildServersChannel ( guild , true , true ) ;
201+ }
202+ //else if (channelName === 'information') {
203+ // continue; // TODO! Implement information setup function
204+ //}
205+ else if ( channelName === 'smartSwitches' ) {
206+ await dm . setupGuildSmartSwitchesChannel ( guild , true , true ) ;
207+ }
208+ //else if (channelName === 'smartSwitchGroups') {
209+ // continue; // TODO! Implement smart switch groups setup function
210+ //}
211+ else if ( channelName === 'smartAlarms' ) {
212+ await dm . setupGuildSmartAlarmsChannel ( guild , true , true ) ;
213+ }
214+ else if ( channelName === 'storageMonitors' ) {
215+ await dm . setupGuildStorageMonitorsChannel ( guild , true , true ) ;
216+ }
217+ //else if (channelName === 'trackers') {
218+ // continue; // TODO! Implement trackers setup function
219+ //}
220+ }
0 commit comments