JaBa/base/JaBa.js

324 lines
10 KiB
JavaScript
Raw Normal View History

const { Client, Collection, SlashCommandBuilder, ContextMenuCommandBuilder } = require("discord.js"),
{ Player } = require("discord-player"),
2023-10-31 20:35:12 +05:00
{ YandexMusicExtractor } = require("discord-player-yandexmusic"),
2022-01-04 02:18:28 +05:00
{ GiveawaysManager } = require("discord-giveaways"),
{ REST } = require("@discordjs/rest"),
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
class JaBa extends Client {
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");
this.states = {};
2022-01-04 02:18:28 +05:00
this.knownGuilds = [];
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();
2023-10-31 20:35:12 +05:00
this.player = new Player(this);
this.player.extractors.loadDefault();
this.player.extractors.register(YandexMusicExtractor, { access_token: this.config.apiKeys.yandex.token, uid: this.config.apiKeys.yandex.uid });
this.player.events.on("playerStart", async (queue, track) => {
const m = await queue.metadata.channel.send({ content: this.translate("music/play:NOW_PLAYING", { songName: track.title }, queue.metadata.channel.guild.data.language) });
if (track.durationMS > 1)
setTimeout(() => {
if (m.deletable) m.delete();
}, track.durationMS);
else
setTimeout(() => {
if (m.deletable) m.delete();
2023-07-05 00:58:06 +05:00
}, 5 * 60 * 1000); // m * s * ms
});
this.player.events.on("emptyQueue", queue => queue.metadata.channel.send(this.translate("music/play:QUEUE_ENDED", null, queue.metadata.channel.guild.data.language)));
this.player.events.on("emptyChannel", queue => queue.metadata.channel.send(this.translate("music/play:STOP_EMPTY", null, queue.metadata.channel.guild.data.language)));
this.player.events.on("playerError", (queue, e) => {
queue.metadata.channel.send({ content: this.translate("music/play:ERR_OCCURRED", { error: e.message }, queue.metadata.channel.guild.data.language) });
2023-10-31 20:35:12 +05:00
console.log(e);
});
this.player.events.on("error", (queue, e) => {
queue.metadata.channel.send({ content: this.translate("music/play:ERR_OCCURRED", { error: e.message }, queue.metadata.channel.guild.data.language) });
2023-10-31 20:35:12 +05:00
console.log(e);
});
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
/**
2023-07-19 21:43:56 +05:00
* Login into account and connect to DB
*/
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.", "log");
})
.catch(err => {
2023-10-19 22:48:38 +05:00
this.logger.log(`Unable to connect to the Mongodb database.\nError: ${err}`, "error");
2023-07-05 00:58:06 +05:00
});
await this.player.extractors.loadDefault();
2023-06-26 17:25:17 +05:00
// const autoUpdateDocs = require("../helpers/autoUpdateDocs");
// autoUpdateDocs.update(this);
}
/**
2023-07-19 21:43:56 +05:00
* Loads commands from directory
* @param {String} dir Directory where's all commands located
* @returns
*/
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);
const aliases = [];
if (command.aliases && Array.isArray(command.aliases) && command.aliases.length > 0)
2023-02-01 00:23:01 +05:00
command.aliases.forEach(alias => {
2023-07-05 00:58:06 +05:00
const command_alias = command.command instanceof SlashCommandBuilder || command.command instanceof ContextMenuCommandBuilder ? { ...command.command.toJSON() } : { ...command.command };
command_alias.name = alias;
aliases.push(command_alias);
this.commands.set(alias, command);
});
2023-07-05 00:58:06 +05:00
commands.push(command.command instanceof SlashCommandBuilder || command.command instanceof ContextMenuCommandBuilder ? command.command.toJSON() : command.command, ...aliases);
if (command.onLoad || typeof command.onLoad === "function") await command.onLoad(this);
this.logger.log(`Successfully loaded "${file}" command file. (Command: ${command.command.name})`);
}
}
}
}
2022-01-04 02:18:28 +05:00
try {
if (this.config.production)
2023-07-05 00:58:06 +05:00
await rest.put(Routes.applicationCommands(this.config.userId), {
body: commands,
});
else
2023-07-05 00:58:06 +05:00
await rest.put(Routes.applicationGuildCommands(this.config.userId, this.config.support.id), {
body: commands,
});
this.logger.log("Successfully registered application commands.");
} catch (err) {
this.logger.log("Cannot load commands: " + err.message, "error");
}
}
/**
2023-07-19 21:43:56 +05:00
* Loads single command in directory
* @param {String} dir Directory where command is
* @param {String} file Filename of the command
*/
async loadCommand(dir, file) {
const Command = require(path.join(dir, `${file}.js`));
if (Command.prototype instanceof BaseCommand) {
const command = new Command(this);
this.commands.set(command.command.name, command);
const aliases = [];
if (command.aliases && Array.isArray(command.aliases) && command.aliases.length > 0)
2023-02-01 00:23:01 +05:00
command.aliases.forEach(alias => {
const command_alias = command.command instanceof SlashCommandBuilder ? { ...command.command.toJSON() } : { ...command.command };
command_alias.name = alias;
aliases.push(command_alias);
this.commands.set(alias, command);
});
if (command.onLoad || typeof command.onLoad === "function") await command.onLoad(this);
this.logger.log(`Successfully loaded "${file}" command file. (Command: ${command.command.name})`);
return;
}
}
/**
2023-07-19 21:43:56 +05:00
* Unloads command from cache
* @param {String} dir Directory of the command
* @param {String} name Name of the command
*/
async unloadCommand(dir, name) {
delete require.cache[require.resolve(`${dir}${path.sep}${name}.js`)];
return;
}
/**
2023-07-19 21:43:56 +05:00
* Loads events from directory
* @param {String} dir Directory where's all events located
* @returns
*/
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
/**
2023-07-19 21:43:56 +05:00
* @returns {String} Bot's default language
*/
get defaultLanguage() {
return this.languages.find(language => language.default).name;
}
/**
2023-07-19 21:43:56 +05:00
* Translates from a key to language
* @param {String} key Key
* @param {Array} args Arguments for translation
* @param {String} locale Language
*/
translate(key, args, locale = this.defaultLanguage) {
const language = this.translations.get(locale);
if (!language) throw "Invalid language set in data.";
return language(key, args);
}
/**
2023-07-19 21:43:56 +05:00
* Finds or creates user in DB
2023-10-30 21:13:12 +05:00
* @param {String} userID User ID
* @returns {import("./User")} Mongoose model
*/
2023-10-30 21:13:12 +05:00
async findOrCreateUser(userID) {
2023-07-03 19:30:47 +05:00
if (this.databaseCache.users.get(userID)) return this.databaseCache.users.get(userID);
2022-01-04 02:18:28 +05:00
else {
2023-07-03 19:30:47 +05:00
let userData = await this.usersData.findOne({ id: userID });
2022-01-04 02:18:28 +05:00
if (userData) {
2023-07-03 19:30:47 +05:00
this.databaseCache.users.set(userID, userData);
2022-01-04 02:18:28 +05:00
return userData;
} else {
2023-07-03 19:30:47 +05:00
userData = new this.usersData({ id: userID });
2023-10-30 21:45:10 +05:00
userData.markModified();
2022-01-04 02:18:28 +05:00
await userData.save();
2023-07-03 19:30:47 +05:00
2022-01-04 02:18:28 +05:00
this.databaseCache.users.set(userID, userData);
2023-07-03 19:30:47 +05:00
return userData;
2022-01-13 00:26:23 +05:00
}
}
}
2022-01-04 02:18:28 +05:00
/**
2023-07-19 21:43:56 +05:00
* Finds or creates member in DB
2023-10-30 21:13:12 +05:00
* @param {Array} { id: Member ID, Guild ID }
* @returns {import("./Member")} Mongoose model
*/
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
memberData.markModified();
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);
2023-10-30 21:45:10 +05:00
guildData.markModified("members");
2023-07-03 19:30:47 +05:00
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
/**
2023-07-19 21:43:56 +05:00
* Finds or creates guild in DB
2023-10-30 21:13:12 +05:00
* @param {String} guildId Guild ID
* @returns {import("./Guild")} Mongoose model
*/
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
guildData.markModified();
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-07-05 00:58:06 +05:00
module.exports = JaBa;