Skip to content

Commit 42aaf84

Browse files
author
xyzjesper
committed
Release v.1.6.2r16
1 parent 3827f4b commit 42aaf84

23 files changed

+1195
-738
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.2r14",
4+
"version": "v.1.6.2r16",
55
"scripts": {
66
"disbot": "node ./.build/src/main/startup.js",
77
"customer": "cd /home/disbot && node ./.build/src/main/startup.js",

src/helper/CommandHelper.ts

Lines changed: 218 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,224 @@ import {cli} from "winston/lib/winston/config/index.js";
1414
colors.enable();
1515

1616
export class CommandHelper {
17+
18+
public static async loadCommandsForGuild(client: ExtendedClient, guild: Guild) {
19+
Logger.info({
20+
timestamp: new Date().toISOString(),
21+
level: "info",
22+
label: "CommandHelper",
23+
message: `Loading commands for ${client.user?.displayName || "Unknown Bot"}`,
24+
botType: Config.BotType.toString() || "Unknown",
25+
action: LoggingAction.Command,
26+
});
27+
28+
let cmdlist: any[] = [];
29+
const stats = {
30+
commands: 0,
31+
userInstall: 0,
32+
contextMenus: 0,
33+
subCommands: 0,
34+
subCommandGroups: 0
35+
};
36+
37+
const modulesFolder = path.join(process.cwd(), ".build", "src", "modules");
38+
if (!fs.existsSync(modulesFolder)) {
39+
console.warn("Modules folder does not exist.".red);
40+
return;
41+
}
42+
43+
const moduleDirectories = fs.readdirSync(modulesFolder, {withFileTypes: true})
44+
.filter(dirent => dirent.isDirectory())
45+
.map(dirent => dirent.name);
46+
47+
for (const moduleDir of moduleDirectories) {
48+
const moduleCommandFolder = path.join(modulesFolder, moduleDir, "commands");
49+
50+
if (!fs.existsSync(moduleCommandFolder)) {
51+
continue
52+
}
53+
54+
const commandDirs = {
55+
commands: moduleCommandFolder,
56+
contextMenus: path.join(modulesFolder, moduleDir, "contextmenu"),
57+
subCommands: path.join(moduleCommandFolder, "subCommand"),
58+
userInstall: path.join(moduleCommandFolder, "userInstall"),
59+
subCommandGroups: path.join(moduleCommandFolder, "subCommandGroup"),
60+
};
61+
62+
// Load main commands
63+
if (fs.existsSync(commandDirs.commands)) {
64+
const commandFiles = getFilesRecursively(commandDirs.commands, [".js"]);
65+
66+
for (const filePath of commandFiles) {
67+
const relativePath = path.relative(commandDirs.commands, filePath);
68+
if (relativePath.includes(path.sep)) {
69+
continue;
70+
}
71+
72+
try {
73+
const module = await import(pathToFileURL(filePath).href);
74+
if (module.default?.data) {
75+
cmdlist.push(module.default.data.toJSON());
76+
stats.commands++;
77+
}
78+
} catch (error) {
79+
console.error(`Failed to load command from ${filePath}:`.red, error);
80+
}
81+
}
82+
}
83+
84+
// Load userInstall commands
85+
if (fs.existsSync(commandDirs.userInstall)) {
86+
const userCommandFiles = getFilesRecursively(commandDirs.userInstall, [".js"]);
87+
for (const filePath of userCommandFiles) {
88+
try {
89+
const module = await import(pathToFileURL(filePath).href);
90+
if (module.default?.data) {
91+
cmdlist.push(module.default.data.toJSON());
92+
stats.userInstall++;
93+
}
94+
} catch (error) {
95+
console.error(`Failed to load userInstall command from ${filePath}:`.red, error);
96+
}
97+
}
98+
}
99+
100+
// Load context menu commands
101+
if (fs.existsSync(commandDirs.contextMenus)) {
102+
const contextCommandFiles = getFilesRecursively(commandDirs.contextMenus, [".js"]);
103+
for (const filePath of contextCommandFiles) {
104+
try {
105+
const module = await import(pathToFileURL(filePath).href);
106+
if (module.default?.data) {
107+
cmdlist.push(module.default.data.toJSON());
108+
stats.contextMenus++;
109+
}
110+
} catch (error) {
111+
console.error(`Failed to load context menu from ${filePath}:`.red, error);
112+
}
113+
}
114+
}
115+
116+
// Count subCommands and subCommandGroups (für Stats)
117+
if (fs.existsSync(commandDirs.subCommands)) {
118+
const subCommandFiles = getFilesRecursively(commandDirs.subCommands, [".js"]);
119+
stats.subCommands += subCommandFiles.length;
120+
}
121+
122+
if (fs.existsSync(commandDirs.subCommandGroups)) {
123+
const subCommandGroupFiles = getFilesRecursively(commandDirs.subCommandGroups, [".js"]);
124+
stats.subCommandGroups += subCommandGroupFiles.length;
125+
}
126+
}
127+
128+
if (!Config.Bot.DiscordApplicationId || !Config.Bot.DiscordBotToken) {
129+
throw new Error("Missing Config variables: DiscordApplicationId or DiscordBotToken");
130+
}
131+
132+
const restClient = new REST({version: "10"}).setToken(Config.Bot.DiscordBotToken);
133+
134+
// Clear application command
135+
await restClient.put(Routes.applicationCommands(Config.Bot.DiscordApplicationId), {
136+
body: [],
137+
});
138+
139+
const buildInCommandOverrides = await database.buildInCommands.findMany({
140+
where: {
141+
GuildCommandMangerId: guild.id
142+
}
143+
})
144+
try {
145+
cmdlist = cmdlist
146+
.filter(cmd => {
147+
const override = buildInCommandOverrides.find(o => o.CodeName === cmd.name);
148+
return !(override && override.IsEnabled === false);
149+
})
150+
.map(cmd => {
151+
const override = buildInCommandOverrides.find(o => o.CodeName === cmd.name);
152+
if (override) {
153+
return {
154+
...cmd,
155+
name: override.CustomName,
156+
description: override.Description ?? client.commands.get(override.CodeName).data.description,
157+
default_member_permissions: override.Permissions ?? client.commands.get(override.CodeName).data.default_member_permissions
158+
};
159+
}
160+
return cmd;
161+
})
162+
163+
await restClient.put(Routes.applicationGuildCommands(Config.Bot.DiscordApplicationId, guild.id), {
164+
body: cmdlist,
165+
});
166+
167+
const ticketCommands = await database.ticketSetups.findMany({
168+
where: {
169+
GuildId: guild.id
170+
}
171+
})
172+
173+
for (const ticketCommand of ticketCommands) {
174+
const clientGuild = await client.guilds.fetch(guild.id);
175+
176+
let guildCommand = null;
177+
try {
178+
guildCommand = await clientGuild.commands.fetch(ticketCommand.SlashCommandId);
179+
} catch {
180+
}
181+
182+
if (!guildCommand) {
183+
guildCommand = await clientGuild.commands.create({
184+
name: ticketCommand.SlashCommandName ?? `open-${ticketCommand.CustomId}-ticket`,
185+
description: ticketCommand.SlashCommandDescription ?? ticketCommand.CustomId,
186+
});
187+
188+
await database.ticketSetups.update({
189+
where: {
190+
CustomId: ticketCommand.CustomId,
191+
},
192+
data: {
193+
SlashCommandId: guildCommand.id,
194+
},
195+
});
196+
} else {
197+
if (
198+
guildCommand.name !== ticketCommand.SlashCommandName ||
199+
guildCommand.description !== ticketCommand.SlashCommandDescription
200+
) {
201+
const updated = await guildCommand.edit({
202+
name: ticketCommand.SlashCommandName ?? guildCommand.name,
203+
description: ticketCommand.SlashCommandDescription ?? guildCommand.description,
204+
});
205+
206+
await database.ticketSetups.update({
207+
where: {CustomId: ticketCommand.CustomId},
208+
data: {SlashCommandId: updated.id},
209+
});
210+
}
211+
}
212+
}
213+
214+
} catch (e) {
215+
Logger.error({
216+
timestamp: new Date().toISOString(),
217+
level: "error",
218+
label: "CommandHelper",
219+
message: `Command loading failed with error: ${e} - Commands failed at ${cmdlist.map((c) => c.name)} commands on Guild \"${guild.name}\" (${guild.id})`,
220+
botType: Config.BotType.toString() || "Unknown",
221+
action: LoggingAction.Command,
222+
});
223+
}
224+
225+
Logger.info({
226+
timestamp: new Date().toISOString(),
227+
level: "info",
228+
label: "CommandHelper",
229+
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 \"${guild.name}\" (${guild.id})`,
230+
botType: Config.BotType.toString() || "Unknown",
231+
action: LoggingAction.Command,
232+
});
233+
}
234+
17235
public static async loadCommands(client: ExtendedClient) {
18236
Logger.info({
19237
timestamp: new Date().toISOString(),
@@ -166,7 +384,6 @@ export class CommandHelper {
166384
body: cmdlist,
167385
});
168386

169-
170387
const ticketCommands = await database.ticketSetups.findMany({
171388
where: {
172389
GuildId: guild.id

src/helper/databaseHelper.ts

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {botData} from "../main/version.js";
55
import {Logger} from "../main/logger.js";
66
import {LoggingAction} from "../enums/loggingTypes.js";
77
import colors from "colors"
8-
import {User} from "discord.js";
8+
import {Guild, User} from "discord.js";
99
import {Config} from "../main/config.js";
1010
import * as cluster from "node:cluster";
1111

@@ -197,6 +197,105 @@ export async function initGuildsToDatabase(client: ExtendedClient) {
197197
}
198198
}
199199

200+
export async function initGuildToDatabase(client: ExtendedClient, guild: Guild) {
201+
try {
202+
// Init Guilds
203+
const guildsData = await database.guilds.findFirst({
204+
where: {
205+
GuildId: guild.id
206+
}
207+
})
208+
const guildCommandManagerData = await database.guildCommandManger.findFirst({
209+
where: {
210+
GuildId: guild.id
211+
}
212+
})
213+
const guildComponentManagerData = await database.guildComponentManager.findFirst({
214+
where: {
215+
GuildId: guild.id
216+
}
217+
})
218+
const guildOwner = await client.guilds.fetch(guild.id)
219+
if (!guildsData) {
220+
await database.guilds.create({
221+
data: {
222+
GuildId: guild.id,
223+
GuildName: guild.name,
224+
GuildOwner: guildOwner.id
225+
}
226+
})
227+
}
228+
if (!guildCommandManagerData) {
229+
await database.guildCommandManger.create({
230+
data: {
231+
Guilds: {
232+
connect: {
233+
GuildId: guild.id,
234+
}
235+
},
236+
Commands: [],
237+
ContextMenus: [],
238+
SubCommands: [],
239+
SubCommandGroups: [],
240+
}
241+
})
242+
}
243+
if (!guildComponentManagerData) {
244+
await database.guildComponentManager.create({
245+
data: {
246+
Guilds: {
247+
connect: {
248+
GuildId: guild.id,
249+
}
250+
},
251+
Buttons: [],
252+
Modals: [],
253+
Selectmenus: []
254+
}
255+
})
256+
}
257+
258+
// Init Guild Users
259+
const clientGuild = await client.guilds.fetch(guild.id)
260+
const guildMembers = await clientGuild.members.fetch()
261+
262+
for (const member of guildMembers.values()) {
263+
await initUsersToDatabase(client, member.user)
264+
}
265+
266+
Logger.info(
267+
{
268+
guildId: "0",
269+
userId: "0",
270+
channelId: "0",
271+
messageId: "0",
272+
timestamp: new Date().toISOString(),
273+
level: "info",
274+
label: "Database",
275+
message: `Database init loaded for Guild ${clientGuild.name} ${guildMembers.size} members!`.gray,
276+
botType: Config.BotType.toString() || "Unknown",
277+
action: LoggingAction.Database,
278+
}
279+
);
280+
281+
} catch (e) {
282+
Logger.error(
283+
{
284+
guildId: "0",
285+
userId: "0",
286+
channelId: "0",
287+
messageId: "0",
288+
timestamp: new Date().toISOString(),
289+
level: "error",
290+
label: "Database",
291+
message: `Database init failed for Guild member init with error ${e}`.red,
292+
botType: Config.BotType.toString() || "Unknown",
293+
action: LoggingAction.Database,
294+
}
295+
);
296+
}
297+
}
298+
200299
export async function initUsersToDatabase(client: ExtendedClient, user: User) {
201300

202301
try {

0 commit comments

Comments
 (0)