This commit is contained in:
Isaac
2021-02-16 22:34:20 +00:00
parent cf8dff2a5b
commit 5fdfb121c7
17 changed files with 257 additions and 32 deletions

28
src/banner.js Normal file
View File

@@ -0,0 +1,28 @@
const link = require('terminal-link');
const leeks = require('leeks.js');
const { version, homepage } = require('../package.json');
module.exports = () => {
console.log(leeks.colours.cyan(`
######## #### ###### ###### ####### ######## ########
## ## ## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ##
## ## ## ###### ## ## ## ######## ## ##
## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ## ## ##
######## #### ###### ###### ####### ## ## ########
######## #### ###### ## ## ######## ######## ######
## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ##
## ## ## ##### ###### ## ######
## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ##
## #### ###### ## ## ######## ## ######
`));
console.log(leeks.colours.cyanBright(`DiscordTickets bot v${version} by eartharoid`));
console.log(leeks.colours.cyanBright(homepage + '\n'));
console.log(leeks.colours.cyanBright(`Please ${link('donate', 'https://ko-fi.com/eartharoid')} if you find this bot useful`));
console.log('\n\n');
};

View File

@@ -19,7 +19,7 @@ module.exports = {
dialect: 'mariadb',
package: ['mariadb']
},
postgre: {
postgre: { // this is wrong
name: 'PostgreSQL',
dialect: 'postgres',
packages: ['pg', 'pg-hstore']

View File

@@ -14,22 +14,23 @@ module.exports = async (log) => {
DB_HOST,
DB_USER,
DB_PASS,
DB_NAME
DB_NAME,
DB_TABLE_PREFIX
} = process.env;
let type = (DB_TYPE || 'sqlite').toLowerCase();
if (!supported.includes(type)) {
log.report('Invalid database type');
throw new Error(`DB_TYPE (${type}) is not a valid type`);
log.error(new Error(`DB_TYPE (${type}) is not a valid type`));
return process.exit();
}
try {
types[type].packages.forEach(pkg => require(pkg));
} catch {
log.report('Specified database type is not installed');
let required = types[type].packages.map(i => `"${i}"`).join(' and ');
throw new Error(`Please install the package(s) for your selected database type: ${required}`);
log.error(new Error(`Please install the package(s) for your selected database type: ${required}`));
return process.exit();
}
let sequelize;
@@ -56,6 +57,7 @@ module.exports = async (log) => {
} catch (error) {
log.warn('Unable to connect to database');
log.error(error);
return process.exit();
}
/* let models = {};
@@ -70,25 +72,23 @@ module.exports = async (log) => {
const Guild = sequelize.define('Guild', {
id: {
type: DataTypes.CHAR(18),
primaryKey: true
},
prefix: {
type: DataTypes.STRING, // STRING(255) = VARCHAR(255)
defaultValue: config.defaults.prefix
primaryKey: true,
allowNull: false,
},
locale: {
type: DataTypes.STRING,
defaultValue: config.defaults.locale
}
}, {
tableName: 'guilds'
tableName: DB_TABLE_PREFIX + 'guilds'
});
const Ticket = sequelize.define('Ticket', {
id: {
type: DataTypes.CHAR(18),
primaryKey: true
primaryKey: true,
allowNull: false,
},
guild: {
type: DataTypes.STRING,
@@ -99,14 +99,15 @@ module.exports = async (log) => {
},
}
}, {
tableName: 'tickets'
tableName: DB_TABLE_PREFIX + 'tickets'
});
// eslint-disable-next-line no-unused-vars
const Message = sequelize.define('Message', {
id: {
type: DataTypes.CHAR(18),
primaryKey: true
primaryKey: true,
allowNull: false,
},
ticket: {
type: DataTypes.STRING,
@@ -117,7 +118,7 @@ module.exports = async (log) => {
},
}
}, {
tableName: 'messages'
tableName: DB_TABLE_PREFIX + 'messages'
});
sequelize.sync();

View File

@@ -36,12 +36,28 @@ require('dotenv').config({
const config = require('../user/config');
require('./banner')();
const Logger = require('leekslazylogger');
const log = new Logger({
name: 'DiscordTickets by eartharoid',
debug: config.debug,
logToFile: config.logs.enabled,
keepFor: config.logs.keep_for
keepFor: config.logs.keep_for,
custom: {
listeners: {
title: 'info',
prefix: 'listeners'
},
commands: {
title: 'info',
prefix: 'commands'
},
plugins: {
title: 'info',
prefix: 'plugins'
}
}
});
@@ -60,12 +76,24 @@ log.report = error => {
const terminalLink = require('terminal-link');
const I18n = require('@eartharoid/i18n');
const { CommandManager } = require('./modules/commands');
const {
Client,
Intents
} = require('discord.js');
const { Client } = require('discord.js');
class Bot extends Client {
constructor() {
super({
autoReconnect: true,
partials: [
'MESSAGE',
'CHANNEL',
'REACTION'
],
ws: {
intents: Intents.NON_PRIVILEGED,
}
});
Object.assign(this, {
@@ -75,10 +103,17 @@ class Bot extends Client {
i18n: new I18n(path('./src/locales'), 'en-GB')
});
this.log.info('Connecting to Discord API');
(async () => {
this.listeners = require('./modules/listeners')(this);
this.commands = new CommandManager(this);
this.plugins = await require('./modules/plugins')(this);
this.login();
this.log.info('Connecting to Discord API');
this.login();
})();
}
}
new Bot();

View File

@@ -0,0 +1,8 @@
module.exports = {
event: 'messageReactionAdd',
execute: (client, r, u) => {
client.log.info('messageReactionAdd');
console.log(r);
console.log(u);
}
};

6
src/listeners/ready.js Normal file
View File

@@ -0,0 +1,6 @@
module.exports = {
event: 'ready',
execute: client => {
client.log.success(`Connected to Discord as ${client.user.tag}`);
}
};

41
src/modules/commands.js Normal file
View File

@@ -0,0 +1,41 @@
const fs = require('fs');
const { join } = require('path');
const { path } = require('../utils/fs');
class CommandManager {
constructor(client) {
this.client = client;
this.commands = new Map();
}
load(command) {
}
unload(command) {
}
reload(command) {
}
get(command) {
}
get list() {
return this.commands;
}
}
class Command {
constructor(client) {
}
}
module.exports = {
CommandManager,
Command
};

13
src/modules/listeners.js Normal file
View File

@@ -0,0 +1,13 @@
const fs = require('fs');
const { path } = require('../utils/fs');
module.exports = client => {
const files = fs.readdirSync(path('./src/listeners'))
.filter(file => file.endsWith('.js'));
for (const file of files) {
const listener = require(`../listeners/${file}`);
// client.on(listener.event, ...args => listener.execute(client, ...args));
client.on(listener.event, args => listener.execute(client, args));
}
};

35
src/modules/plugins.js Normal file
View File

@@ -0,0 +1,35 @@
const yarpm = require('yarpm');
const fs = require('fs');
const { join } = require('path');
const { path } = require('../utils/fs');
module.exports = async client => {
const plugins = {};
const dirs = fs.readdirSync(path('./user/plugins'));
for (const dir of dirs) {
let package_path = path(`./user/plugins/${dir}/package.json`);
if (!fs.existsSync(package_path)) continue;
let package = require(`../../user/plugins/${dir}/package.json`);
let main = require(join(`../../user/plugins/${dir}/`, package.main));
plugins[package.name] = package;
client.log.plugins(`Loading ${package.name} v${package.version} by ${package.author}`);
if (package.dependencies && Object.keys(package.dependencies).length >= 1) {
client.log.plugins(`Installing dependencies for ${package.name}`);
let deps = Object.keys(package.dependencies)
.map(d => `${d}@${package.dependencies[d]}`)
.join(' ');
await yarpm(['install', '--no-save', deps], {
stdout: process.stdout
});
await main(client);
} else {
await main(client);
}
}
};

17
src/structures/guild.js Normal file
View File

@@ -0,0 +1,17 @@
const { Structures } = require('discord.js');
Structures.extend('Guild', Guild => {
return class extends Guild {
constructor(client, data) {
super(client, data);
}
get settings() {
return this.client.db.Guild.findOne({
where: {
id: this.id
}
});
}
};
});

View File

@@ -0,0 +1,25 @@
const { Structures } = require('discord.js');
Structures.extend('TextChannel', TextChannel => {
return class extends TextChannel {
constructor(client, data) {
super(client, data);
}
get isTicket() {
return !!this.client.db.Ticket.findOne({
where: {
id: this.id
}
});
}
get ticket() {
return new class {
constructor(channel) {
this.channel = channel;
}
}(this);
}
};
});