JaBa/base/Client.js

397 lines
14 KiB
JavaScript
Raw Normal View History

const { Client, Collection, SlashCommandBuilder, ContextMenuCommandBuilder, EmbedBuilder, PermissionsBitField, ChannelType } = require("discord.js"),
2022-01-04 02:18:28 +05:00
{ GiveawaysManager } = require("discord-giveaways"),
{ REST } = require("@discordjs/rest"),
2024-04-29 15:57:02 +05:00
{ LavalinkManager } = require("lavalink-client"),
2023-04-21 22:47:21 +05:00
{ Routes } = require("discord-api-types/v10");
2022-01-04 02:18:28 +05:00
const BaseEvent = require("./BaseEvent.js"),
BaseCommand = require("./BaseCommand.js"),
2022-01-04 02:18:28 +05:00
path = require("path"),
fs = require("fs").promises,
mongoose = require("mongoose");
2022-01-04 02:18:28 +05:00
2023-11-05 16:03:23 +05:00
class JaBaClient extends Client {
2022-01-04 02:18:28 +05:00
constructor(options) {
super(options);
this.config = require("../config");
this.customEmojis = require("../emojis");
this.languages = require("../languages/language-meta");
this.commands = new Collection();
this.logger = require("../helpers/logger");
this.wait = require("node:timers/promises").setTimeout;
this.functions = require("../helpers/functions");
this.guildsData = require("../base/Guild");
this.usersData = require("../base/User");
this.membersData = require("../base/Member");
2023-06-26 17:25:17 +05:00
this.dashboard = require("../dashboard/dashboard");
2022-01-04 02:18:28 +05:00
this.databaseCache = {};
this.databaseCache.users = new Collection();
this.databaseCache.guilds = new Collection();
this.databaseCache.members = new Collection();
2022-08-29 21:31:36 +05:00
this.databaseCache.usersReminds = new Collection();
2024-04-29 15:57:02 +05:00
this.lavalink = new LavalinkManager({
nodes: [this.config.lavalinkNodes],
sendToShard: (guildId, payload) => this.guilds.cache.get(guildId)?.shard?.send(payload),
client: {
id: this.config.userId,
username: "JaBa",
},
autoSkip: true,
playerOptions: {
defaultSearchPlatform: "ytmsearch",
volumeDecrementer: 1,
onDisconnect: {
autoReconnect: false,
destroyPlayer: true,
},
onEmptyQueue: {
destroyAfterMs: 5000,
},
},
});
2023-10-31 20:35:12 +05:00
2024-04-29 15:57:02 +05:00
this.lavalink.on("trackStart", async (player, track) => {
const guildData = await this.findOrCreateGuild(player.guildId),
channel = this.channels.cache.get(player.textChannelId);
const m = (
2024-04-29 15:57:02 +05:00
await channel.send({
content: this.translate("music/play:NOW_PLAYING", { songName: `${track.info.title} - ${track.info.author}` }, guildData.language),
})
).id;
2024-04-29 15:57:02 +05:00
if (track.info.duration > 1)
setTimeout(() => {
2024-04-29 15:57:02 +05:00
const message = channel.messages.cache.get(m);
if (message && message.deletable) message.delete();
2024-04-29 15:57:02 +05:00
}, track.info.duration);
else
setTimeout(() => {
2024-04-29 15:57:02 +05:00
const message = channel.messages.cache.get(m);
if (message && message.deletable) message.delete();
}, 5 * 60 * 1000);
});
2024-04-29 15:57:02 +05:00
this.lavalink.on("queueEnd", async player => {
const guildData = await this.findOrCreateGuild(player.guildId),
channel = this.channels.cache.get(player.textChannelId);
channel.send(this.translate("music/play:QUEUE_ENDED", null, guildData.language));
});
2024-04-29 15:57:02 +05:00
this.lavalink.on("trackError", async player => {
const guildData = await this.findOrCreateGuild(player.guildId),
channel = this.channels.cache.get(player.textChannelId);
channel.send({ content: this.translate("music/play:ERR_OCCURRED", null, guildData.language) });
});
2022-01-04 02:18:28 +05:00
this.giveawaysManager = new GiveawaysManager(this, {
storage: "./giveaways.json",
default: {
botsCanWin: false,
embedColor: this.config.embed.color,
embedColorEnd: "#FF0000",
reaction: "🎉",
},
2022-01-04 02:18:28 +05:00
});
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
/**
2024-04-07 15:29:16 +05:00
* Initializes the client by logging in with the provided token and connecting to the MongoDB database.
*
* This method is called during the client's startup process to set up the necessary connections and resources.
*
* @returns {Promise<void>} A Promise that resolves when the client is fully initialized.
*/
async init() {
this.login(this.config.token);
2023-07-05 00:58:06 +05:00
mongoose
2023-10-19 22:48:38 +05:00
.connect(this.config.mongoDB)
2023-07-05 00:58:06 +05:00
.then(() => {
this.logger.log("Connected to the Mongodb database.");
2023-07-05 00:58:06 +05:00
})
.catch(err => {
this.logger.error(`Unable to connect to the Mongodb database.\nError: ${err}`);
2023-07-05 00:58:06 +05:00
});
2023-06-26 17:25:17 +05:00
// const autoUpdateDocs = require("../helpers/autoUpdateDocs");
// autoUpdateDocs.update(this);
}
/**
2024-04-07 15:29:16 +05:00
* Loads all the commands from the specified directory and registers them with the Discord API.
*
* This method is responsible for dynamically loading all the command files from the specified directory,
* creating instances of the corresponding command classes, and registering the commands with the Discord API.
* It also handles any additional setup or initialization required by the loaded commands.
*
* @param {string} dir - The directory path where the command files are located.
* @returns {Promise<void>} A Promise that resolves when all the commands have been loaded and registered.
*/
async loadCommands(dir) {
2022-12-17 13:49:03 +05:00
const rest = new REST().setToken(this.config.token),
filePath = path.join(__dirname, dir),
2023-07-05 00:58:06 +05:00
folders = (await fs.readdir(filePath)).map(file => path.join(filePath, file)).filter(async path => (await fs.lstat(path)).isDirectory());
const commands = [];
for (let index = 0; index < folders.length; index++) {
const folder = folders[index];
if (folder.endsWith("!DISABLED")) continue;
const files = await fs.readdir(folder);
for (let index = 0; index < files.length; index++) {
const file = files[index];
if (file.endsWith(".js")) {
const Command = require(path.join(folder, file));
if (Command.prototype instanceof BaseCommand) {
const command = new Command(this);
this.commands.set(command.command.name, command);
if (command.onLoad && typeof command.onLoad === "function") await command.onLoad(this);
commands.push(command.command instanceof SlashCommandBuilder || command.command instanceof ContextMenuCommandBuilder ? command.command.toJSON() : command.command);
this.logger.log(`Successfully loaded "${file}" command file. (Command: ${command.command.name})`);
}
}
}
}
2022-01-04 02:18:28 +05:00
try {
2024-02-28 18:35:51 +05:00
if (this.config.production) await rest.put(Routes.applicationCommands(this.config.userId), { body: commands });
else await rest.put(Routes.applicationGuildCommands(this.config.userId, this.config.support.id), { body: commands });
this.logger.log("Successfully registered application commands.");
} catch (err) {
2024-02-28 18:35:51 +05:00
console.log(err);
}
}
/**
2024-04-07 15:29:16 +05:00
* Returns the default language from the list of available languages.
* @returns {Language} The default language.
*/
get defaultLanguage() {
2024-02-09 23:26:57 +05:00
return this.languages.find(language => language.default);
}
/**
2024-04-07 15:29:16 +05:00
* Translates a key using the specified locale, or the default language if no locale is provided.
* @param {string} key The translation key to look up.
* @param {any[]} args Any arguments to pass to the translation function.
* @param {string} [locale=this.defaultLanguage.name] The locale to use for the translation. Defaults to the default language.
* @returns {string} The translated string.
*/
2024-02-09 23:26:57 +05:00
translate(key, args, locale = this.defaultLanguage.name) {
const lang = this.translations.get(locale);
return lang(key, args);
}
/**
2024-04-07 15:29:16 +05:00
* Generates an EmbedBuilder instance with the provided data.
* @param {Object} data - The data to use for the embed.
* @param {string} [data.title] - The title of the embed.
* @param {string} [data.description] - The description of the embed.
* @param {string} [data.thumbnail] - The URL of the thumbnail image for the embed.
* @param {Object[]} [data.fields] - An array of field objects for the embed.
* @param {string} [data.image] - The URL of the image for the embed.
* @param {string} [data.url] - The URL to be used as the embed's hyperlink.
2024-04-29 15:57:02 +05:00
* @param {string} [data.color] - The HEX color of the embed's border. If not provided, the default color from the client's configuration will be used.
2024-04-07 15:29:16 +05:00
* @param {string} [data.footer] - The text to be displayed as the embed's footer. If not provided, the default footer from the client's configuration will be used.
* @param {Date} [data.timestamp] - The timestamp to be displayed in the embed's footer. If not provided, the current timestamp will be used.
* @param {string|Object} [data.author] - The author information for the embed. Can be a string (name) or an object with `name` and/or `iconURL` properties.
* @returns {EmbedBuilder} The generated EmbedBuilder instance.
*/
embed(data) {
const embed = new EmbedBuilder()
.setTitle(data.title || null)
.setDescription(data.description || null)
.setThumbnail(data.thumbnail || null)
.addFields(data.fields || [])
.setImage(data.image || null)
2024-02-14 21:16:13 +05:00
.setURL(data.url || null);
if (data.color) embed.setColor(data.color);
else if (data.color === null) embed.setColor(null);
else embed.setColor(this.config.embed.color);
if (data.footer) embed.setFooter(data.footer);
else if (data.footer === null) embed.setFooter(null);
else embed.setFooter(this.config.embed.footer);
2024-02-09 21:30:11 +05:00
if (data.timestamp) embed.setTimestamp(data.timestamp);
else if (data.timestamp === null) embed.setTimestamp(null);
else embed.setTimestamp();
2024-02-14 21:16:13 +05:00
if (!data.author || data.author === null) embed.setAuthor(null);
else if (typeof data.author === "string") embed.setAuthor({ name: data.author, iconURL: this.user.avatarURL() });
2024-04-07 15:29:16 +05:00
else if (typeof data.author === "object" && (data.author.iconURL !== null || data.author.iconURL !== undefined)) embed.setAuthor({ name: data.author.name, iconURL: data.author.iconURL });
else embed.setAuthor(data.author);
return embed;
}
/**
2024-04-07 15:29:16 +05:00
* Creates an invite for the specified guild.
* @param {string} guildId - The ID of the guild to create the invite for.
* @returns {Promise<string>} The URL of the created invite, or an error message if no suitable channel was found or the bot lacks the necessary permissions.
*/
async createInvite(guildId) {
const guild = this.guilds.cache.get(guildId),
member = guild.members.me,
channel = guild.channels.cache.find(ch => ch.permissionsFor(member.id).has(PermissionsBitField.FLAGS.CREATE_INSTANT_INVITE) && (ch.type === ChannelType.GuildText || ch.type === ChannelType.GuildVoice));
if (channel) return (await channel.createInvite()).url || "No channels found or missing permissions";
}
/**
2024-04-07 15:29:16 +05:00
* Loads a command from the specified directory and file.
* @param {string} dir - The directory containing the command file.
* @param {string} file - The name of the command file (without the .js extension).
* @returns {Promise<string>} A log message indicating the successful loading of the command.
*/
async loadCommand(dir, file) {
const Command = require(path.join(dir, `${file}.js`));
if (!(Command.prototype instanceof BaseCommand)) return this.logger.error("Tried to load a non-command file!");
const command = new Command(this);
this.commands.set(command.command.name, command);
if (command.onLoad && typeof command.onLoad === "function") await command.onLoad(this);
return this.logger.log(`Successfully loaded "${file}" command file. (Command: ${command.command.name})`);
}
/**
2024-04-07 15:29:16 +05:00
* Unloads a command from the specified directory and file.
* @param {string} dir - The directory containing the command file.
* @param {string} name - The name of the command file (without the .js extension).
* @returns {void} This method does not return a value.
*/
async unloadCommand(dir, name) {
delete require.cache[require.resolve(`${dir}${path.sep}${name}.js`)];
return;
}
/**
2024-04-07 15:29:16 +05:00
* Loads all event files from the specified directory and its subdirectories.
* @param {string} dir - The directory containing the event files.
* @returns {void} This method does not return a value.
*/
async loadEvents(dir) {
const filePath = path.join(__dirname, dir);
const files = await fs.readdir(filePath);
for (let index = 0; index < files.length; index++) {
const file = files[index];
const stat = await fs.lstat(path.join(filePath, file));
if (stat.isDirectory()) this.loadEvents(path.join(dir, file));
if (file.endsWith(".js")) {
const Event = require(path.join(filePath, file));
if (Event.prototype instanceof BaseEvent) {
const event = new Event();
if (!event.name || !event.name.length) return console.error(`Cannot load "${file}" event file: Event name is not set!`);
if (event.once) this.once(event.name, event.execute.bind(event, this));
else this.on(event.name, event.execute.bind(event, this));
this.logger.log(`Successfully loaded "${file}" event file. (Event: ${event.name})`);
}
}
2022-01-13 00:26:23 +05:00
}
}
2022-01-04 02:18:28 +05:00
/**
2024-04-07 15:29:16 +05:00
* Finds or creates a user in the database based on the provided user ID.
* @param {string} userID - The ID of the user to find or create.
2024-04-29 15:57:02 +05:00
* @returns {Promise<import("./User")>} The user data object, either retrieved from the database or newly created.
*/
2023-10-30 21:13:12 +05:00
async findOrCreateUser(userID) {
let userData = await this.usersData.findOne({ id: userID });
2023-07-03 19:30:47 +05:00
if (userData) {
this.databaseCache.users.set(userID, userData);
2022-01-04 02:18:28 +05:00
return userData;
} else {
userData = new this.usersData({ id: userID });
2023-10-30 21:45:10 +05:00
await userData.save();
2023-07-03 19:30:47 +05:00
this.databaseCache.users.set(userID, userData);
2022-01-04 02:18:28 +05:00
return userData;
2022-01-13 00:26:23 +05:00
}
}
2022-01-04 02:18:28 +05:00
/**
2024-04-07 15:29:16 +05:00
* Finds or creates a member in the database based on the provided member ID and guild ID.
* @param {Object} options - The options for finding or creating the member.
* @param {string} options.id - The ID of the member to find or create.
* @param {string} options.guildId - The ID of the guild the member belongs to.
2024-04-29 15:57:02 +05:00
* @returns {Promise<import("./Member")>} The member data object, either retrieved from the database or newly created.
*/
2023-07-03 19:30:47 +05:00
async findOrCreateMember({ id: memberID, guildId }) {
let memberData = await this.membersData.findOne({ guildID: guildId, id: memberID });
if (memberData) {
this.databaseCache.members.set(`${memberID}${guildId}`, memberData);
return memberData;
} else {
memberData = new this.membersData({ id: memberID, guildID: guildId });
2023-10-30 21:45:10 +05:00
2023-07-03 19:30:47 +05:00
await memberData.save();
2023-10-15 15:05:10 +05:00
const guildData = await this.findOrCreateGuild(guildId);
2022-01-04 02:18:28 +05:00
2023-07-03 19:30:47 +05:00
if (guildData) {
guildData.members.push(memberData._id);
await guildData.save();
2022-01-13 00:26:23 +05:00
}
2023-07-03 19:30:47 +05:00
this.databaseCache.members.set(`${memberID}${guildId}`, memberData);
return memberData;
2022-01-13 00:26:23 +05:00
}
}
2022-01-04 02:18:28 +05:00
/**
2024-04-07 15:29:16 +05:00
* Finds or creates a guild in the database based on the provided guild ID.
* @param {string} guildId - The ID of the guild to find or create.
2024-04-29 15:57:02 +05:00
* @returns {Promise<import("./Guild")>} The guild data object, either retrieved from the database or newly created.
*/
2023-10-15 15:05:10 +05:00
async findOrCreateGuild(guildId) {
2023-07-03 19:30:47 +05:00
let guildData = await this.guildsData.findOne({ id: guildId }).populate("members");
2022-01-04 02:18:28 +05:00
2023-07-03 19:30:47 +05:00
if (guildData) {
this.databaseCache.guilds.set(guildId, guildData);
2022-01-04 02:18:28 +05:00
2023-07-03 19:30:47 +05:00
return guildData;
} else {
guildData = new this.guildsData({ id: guildId });
2023-10-30 21:45:10 +05:00
2023-07-03 19:30:47 +05:00
await guildData.save();
this.databaseCache.guilds.set(guildId, guildData);
return guildData;
2022-01-13 00:26:23 +05:00
}
}
}
2022-01-04 02:18:28 +05:00
2023-11-05 16:03:23 +05:00
module.exports = JaBaClient;