|
1 | 1 | import colors from "colors"; |
2 | | -import {REST, Routes} from "discord.js"; |
| 2 | +import {Guild, REST, Routes} from "discord.js"; |
3 | 3 | import fs from "fs"; |
4 | 4 | import path from "path"; |
5 | 5 | import {pathToFileURL} from "url"; |
@@ -135,55 +135,92 @@ export class CommandHelper { |
135 | 135 | body: [], |
136 | 136 | }); |
137 | 137 |
|
138 | | - try { |
139 | | - const allGuilds = await client.guilds.fetch(); |
140 | | - for (const guild of allGuilds.values()) { |
141 | | - const buildInCommandOverrides = await database.buildInCommands.findMany({ |
142 | | - where: { |
143 | | - GuildCommandMangerId: guild.id |
| 138 | + const allGuilds = await client.guilds.fetch(); |
| 139 | + for (const guild of allGuilds.values()) { |
| 140 | + const buildInCommandOverrides = await database.buildInCommands.findMany({ |
| 141 | + where: { |
| 142 | + GuildCommandMangerId: guild.id |
| 143 | + } |
| 144 | + }) |
| 145 | + |
| 146 | + cmdlist = cmdlist |
| 147 | + .filter(cmd => { |
| 148 | + const override = buildInCommandOverrides.find(o => o.CodeName === cmd.name); |
| 149 | + return !(override && override.IsEnabled === false); |
| 150 | + }) |
| 151 | + .map(cmd => { |
| 152 | + const override = buildInCommandOverrides.find(o => o.CodeName === cmd.name); |
| 153 | + if (override) { |
| 154 | + return { |
| 155 | + ...cmd, |
| 156 | + name: override.CustomName, |
| 157 | + description: override.Description ?? client.commands.get(override.CodeName).data.description, |
| 158 | + default_member_permissions: override.Permissions ?? client.commands.get(override.CodeName).data.default_member_permissions |
| 159 | + }; |
144 | 160 | } |
| 161 | + return cmd; |
145 | 162 | }) |
146 | 163 |
|
147 | | - cmdlist = cmdlist |
148 | | - .filter(cmd => { |
149 | | - const override = buildInCommandOverrides.find(o => o.CodeName === cmd.name); |
150 | | - return !(override && override.IsEnabled === false); |
151 | | - }) |
152 | | - .map(cmd => { |
153 | | - const override = buildInCommandOverrides.find(o => o.CodeName === cmd.name); |
154 | | - if (override) { |
155 | | - return { |
156 | | - ...cmd, |
157 | | - name: override.CustomName, |
158 | | - description: override.Description ?? client.commands.get(override.CodeName).data.description, |
159 | | - default_member_permissions: override.Permissions ?? client.commands.get(override.CodeName).data.default_member_permissions |
160 | | - }; |
161 | | - } |
162 | | - return cmd; |
163 | | - }) |
| 164 | + await restClient.put(Routes.applicationGuildCommands(Config.Bot.DiscordApplicationId, guild.id), { |
| 165 | + body: cmdlist, |
| 166 | + }); |
| 167 | + |
| 168 | + const ticketCommands = await database.ticketSetups.findMany({ |
| 169 | + where: { |
| 170 | + GuildId: guild.id |
| 171 | + } |
| 172 | + }) |
164 | 173 |
|
165 | | - await restClient.put(Routes.applicationGuildCommands(Config.Bot.DiscordApplicationId, guild.id), { |
166 | | - body: cmdlist, |
167 | | - }); |
| 174 | + for (const ticketCommand of ticketCommands) { |
| 175 | + const clientGuild = await client.guilds.fetch(guild.id); |
| 176 | + |
| 177 | + let guildCommand = null; |
| 178 | + try { |
| 179 | + guildCommand = await clientGuild.commands.fetch(ticketCommand.SlashCommandId); |
| 180 | + } catch { |
| 181 | + } |
| 182 | + |
| 183 | + if (!guildCommand) { |
| 184 | + guildCommand = await clientGuild.commands.create({ |
| 185 | + name: ticketCommand.SlashCommandName ?? `open-${ticketCommand.CustomId}-ticket`, |
| 186 | + description: ticketCommand.SlashCommandDescription ?? ticketCommand.CustomId, |
| 187 | + }); |
| 188 | + |
| 189 | + await database.ticketSetups.update({ |
| 190 | + where: { |
| 191 | + CustomId: ticketCommand.CustomId, |
| 192 | + }, |
| 193 | + data: { |
| 194 | + SlashCommandId: guildCommand.id, |
| 195 | + }, |
| 196 | + }); |
| 197 | + } else { |
| 198 | + if ( |
| 199 | + guildCommand.name !== ticketCommand.SlashCommandName || |
| 200 | + guildCommand.description !== ticketCommand.SlashCommandDescription |
| 201 | + ) { |
| 202 | + const updated = await guildCommand.edit({ |
| 203 | + name: ticketCommand.SlashCommandName ?? guildCommand.name, |
| 204 | + description: ticketCommand.SlashCommandDescription ?? guildCommand.description, |
| 205 | + }); |
| 206 | + |
| 207 | + await database.ticketSetups.update({ |
| 208 | + where: {CustomId: ticketCommand.CustomId}, |
| 209 | + data: {SlashCommandId: updated.id}, |
| 210 | + }); |
| 211 | + } |
| 212 | + } |
168 | 213 | } |
169 | | - Logger.info({ |
170 | | - timestamp: new Date().toISOString(), |
171 | | - level: "info", |
172 | | - label: "CommandHelper", |
173 | | - message: `Discord added ${cmdlist.length} commands (${stats.subCommands} subCommands, ${stats.subCommandGroups} subCommandGroups), ${stats.userInstall} userInstall commands, ${stats.contextMenus} context menu commands from ${moduleDirectories.length} module(s) for ${allGuilds.size} Guilds`, |
174 | | - botType: Config.BotType.toString() || "Unknown", |
175 | | - action: LoggingAction.Command, |
176 | | - }); |
177 | | - } catch (err) { |
178 | | - Logger.error({ |
179 | | - timestamp: new Date().toISOString(), |
180 | | - level: "error", |
181 | | - label: "CommandHelper", |
182 | | - message: `Failed to load commands: ${err instanceof Error ? err : String(err)}`, |
183 | | - botType: Config.BotType.toString() || "Unknown", |
184 | | - action: LoggingAction.Command, |
185 | | - }); |
| 214 | + |
186 | 215 | } |
| 216 | + Logger.info({ |
| 217 | + timestamp: new Date().toISOString(), |
| 218 | + level: "info", |
| 219 | + label: "CommandHelper", |
| 220 | + message: `Discord added ${cmdlist.length} commands (${stats.subCommands} subCommands, ${stats.subCommandGroups} subCommandGroups), ${stats.userInstall} userInstall commands, ${stats.contextMenus} context menu commands from ${moduleDirectories.length} module(s) for ${allGuilds.size} Guilds`, |
| 221 | + botType: Config.BotType.toString() || "Unknown", |
| 222 | + action: LoggingAction.Command, |
| 223 | + }); |
187 | 224 | } |
188 | 225 |
|
189 | 226 | public static async guildLoadCommands(client: ExtendedClient) { |
|
0 commit comments