2022-08-02 19:25:53 +03:00
|
|
|
const { FrameworkClient } = require('@eartharoid/dbf');
|
2022-08-06 00:21:55 +03:00
|
|
|
const {
|
2023-01-13 22:48:37 +02:00
|
|
|
GatewayIntentBits,
|
|
|
|
Partials,
|
2022-08-06 00:21:55 +03:00
|
|
|
} = require('discord.js');
|
2022-05-06 02:27:27 +03:00
|
|
|
const { PrismaClient } = require('@prisma/client');
|
2022-07-16 01:19:42 +03:00
|
|
|
const Keyv = require('keyv');
|
2022-07-17 00:18:50 +03:00
|
|
|
const I18n = require('@eartharoid/i18n');
|
|
|
|
const fs = require('fs');
|
|
|
|
const { join } = require('path');
|
|
|
|
const YAML = require('yaml');
|
2022-08-06 00:21:55 +03:00
|
|
|
const TicketManager = require('./lib/tickets/manager');
|
2022-07-23 22:28:48 +03:00
|
|
|
const sqliteMiddleware = require('./lib/middleware/prisma-sqlite');
|
2023-06-20 22:59:08 +03:00
|
|
|
const ms = require('ms');
|
2022-05-05 23:29:28 +03:00
|
|
|
|
|
|
|
module.exports = class Client extends FrameworkClient {
|
2022-07-17 00:18:50 +03:00
|
|
|
constructor(config, log) {
|
2024-01-11 02:08:57 +02:00
|
|
|
super(
|
|
|
|
{
|
|
|
|
intents: [
|
|
|
|
...[
|
|
|
|
GatewayIntentBits.DirectMessages,
|
|
|
|
GatewayIntentBits.DirectMessageReactions,
|
|
|
|
GatewayIntentBits.DirectMessageTyping,
|
|
|
|
GatewayIntentBits.MessageContent,
|
|
|
|
GatewayIntentBits.Guilds,
|
|
|
|
GatewayIntentBits.GuildMembers,
|
|
|
|
GatewayIntentBits.GuildMessages,
|
|
|
|
],
|
|
|
|
...(process.env.PUBLIC_BOT !== 'true' ? [GatewayIntentBits.GuildPresences] : []),
|
2023-06-30 01:45:15 +03:00
|
|
|
],
|
2024-01-11 02:08:57 +02:00
|
|
|
partials: [
|
|
|
|
Partials.Channel,
|
|
|
|
Partials.Message,
|
|
|
|
Partials.Reaction,
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{ baseDir: __dirname },
|
|
|
|
);
|
2022-07-17 00:18:50 +03:00
|
|
|
|
|
|
|
const locales = {};
|
|
|
|
fs.readdirSync(join(__dirname, 'i18n'))
|
|
|
|
.filter(file => file.endsWith('.yml'))
|
|
|
|
.forEach(file => {
|
|
|
|
const data = fs.readFileSync(join(__dirname, 'i18n/' + file), { encoding: 'utf8' });
|
|
|
|
const name = file.slice(0, file.length - 4);
|
|
|
|
locales[name] = YAML.parse(data);
|
|
|
|
});
|
|
|
|
|
2024-01-15 04:45:29 +02:00
|
|
|
this.keyv = new Keyv();
|
2022-07-17 00:18:50 +03:00
|
|
|
/** @type {I18n} */
|
|
|
|
this.i18n = new I18n('en-GB', locales);
|
2022-08-06 00:21:55 +03:00
|
|
|
/** @type {TicketManager} */
|
|
|
|
this.tickets = new TicketManager(this);
|
2022-07-17 00:18:50 +03:00
|
|
|
this.config = config;
|
|
|
|
this.log = log;
|
2022-07-18 23:53:17 +03:00
|
|
|
this.supers = (process.env.SUPER ?? '').split(',');
|
2024-01-15 04:45:29 +02:00
|
|
|
/** @param {import('discord.js/typings').Interaction} interaction */
|
|
|
|
this.commands.interceptor = async interaction => {
|
|
|
|
if (!interaction.inGuild()) return;
|
|
|
|
const id = interaction.guildId;
|
|
|
|
const cacheKey = `cache/known/guild:${id}`;
|
|
|
|
if (await this.keyv.has(cacheKey)) return;
|
|
|
|
await this.prisma.guild.upsert({
|
|
|
|
create: {
|
|
|
|
id,
|
|
|
|
locale: this.i18n.locales.find(locale => locale === interaction.guild.preferredLocale), // undefined if not supported
|
|
|
|
},
|
|
|
|
update: {},
|
|
|
|
where: { id },
|
|
|
|
});
|
|
|
|
await this.keyv.set(cacheKey, true);
|
|
|
|
};
|
2022-05-05 23:29:28 +03:00
|
|
|
}
|
2022-05-06 02:27:27 +03:00
|
|
|
|
2022-05-05 23:29:28 +03:00
|
|
|
async login(token) {
|
2023-06-25 15:43:10 +03:00
|
|
|
const levels = ['error', 'info', 'warn'];
|
|
|
|
if (this.config.logs.level === 'debug') levels.push('query');
|
|
|
|
|
2024-01-11 02:08:57 +02:00
|
|
|
const prisma_options = {
|
2023-06-25 15:43:10 +03:00
|
|
|
log: levels.map(level => ({
|
|
|
|
emit: 'event',
|
|
|
|
level,
|
|
|
|
})),
|
2024-01-11 02:08:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (process.env.DB_PROVIDER === 'sqlite' && !process.env.DB_CONNECTION_URL) {
|
|
|
|
prisma_options.datasources = { db: { url:'file:' + join(process.cwd(), './user/database.db') } };
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @type {PrismaClient} */
|
|
|
|
this.prisma = new PrismaClient(prisma_options);
|
2023-06-25 15:43:10 +03:00
|
|
|
|
|
|
|
this.prisma.$on('error', e => this.log.error.prisma(`${e.target} ${e.message}`));
|
|
|
|
this.prisma.$on('info', e => this.log.info.prisma(`${e.target} ${e.message}`));
|
|
|
|
this.prisma.$on('warn', e => this.log.warn.prisma(`${e.target} ${e.message}`));
|
|
|
|
this.prisma.$on('query', e => this.log.debug.prisma(e));
|
2023-06-20 22:59:08 +03:00
|
|
|
|
2023-01-30 17:56:41 +02:00
|
|
|
if (process.env.DB_PROVIDER === 'sqlite') {
|
2023-06-20 22:59:08 +03:00
|
|
|
// rewrite queries that use unsupported features
|
2023-01-30 17:56:41 +02:00
|
|
|
this.prisma.$use(sqliteMiddleware);
|
2023-06-20 22:59:08 +03:00
|
|
|
// make sqlite faster (missing parentheses are not a mistake, `$queryRaw` is a tagged template literal)
|
2023-06-17 19:01:06 +03:00
|
|
|
this.log.debug(await this.prisma.$queryRaw`PRAGMA journal_mode=WAL;`); // https://www.sqlite.org/wal.html
|
|
|
|
this.log.debug(await this.prisma.$queryRaw`PRAGMA synchronous=normal;`); // https://www.sqlite.org/pragma.html#pragma_synchronous
|
2023-06-20 22:59:08 +03:00
|
|
|
|
|
|
|
setInterval(async () => {
|
|
|
|
this.log.debug(await this.prisma.$queryRaw`PRAGMA optimize;`); // https://www.sqlite.org/pragma.html#pragma_optimize
|
|
|
|
}, ms('6h'));
|
2023-01-30 17:56:41 +02:00
|
|
|
}
|
2023-06-20 22:59:08 +03:00
|
|
|
|
2022-05-05 23:29:28 +03:00
|
|
|
return super.login(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
async destroy() {
|
|
|
|
await this.prisma.$disconnect();
|
|
|
|
return super.destroy();
|
|
|
|
}
|
2023-03-23 23:48:46 +02:00
|
|
|
};
|