DiscordTickets/src/index.js

169 lines
4.7 KiB
JavaScript
Raw Normal View History

/**
2021-03-15 23:12:58 +02:00
* Discord Tickets
2021-02-15 20:34:59 +02:00
* Copyright (C) 2021 Isaac Saunders
*
2021-02-15 20:34:59 +02:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
2021-02-15 20:34:59 +02:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
2021-02-15 20:34:59 +02:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
2021-02-26 18:07:20 +02:00
* @name @eartharoid/discord-tickets
2021-02-20 19:09:08 +02:00
* @description An open-source Discord bot for ticket management
2021-02-15 20:34:59 +02:00
* @copyright 2021 Isaac Saunders
* @license GNU-GPLv3
*/
const node_version = Number(process.versions.node.split('.')[0]);
2021-02-17 15:24:33 +02:00
if (node_version < 14)
2021-03-15 23:12:58 +02:00
return console.log(`Error: Discord Tickets does not work on Node v${node_version}. Please upgrade to v14 or above.`);
const fs = require('fs');
const { path } = require('./utils/fs');
2021-02-17 15:24:33 +02:00
const checkFile = (file, example) => {
file = path(file);
example = path(example);
if (fs.existsSync(file)) return true;
if (!fs.existsSync(example)) {
console.log(`Error: '${file}' not found, and unable to create it due to '${example}' being missing`);
return process.exit();
}
console.log(`Copying '${example}' to '${file}'`);
fs.copyFileSync(example, file);
return false;
};
if (!checkFile('./.env', './example.env')) {
console.log('Please set your bot\'s token in \'.env\'');
process.exit();
}
checkFile('./user/config.js', './user/example.config.js');
2021-02-16 16:48:04 +02:00
require('dotenv').config({
path: path('./.env')
});
const config = require('../user/config');
2021-02-17 00:34:20 +02:00
require('./banner')();
2021-02-27 23:13:46 +02:00
const log = require('./logger');
const { selectPresence } = require('./utils/discord');
2021-02-16 16:48:04 +02:00
const I18n = require('@eartharoid/i18n');
const CommandManager = require('./modules/commands/manager');
const PluginManager = require('./modules/plugins/manager');
const TicketManager = require('./modules/tickets/manager');
2021-02-17 00:34:20 +02:00
2021-02-20 19:09:08 +02:00
require('./modules/structures')(); // load extended structures before creating the client
2021-02-17 00:34:20 +02:00
const {
Client,
Intents
} = require('discord.js');
2021-02-16 16:48:04 +02:00
/**
* The bot client
* @extends {Client}
*/
class Bot extends Client {
constructor() {
super({
2021-02-17 00:34:20 +02:00
partials: [
'MESSAGE',
'CHANNEL',
'REACTION'
],
presence: selectPresence(),
2021-02-17 00:34:20 +02:00
ws: {
intents: Intents.NON_PRIVILEGED,
}
});
(async () => {
/** The global bot configuration */
this.config = config;
/** A sequelize instance */
this.db = await require('./database')(log), // this.db.models.Ticket...
/** A leekslazylogger instance */
this.log = log;
/** An @eartharoid/i18n instance */
this.i18n = new I18n(path('./src/locales'), 'en-GB');
this.setMaxListeners(this.config.max_listeners); // set the max listeners for each event
require('./updater')(this); // check for updates
require('./modules/listeners')(this); // load internal listeners
2021-02-17 15:24:33 +02:00
2021-02-25 11:58:19 +02:00
/** The ticket manager */
this.tickets = new TicketManager(this);
/** The command manager, used by internal and plugin commands */
this.commands = new CommandManager(this);
2021-02-17 22:17:35 +02:00
/** The plugin manager */
this.plugins = new PluginManager(this);
this.plugins.load(); // load plugins
this.log.info('Connecting to Discord API...');
2021-02-17 01:35:31 +02:00
this.login();
})();
}
2021-02-17 00:34:20 +02:00
2021-03-04 12:18:48 +02:00
async postStats() {
2021-03-03 23:19:51 +02:00
/**
* OH NO, TELEMETRY!?
2021-03-15 23:12:58 +02:00
* Relax, it just counts how many people are using Discord Tickets.
2021-03-04 12:18:48 +02:00
* You can see the source here: https://github.com/discord-tickets/stats
2021-03-03 23:19:51 +02:00
*/
if (this.config.super_secret_setting) { // you can disable it if you really want
const fetch = require('node-fetch');
2021-03-04 12:18:48 +02:00
let tickets = await this.db.models.Ticket.count();
2021-03-04 17:39:22 +02:00
await fetch(`https://stats.discordtickets.app/client?id=${this.user.id}&tickets=${tickets}`, {
2021-03-03 23:19:51 +02:00
method: 'post',
}).catch(e => {
// fail quietly, it doesn't really matter if it didn't work
this.log.debug(e);
});
2021-03-04 12:18:48 +02:00
this.guilds.cache.forEach(async g => {
let members = (await g.fetch()).approximateMemberCount;
2021-03-04 17:39:22 +02:00
await fetch(`https://stats.discordtickets.app/guild?id=${g.id}&members=${members}`, {
2021-03-04 12:18:48 +02:00
method: 'post',
}).catch(e => {
this.log.debug(e);
});
});
2021-03-03 23:19:51 +02:00
}
}
}
new Bot();
const { version } = require('../package.json');
process.on('unhandledRejection', error => {
log.notice('PLEASE INCLUDE THIS INFORMATION IF YOU ASK FOR HELP ABOUT THE FOLLOWING ERROR:');
log.notice(`Discord Tickets v${version}, Node v${process.versions.node} on ${process.platform}`);
log.warn('An error was not caught');
2021-02-25 11:58:19 +02:00
if (error instanceof Error) log.warn(`Uncaught ${error.name}`);
log.error(error);
});