DiscordTickets/src/index.js

163 lines
4.3 KiB
JavaScript
Raw Normal View History

/**
2020-10-03 17:08:10 +03:00
*
* @name DiscordTickets
* @author eartharoid <contact@eartharoid.me>
* @license GNU-GPLv3
2020-10-03 17:08:10 +03:00
*
*/
2020-08-24 00:01:21 +03:00
const fs = require('fs');
const path = require('path');
let dev = fs.existsSync('user/dev.env') && fs.existsSync('user/dev.config.js');
require('dotenv').config({path: path.join('user/', dev ? 'dev.env' : '.env')});
module.exports.config = dev ? 'dev.config.js' : 'config.js';
const config = require(path.join('../user/', module.exports.config));
const Discord = require('discord.js');
const client = new Discord.Client({
2020-08-17 16:46:23 +03:00
autoReconnect: true,
2020-08-18 00:07:05 +03:00
partials: ['MESSAGE', 'CHANNEL', 'REACTION'],
});
client.events = new Discord.Collection();
client.commands = new Discord.Collection();
client.cooldowns = new Discord.Collection();
2020-08-23 17:35:24 +03:00
const utils = require('./modules/utils');
2020-08-24 00:01:21 +03:00
const leeks = require('leeks.js');
2020-08-23 17:35:24 +03:00
require('./modules/banner')(leeks); // big coloured text thing
const Logger = require('leekslazylogger');
const log = new Logger({
name: config.name,
logToFile: config.logs.files.enabled,
maxAge: config.logs.files.keep_for,
debug: config.debug
});
2020-08-24 00:01:21 +03:00
2020-08-23 17:35:24 +03:00
require('./modules/updater')(); // check for updates
/**
* storage
*/
const { Sequelize, Model, DataTypes } = require('sequelize');
let sequelize;
2020-10-03 17:08:10 +03:00
switch (config.storage.type) {
case 'mysql':
log.info('Connecting to MySQL database...');
sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASS, {
dialect: 'mysql',
host: process.env.DB_HOST,
logging: log.debug
});
2020-10-03 17:08:10 +03:00
break;
case 'mariadb':
log.info('Connecting to MariaDB database...');
sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASS, {
dialect: 'mariadb',
host: process.env.DB_HOST,
logging: log.debug
});
break;
case 'postgres':
log.info('Connecting to PostgresSQL database...');
sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASS, {
dialect: 'postgres',
host: process.env.DB_HOST,
logging: log.debug
});
break;
case 'microsoft':
log.info('Connecting to Microsoft SQL Server database...');
sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASS, {
dialect: 'mssql',
host: process.env.DB_HOST,
logging: log.debug
});
break;
default:
log.info('Using SQLite storage');
sequelize = new Sequelize({
dialect: 'sqlite',
storage: 'user/storage.db',
logging: log.debug
});
}
class Ticket extends Model {}
Ticket.init({
channel: DataTypes.STRING,
creator: DataTypes.STRING,
open: DataTypes.BOOLEAN,
topic: DataTypes.TEXT
}, {
sequelize,
modelName: 'ticket'
});
2020-08-17 16:46:23 +03:00
class Setting extends Model {}
Setting.init({
key: DataTypes.STRING,
value: DataTypes.STRING,
}, {
sequelize,
modelName: 'setting'
});
Ticket.sync();
2020-08-17 16:46:23 +03:00
Setting.sync();
/**
* event loader
*/
2020-10-03 17:08:10 +03:00
const events = fs.readdirSync('src/events').filter(file => file.endsWith('.js'));
for (const file of events) {
const event = require(`./events/${file}`);
client.events.set(event.event, event);
2020-08-17 16:46:23 +03:00
// client.on(event.event, e => client.events.get(event.event).execute(client, e, Ticket, Setting));
2020-08-24 01:15:39 +03:00
client.on(event.event, (e1, e2) => client.events.get(event.event).execute(client, [e1, e2], {config, Ticket, Setting}));
log.console(log.format(`> Loaded &7${event.event}&f event`));
}
/**
* command loader
*/
2020-10-03 17:08:10 +03:00
const commands = fs.readdirSync('src/commands').filter(file => file.endsWith('.js'));
for (const file of commands) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
log.console(log.format(`> Loaded &7${config.prefix}${command.name}&f command`));
2020-10-03 17:08:10 +03:00
}
log.info(`Loaded ${events.length} events and ${commands.length} commands`);
2020-08-23 17:35:24 +03:00
const one_day = 1000 * 60 * 60 * 24;
const txt = 'user/transcripts/text';
const clean = () => {
const files = fs.readdirSync(txt).filter(file => file.endsWith('.txt'));
let total = 0;
for (const file of files) {
let diff = (new Date() - new Date(fs.statSync(`${txt}/${file}`).mtime));
if (Math.floor(diff / one_day) > config.transcripts.text.keep_for) {
fs.unlinkSync(`${txt}/${file}`);
total++;
}
}
2020-10-03 19:18:07 +03:00
if (total > 0) log.info(`Deleted ${total} old text ${utils.plural('transcript', total)}`);
2020-08-23 17:35:24 +03:00
};
if (config.transcripts.text.enabled) {
clean();
setInterval(clean, one_day);
}
process.on('unhandledRejection', error => {
log.warn('An error was not caught');
2020-08-17 16:46:23 +03:00
log.warn(`Uncaught ${error.name}: ${error.message}`);
log.error(error);
});
client.login(process.env.TOKEN);