Files
SupportBot/Structures/Client.js
T

214 lines
6.9 KiB
JavaScript
Raw Permalink Normal View History

2021-10-04 20:41:05 +01:00
const fs = require("fs");
2025-09-20 23:09:22 +01:00
const path = require("path");
2021-10-04 20:41:05 +01:00
const Discord = require("discord.js");
2022-07-22 13:19:05 +01:00
const { GatewayIntentBits, Partials } = require('discord.js');
2021-10-24 22:41:46 +01:00
const yaml = require("js-yaml");
2025-09-20 23:09:22 +01:00
const { Command, Event } = require('./Addon.js');
2022-08-11 19:19:29 +01:00
2024-06-13 17:36:44 +01:00
const supportbot = yaml.load(fs.readFileSync("./Configs/supportbot.yml", "utf8"));
const cmdconfig = yaml.load(fs.readFileSync("./Configs/commands.yml", "utf8"));
2022-08-11 19:19:29 +01:00
2025-09-22 18:36:04 +01:00
2025-09-20 23:09:22 +01:00
function walk(dir, ext = ".js", fileList = []) {
const files = fs.readdirSync(dir);
for (const file of files) {
const full = `${dir}/${file}`;
const stat = fs.statSync(full);
if (stat.isDirectory()) {
walk(full, ext, fileList);
} else if (file.endsWith(ext)) {
fileList.push(full);
}
}
return fileList;
}
2021-10-04 20:41:05 +01:00
class Client extends Discord.Client {
2021-10-24 22:41:46 +01:00
constructor() {
2024-06-12 12:54:31 +01:00
super({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
2024-11-09 00:37:51 +00:00
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
2024-06-12 12:54:31 +01:00
],
2024-11-09 00:37:51 +00:00
partials: [Partials.Message, Partials.Channel]
2024-06-12 12:54:31 +01:00
});
2021-10-04 20:41:05 +01:00
2021-10-24 22:41:46 +01:00
this.commands = new Discord.Collection();
}
2024-06-13 17:36:44 +01:00
async getChannel(channel, guild) {
2025-09-20 23:09:22 +01:00
if (!channel) return null;
return guild.channels.cache.find(
(c) =>
2022-08-11 19:19:29 +01:00
(c.type === Discord.ChannelType.GuildText || c.type === Discord.ChannelType.GuildNews) &&
2024-06-13 17:36:44 +01:00
(c.id === channel || (c.name && c.name.toLowerCase() === channel.toLowerCase()))
);
}
2024-06-13 17:36:44 +01:00
async getRole(role, guild) {
2025-09-20 23:09:22 +01:00
if (!role) return null;
return guild.roles.cache.find(
2024-06-13 17:36:44 +01:00
(r) => r.id === role || (r.name && r.name.toLowerCase() === role.toLowerCase())
);
}
2024-06-13 17:36:44 +01:00
async getCategory(category, guild) {
2025-09-20 23:09:22 +01:00
if (!category) return null;
return guild.channels.cache.find(
(c) =>
2022-08-11 19:19:29 +01:00
c.type === Discord.ChannelType.GuildCategory &&
2024-06-13 17:36:44 +01:00
(c.id === category || (c.name && c.name.toLowerCase() === category.toLowerCase()))
);
}
2021-10-04 20:41:05 +01:00
2022-01-29 23:57:10 -06:00
async start(token) {
2025-09-20 23:09:22 +01:00
let tempCommandFiles = walk("./Commands");
2022-06-21 01:38:22 +01:00
2022-08-11 19:19:29 +01:00
if (cmdconfig.Suggestion.Enabled === false) {
2025-09-20 23:09:22 +01:00
tempCommandFiles = tempCommandFiles.filter(item => path.basename(item) !== "suggest.js");
}
if (cmdconfig.Suggestion.Enabled === false) {
tempCommandFiles = tempCommandFiles.filter(item => path.basename(item) !== "suggestadmin.js");
}
2022-08-11 19:19:29 +01:00
if (cmdconfig.Help.Enabled === false) {
2025-09-20 23:09:22 +01:00
tempCommandFiles = tempCommandFiles.filter(item => path.basename(item) !== "help.js");
2022-08-11 19:19:29 +01:00
}
if (cmdconfig.Info.Enabled === false) {
2025-09-20 23:09:22 +01:00
tempCommandFiles = tempCommandFiles.filter(item => path.basename(item) !== "info.js");
2022-08-11 19:19:29 +01:00
}
if (cmdconfig.OpenTicket.Enabled === false) {
2025-09-20 23:09:22 +01:00
tempCommandFiles = tempCommandFiles.filter(item => path.basename(item) !== "ticket.js");
2022-08-11 19:19:29 +01:00
}
if (cmdconfig.CloseTicket.Enabled === false) {
2025-09-20 23:09:22 +01:00
tempCommandFiles = tempCommandFiles.filter(item => path.basename(item) !== "close.js");
2022-08-11 19:19:29 +01:00
}
if (cmdconfig.Embed.Enabled === false) {
2025-09-20 23:09:22 +01:00
tempCommandFiles = tempCommandFiles.filter(item => path.basename(item) !== "embed.js");
2022-08-11 19:19:29 +01:00
}
if (cmdconfig.Translate.Enabled === false) {
2025-09-20 23:09:22 +01:00
tempCommandFiles = tempCommandFiles.filter(item => path.basename(item) !== "translate.js");
2022-08-11 19:19:29 +01:00
}
if (cmdconfig.UserInfo.Enabled === false) {
2025-09-20 23:09:22 +01:00
tempCommandFiles = tempCommandFiles.filter(item => path.basename(item) !== "userinfo.js");
2022-08-11 19:19:29 +01:00
}
if (cmdconfig.Ping.Enabled === false) {
2025-09-20 23:09:22 +01:00
tempCommandFiles = tempCommandFiles.filter(item => path.basename(item) !== "Ping.js");
2022-08-11 19:19:29 +01:00
}
2024-07-09 22:49:49 +01:00
if (cmdconfig.AddUser.Enabled === false) {
2025-09-20 23:09:22 +01:00
tempCommandFiles = tempCommandFiles.filter(item => path.basename(item) !== "addUser.js");
2024-06-12 12:54:31 +01:00
}
2024-07-09 22:49:49 +01:00
if (cmdconfig.RemoveUser.Enabled === false) {
2025-09-20 23:09:22 +01:00
tempCommandFiles = tempCommandFiles.filter(item => path.basename(item) !== "removeUser.js");
2024-07-09 22:49:49 +01:00
}
if (cmdconfig.ForceAddUser.Enabled === false) {
2025-09-20 23:09:22 +01:00
tempCommandFiles = tempCommandFiles.filter(item => path.basename(item) !== "forceaddUser.js");
2024-07-09 22:49:49 +01:00
}
if (cmdconfig.Profile.Enabled === false) {
2025-09-20 23:09:22 +01:00
tempCommandFiles = tempCommandFiles.filter(item => path.basename(item) !== "profile.js");
2024-10-10 19:14:45 +01:00
}
if (cmdconfig.Settings.Enabled === false) {
2025-09-20 23:09:22 +01:00
tempCommandFiles = tempCommandFiles.filter(item => path.basename(item) !== "settings.js");
2024-07-09 22:49:49 +01:00
}
2024-11-09 00:37:51 +00:00
if (cmdconfig.Mod.Enabled === false) {
2025-09-20 23:09:22 +01:00
tempCommandFiles = tempCommandFiles.filter(item => path.basename(item) !== "mod.js");
2024-11-09 00:37:51 +00:00
}
const commandFiles = tempCommandFiles;
2025-09-20 23:09:22 +01:00
const commands = commandFiles.map((file) => require(`../${file}`));
2021-10-04 20:41:05 +01:00
2024-07-30 17:45:35 +01:00
console.log(`\u001b[33m`, "――――――――――――――――――――――――――――――――――――――――――――");
2021-10-04 20:41:05 +01:00
2021-10-24 22:41:46 +01:00
commands.forEach((cmd) => {
2022-07-22 13:19:05 +01:00
console.log(`\u001b[32m`, `[CMD]`, `\u001b[37;1m`, `${cmd.name}`, `\u001b[32;1m`, "Loaded");
2021-10-24 22:41:46 +01:00
this.commands.set(cmd.name, cmd);
});
2021-10-21 19:28:23 +01:00
2024-07-30 17:45:35 +01:00
console.log(`\u001b[33m`, "▬▬▬▬▬▬▬ Commands ▬▬▬▬▬▬▬");
2021-12-18 18:57:45 -06:00
2022-08-12 01:35:36 +01:00
if (supportbot.General.Addons.Enabled) {
2024-06-13 17:36:44 +01:00
const addonFiles = fs.readdirSync("./Addons").filter((file) => file.endsWith(".js"));
2022-08-12 01:35:36 +01:00
const addons = addonFiles.map((file) => {
2024-07-03 13:46:57 +01:00
const addon = require(`../Addons/${file}`);
2022-08-12 01:35:36 +01:00
addon.name = file.split(".")[0];
return addon;
});
console.log(" ");
2024-07-30 17:45:35 +01:00
console.log(`\u001b[33m`, "▬▬▬▬▬▬▬ Addons ▬▬▬▬▬▬▬");
2022-08-12 01:35:36 +01:00
addons.forEach((addon) => {
console.log(
`\u001b[32m`,
`[ADDON]`,
`\u001b[37;1m`,
`${addon.name}`,
`\u001b[32;1m`,
"Loaded"
);
2024-07-03 13:46:57 +01:00
if (addon instanceof Command) {
this.commands.set(addon.name, addon);
}
if (addon.events && Array.isArray(addon.events)) {
addon.events.forEach((event) => {
if (event instanceof Event) {
this.on(event.event, (...args) => event.run(this, ...args));
}
});
}
2021-12-18 18:57:45 -06:00
});
2021-10-21 19:28:23 +01:00
2024-07-30 17:45:35 +01:00
console.log(`\u001b[33m`, "▬▬▬▬▬▬▬ Addons ▬▬▬▬▬▬▬");
2024-06-13 17:36:44 +01:00
}
2022-08-12 01:35:36 +01:00
2025-09-22 18:36:04 +01:00
this.once("clientReady", async () => {
2022-02-17 19:57:37 -06:00
await this.guilds.cache.first()?.commands.set(this.commands);
2021-12-18 18:57:45 -06:00
console.log(
2022-07-22 13:19:05 +01:00
"\u001b[32m",
`[SUCCESS]`,
`\u001b[37;1m`,
2022-01-30 00:07:04 -06:00
`Slash Commands Registered for ${this.guilds.cache.first().name}`
2021-10-25 00:13:27 +01:00
);
2021-10-24 22:41:46 +01:00
});
2021-10-21 19:28:23 +01:00
2021-10-24 22:41:46 +01:00
console.log(" ");
2024-07-30 17:45:35 +01:00
console.log(`\u001b[33m`, "▬▬▬▬▬▬▬ Events ▬▬▬▬▬▬▬");
2021-10-21 19:28:23 +01:00
2024-06-13 17:36:44 +01:00
fs.readdirSync("./Events").filter((file) => file.endsWith(".js")).forEach((file) => {
const event = require(`../Events/${file}`);
console.log(`\u001b[32m`, `[EVENT]`, `\u001b[37;1m`, `${event.event}`, `\u001b[32;1m`, "Loaded");
this.on(event.event, (...args) => event.run(this, ...args));
});
2021-10-21 19:28:23 +01:00
2024-07-30 17:45:35 +01:00
console.log(`\u001b[33m`, "▬▬▬▬▬▬▬ Events ▬▬▬▬▬▬▬");
2022-01-29 23:57:10 -06:00
if (process.argv[2] !== "test") {
await this.login(token);
2021-12-04 21:19:51 -06:00
} else {
console.log(
2022-01-30 00:07:04 -06:00
`\u001b[31;1m`,
"RUNNING IN TEST MODE, IF NOT INTENDED, PLEASE USE npm start"
2021-12-04 21:19:51 -06:00
);
}
2021-10-24 22:41:46 +01:00
}
2021-10-04 20:41:05 +01:00
}
2025-09-20 23:09:22 +01:00
module.exports = Client;