Skip to content

Commit 185d858

Browse files
author
xyzjesper
committed
Fixed commands loading. bump version.
1 parent 0ebcd0d commit 185d858

File tree

6 files changed

+123
-78
lines changed

6 files changed

+123
-78
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "disbot",
33
"main": "./.build/startup.js",
4-
"version": "v.1.6.4",
4+
"version": "v.1.6.5",
55
"scripts": {
66
"disbot": "npx prisma migrate deploy && node ./.build/src/main/startup.js",
77
"disbotdev": "bun run build && node ./.build/src/main/startup.js",

src/helper/CommandHelper.ts

Lines changed: 112 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export class CommandHelper {
2121

2222
Logger.info(`Starting Command loading for ${guildId}....`.gray.italic)
2323

24+
await this.addDefaultCommandsToGuild(client, guildId)
25+
2426
let cmdlist: any[] = [];
2527
const stats = {
2628
commands: 0,
@@ -112,50 +114,60 @@ export class CommandHelper {
112114
GuildCommandMangerId: guildId
113115
}
114116
})
117+
const commands = cmdlist
118+
.filter(cmd => {
119+
const override = buildInCommandOverrides.find(o => o.CodeName === cmd.name);
120+
return !(override && override.IsEnabled === false);
121+
})
122+
.map(cmd => {
123+
const override = buildInCommandOverrides.find(o => o.CodeName === cmd.name);
124+
if (override) {
125+
return {
126+
...cmd,
127+
name: override.CustomName.slice(0, 31),
128+
description: override.Description.slice(0, 99) ?? client.commands.get(override.CodeName).command.description.slice(0, 99),
129+
default_member_permissions: override.Permissions ?? client.commands.get(override.CodeName).command.default_member_permissions
130+
};
131+
}
132+
return cmd;
133+
})
134+
135+
Logger.info(`Sending commands to guild ${guildId} for client ${client.user.username}`);
136+
137+
const restClient = new REST().setToken(Config.Bot.DiscordBotToken)
138+
const currentCommands = await (await client.guilds.fetch(guildId)).commands.fetch();
139+
140+
for (const [commandId, command] of currentCommands) {
141+
try {
142+
const cmd = commands.find((c) => c.name === command.name);
115143

116-
if (buildInCommandOverrides.length > 0) {
117-
cmdlist = cmdlist
118-
.filter(cmd => {
119-
const override = buildInCommandOverrides.find(o => o.CodeName === cmd.name);
120-
return !(override && override.IsEnabled === false);
121-
})
122-
.map(cmd => {
123-
const override = buildInCommandOverrides.find(o => o.CodeName === cmd.name);
124-
if (override) {
125-
return {
126-
...cmd,
127-
name: override.CustomName,
128-
description: override.Description ?? client.commands.get(override.CodeName).command.description,
129-
default_member_permissions: override.Permissions ?? client.commands.get(override.CodeName).command.default_member_permissions
130-
};
144+
if (!cmd) {
145+
await restClient.delete(
146+
Routes.applicationGuildCommand(client.user.id, guildId, commandId)
147+
);
148+
Logger.info(`[CMD DELETE] Deleted command: ${command.name}`);
149+
} else {
150+
if (Config.Commands.CommandsToUpdate.includes(command.name)) {
151+
await restClient.patch(
152+
Routes.applicationGuildCommand(client.user.id, guildId, commandId),
153+
{body: cmd}
154+
);
155+
Logger.info(`[CMD UPDATE] Updated command: ${command.name}`);
131156
}
132-
return cmd;
133-
})
157+
}
158+
} catch (e) {
159+
Logger.error(`[CMD] Failed to process command ${command.name}: ${e}`);
160+
}
134161
}
135162

136-
Logger.info(`Sending commands to guild ${guildId} for client ${client.user.username}`);
137-
138-
try {
139-
await fetch(`https://discord.com/api/v10/applications/${client.user.id}/guilds/${guildId}/commands`, {
140-
method: "PUT",
141-
headers: {
142-
"Content-Type": "application/json",
143-
Authorization: `Bot ${Config.Bot.DiscordBotToken}`
144-
},
145-
body: JSON.stringify([])
146-
})
147-
await fetch(`https://discord.com/api/v10/applications/${client.user.id}/guilds/${guildId}/commands`, {
148-
method: "PUT",
149-
headers: {
150-
"Content-Type": "application/json",
151-
Authorization: `Bot ${Config.Bot.DiscordBotToken}`
152-
},
153-
body: JSON.stringify(cmdlist)
154-
})
155-
} catch (e) {
156-
Logger.error(`Failed to load commands: ${e}`)
157-
} finally {
158-
await this.addDefaultCommandsToGuild(client, guildId)
163+
for (const cmd of commands) {
164+
if (!currentCommands.some((c) => c.name === cmd.name)) {
165+
await restClient.post(
166+
Routes.applicationGuildCommands(client.user.id, guildId),
167+
{body: cmd}
168+
);
169+
Logger.info(`[CMD ADD] Added new command: ${cmd.name}`);
170+
}
159171
}
160172

161173
const ticketCommands = await database.ticketSetups.findMany({
@@ -166,52 +178,79 @@ export class CommandHelper {
166178

167179
if (ticketCommands.length > 0) {
168180
for (const ticketCommand of ticketCommands) {
169-
const clientGuild = await client.guilds.fetch(guildId);
170-
171-
let guildCommand = null;
172181
try {
173-
guildCommand = await clientGuild.commands.fetch(ticketCommand.SlashCommandId);
174-
} catch (e) {
175-
Logger.error(`Failed to load commands: ${e}`)
176-
}
182+
const clientGuild = await client.guilds.fetch(guildId);
177183

178-
if (!guildCommand) {
184+
if (!clientGuild || !ticketCommand || !ticketCommand.SlashCommandId) return
185+
186+
let guildCommand = null;
179187
try {
180-
guildCommand = await clientGuild.commands.create({
181-
name: ticketCommand.SlashCommandName ?? `open-${ticketCommand.CustomId}-ticket`,
182-
description: ticketCommand.SlashCommandDescription ?? ticketCommand.CustomId,
183-
});
184-
185-
await database.ticketSetups.update({
186-
where: {
187-
CustomId: ticketCommand.CustomId,
188-
},
189-
data: {
190-
SlashCommandId: guildCommand.id,
191-
},
192-
});
188+
guildCommand = await clientGuild.commands.fetch(ticketCommand.SlashCommandId);
193189
} catch (e) {
194-
Logger.error(`Failed to load commands: ${e}`)
190+
Logger.error(`[TICKET] Failed to load commands: ${e}`)
191+
return
195192
}
196-
} else {
197-
if (
198-
guildCommand.name !== ticketCommand.SlashCommandName ||
199-
guildCommand.description !== ticketCommand.SlashCommandDescription
200-
) {
193+
194+
let name = `open-${ticketCommand.CustomId.split("-")[0]}-ticket`
195+
if (name.length >= 32 || (ticketCommand?.SlashCommandName && ticketCommand.SlashCommandName?.length >= 32)) {
196+
return
197+
} else if (ticketCommand.SlashCommandName.length <= 30) {
198+
name = ticketCommand.SlashCommandName
199+
}
200+
201+
let description = "Open Ticket with command."
202+
if (ticketCommand && ticketCommand.SlashCommandDescription.length < 31) {
203+
description = "Open Ticket with command."
204+
} else if (guildCommand && guildCommand?.description < 99) {
205+
description = guildCommand.description
206+
}
207+
208+
if (!guildCommand) {
201209
try {
202-
const updated = await guildCommand.edit({
203-
name: ticketCommand.SlashCommandName ?? guildCommand.name,
204-
description: ticketCommand.SlashCommandDescription ?? guildCommand.description,
210+
211+
212+
guildCommand = await clientGuild.commands.create({
213+
name: name,
214+
description: description,
205215
});
206216

207217
await database.ticketSetups.update({
208-
where: {CustomId: ticketCommand.CustomId},
209-
data: {SlashCommandId: updated.id},
218+
where: {
219+
CustomId: ticketCommand.CustomId,
220+
},
221+
data: {
222+
SlashCommandId: guildCommand.id,
223+
},
210224
});
211225
} catch (e) {
212-
Logger.error(`Failed to load commands: ${e}`)
226+
Logger.error(`[TICKET] Failed to load commands: ${e}`)
227+
return
228+
}
229+
} else {
230+
if (
231+
guildCommand.name !== ticketCommand.SlashCommandName ||
232+
guildCommand.description !== ticketCommand.SlashCommandDescription
233+
) {
234+
235+
236+
try {
237+
const updated = await guildCommand.edit({
238+
name: name,
239+
description: description,
240+
});
241+
242+
await database.ticketSetups.update({
243+
where: {CustomId: ticketCommand.CustomId},
244+
data: {SlashCommandId: updated.id},
245+
});
246+
} catch (e) {
247+
Logger.error(`[TICKET] Failed to load commands: ${e}`)
248+
return
249+
}
213250
}
214251
}
252+
} catch (e) {
253+
return
215254
}
216255
}
217256
}

src/main/bot.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ await disbotClient
7777
.login(Config.Bot.DiscordBotToken)
7878
.then(async () => {
7979
Logger.info(`Connected to Discord as ${disbotClient.user.tag} on ${disbotClient.guilds.cache.size}!`)
80-
process.setMaxListeners(0);
81-
disbotClient.setMaxListeners(0);
80+
process.setMaxListeners(Infinity);
81+
disbotClient.setMaxListeners(Infinity);
8282

8383
// Database init (Default)
8484
await initDataToDatabase(disbotClient)

src/main/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ export async function configStartup() {
6969
ApiKey: "",
7070
},
7171
},
72+
Commands: {
73+
CommandsToUpdate: []
74+
},
7275
Logging: {
7376
ErrorWebhook: "",
7477
BotLogger: "",

src/main/data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const configVersion = "1.3.0"
1+
export const configVersion = "1.3.1"
22

33
export const protectedCommands: string[] = [
44
"permissions", "commands", "letmegooglethat"

src/types/Config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BotType } from "enums/botType.js";
1+
import {BotType} from "enums/botType.js";
22

33
export type DisBotConfigData = {
44
Bot: {
@@ -58,6 +58,9 @@ export type DisBotConfigData = {
5858
BotLogger: string;
5959
GitHubAPIToken: string;
6060
};
61+
Commands: {
62+
CommandsToUpdate: string[]
63+
};
6164
BotType: string;
6265
CONFIG_VERSION: string
6366
};

0 commit comments

Comments
 (0)