Skip to content

Commit

Permalink
Switch to SQLite, switch to prettier, add whitelist/blacklist system …
Browse files Browse the repository at this point in the history
…to conf
  • Loading branch information
dsevillamartin committed Nov 28, 2019
1 parent 97cfd1e commit 41884cb
Show file tree
Hide file tree
Showing 42 changed files with 4,251 additions and 3,284 deletions.
2 changes: 0 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ DISCORD_CLIENT_ID=
DISCORD_CLIENT_SECRET=
DISCORD_CHANNEL_LOGGING=

DB_URL=

BDPW_KEY=

LOG_LEVEL=info
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.DS_Store
node_modules
.env
.vscode
db/*.sqlite
137 changes: 137 additions & 0 deletions db/migrate-from-mongo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
const mongoose = require('mongoose');
const { default: PQueue } = require('p-queue');
const ProgressBar = require('progress');
const Guild = require('../lib/Models/Guild');
const Channel = require('../lib/Models/Channel');

require('dotenv').config();

const uniqueElementsBy = (arr, fn) =>
arr.reduce((acc, v) => {
if (!acc.some(x => fn(v, x))) acc.push(v);
return acc;
}, []);
const uniqueBy = prop => (a, b) => a[prop] == b[prop];
const progressBarFormat = '[:bar] :rate/s :percent :elapseds (estimated :etas)';

const channelConfig = mongoose.model('ChannelConfig', {
guildName: String,
guildID: String,
channelName: String,
channelID: String,
repos: Array,
repo: String,
embed: Boolean,
disabledEvents: {
type: Array,
default: ['merge_request/update'],
},
ignoredUsers: Array,
ignoredBranches: Array,
});

const serverConfig = mongoose.model('ServerConfig', {
guildName: String,
guildID: String,
prefix: String,
});

process.on('unhandledRejection', console.error);

(async () => {
console.log('DB |> Connecting');

await mongoose.connect(process.env.DB_URL, {
useNewUrlParser: true,
});

console.log('DB |> Connected');

// === GUILDS ===

console.log('DB |> Guilds |> Retrieving');

const guilds = await Guild.fetchAll();
const allGuilds = await serverConfig.find({});

console.log('DB |> Guilds |> Filtering');

const guildsToMigrate = uniqueElementsBy(
allGuilds.filter(g => g && g.guildID && !guilds.get(g.guildID)),
uniqueBy('guildID')
);

console.log(`DB |> Guilds |> Migrating (${guildsToMigrate.length})`);

let progress;

if (guildsToMigrate.length) {
progress = new ProgressBar(`DB |> Guilds |> Migrating ${progressBarFormat}`, { total: guildsToMigrate.length, width: 20 });
}

for (const guild of guildsToMigrate) {
await Guild.forge({
id: guild.guildID,
name: guild.guildName,
prefix: guild.prefix,
}).save(null, {
method: 'insert',
});

progress.tick();
}

// === CHANNELS ===

console.log('DB |> Channels |> Retrieving');

const channels = await Channel.fetchAll();
const allChannels = await channelConfig.find({});
const channelsAdded = [];

console.log('DB |> Channels |> Filtering');

const channelsToMigrate = allChannels.filter(ch => ch && ch.channelID && !channels.get(ch.channelID) && ch.guildID);

console.log(`DB |> Channels |> Migrating (${channelsToMigrate.length})`);

if (channelsToMigrate.length) {
progress = new ProgressBar(`DB |> Channels |> Migrating ${progressBarFormat}`, { total: channelsToMigrate.length, width: 20 });
}

for (const ch of channelsToMigrate) {
if (channelsAdded.includes(ch.channelID)) continue;

const channel = await Channel.forge({
id: ch.channelID,
name: ch.channelName,
guild_id: ch.guildID,
repo: ch.repo,
use_embed: !!ch.embed,
events_list: JSON.stringify(ch.disabledEvents || []) || [],
users_list: JSON.stringify(ch.ignoredUsers || []) || [],
branches_list: JSON.stringify(ch.ignoredBranches || []) || [],
}).save(null, {
method: 'insert',
});

if (Array.isArray(ch.repos))
await Promise.all(
ch.repos.map(repo =>
channel.related('repos').create({
name: repo,
})
)
);

channelsAdded.push(ch.channelID);
progress.tick();
}

console.log();
console.log(`DB |> Channels |> Migrated (${channelsAdded.length})`);

if (channelsToMigrate.length) process.stdout.write('\n');

process.exit(0);
})().then(() => process.exit());
41 changes: 41 additions & 0 deletions db/migrations/2019_11_25_000000_create_tables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
exports.up = knex => {
return knex.schema
.createTable('guilds', t => {
t.string('id').primary();

t.string('name').nullable();

t.string('prefix').nullable();
})

.createTable('channels', t => {
t.string('id').primary();

t.string('name').nullable();
t.string('guild_id').nullable();

t.string('repo').nullable();

t.boolean('use_embed').defaultTo(true);

t.enum('events_type', ['whitelist', 'blacklist']).defaultTo('blacklist');
t.json('events_list').defaultTo(['merge_request/update']);

t.enum('users_type', ['whitelist', 'blacklist']).defaultTo('blacklist');
t.json('users_list').defaultTo([]);

t.enum('branches_type', ['whitelist', 'blacklist']).defaultTo('blacklist');
t.json('branches_list').defaultTo([]);

t.enum('repos_type', ['whitelist', 'blacklist']).defaultTo('blacklist');
t.json('repos_list').defaultTo([]);

t.foreign('guild_id')
.references('guilds.id')
.onDelete('cascade');
});
};

exports.down = knex => {
return knex.schema.dropTable('channels').dropTable('guilds');
};
13 changes: 13 additions & 0 deletions db/migrations/2019_11_25_000001_create_channel_repos_table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports.up = knex =>
knex.schema.createTable('channel_repos', t => {
t.increments('id');

t.string('channel_id');
t.string('name').index();

t.foreign('channel_id')
.references('channels.id')
.onDelete('cascade');
});

exports.down = knex => knex.schema.dropTable('channel_repos');
13 changes: 13 additions & 0 deletions db/migrations/2019_11_25_000002_create_channel_orgs_table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports.up = knex =>
knex.schema.createTable('channel_orgs', t => {
t.increments('id');

t.string('channel_id');
t.string('name').index();

t.foreign('channel_id')
.references('channels.id')
.onDelete('cascade');
});

exports.down = knex => knex.schema.dropTable('channel_orgs');
17 changes: 17 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require('dotenv').config();

module.exports = {
client: 'sqlite3',
connection: {
filename: "./db/db.sqlite"
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'migrations',
directory: './db/migrations'
},
useNullAsDefault: true,
};
14 changes: 7 additions & 7 deletions lib/Discord/Client.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Discord = require('discord.js');
const DiscordClient = Discord.Client;
const ServerConfig = require('../Models/ServerConfig');
const fs = require('fs');
const Guild = require('../Models/Guild');
const Log = require('../Util/Log');

/**
Expand Down Expand Up @@ -84,7 +84,7 @@ class Client extends DiscordClient {

if (disabled.includes(Command.help.name)) return Log.info(`Skipping command: ${Command.help.name}`);

Log.debug(`Command | Loading ${Command.help.name}. 👌`);
Log.debug(`Command | Loaded ${Command.help.name}`);
Command.props.help.file = f;

this.commands.set(Command.help.name, Command);
Expand Down Expand Up @@ -113,7 +113,7 @@ class Client extends DiscordClient {
}

if (!files.length) {
Log.info(`Module | No Modules Loaded.`);
Log.info(`Module | No Modules Loaded`);
return this;
}

Expand All @@ -122,7 +122,7 @@ class Client extends DiscordClient {
const module = new (require(`./Modules/${f}`))(this);
const name = module.constructor.name.replace('Module', '');

Log.debug(`Module | Loading ${name}. 👌`);
Log.debug(`Module | Loaded ${name}`);

this.middleware.set(name, module);
} catch (error) {
Expand All @@ -145,7 +145,7 @@ class Client extends DiscordClient {
return new Promise((resolve, reject) => {
try {
delete require.cache[require.resolve(`./Commands/${command}`)];
Log.debug(`Command | Reloading ${command} 👌`);
Log.debug(`Command | Reloading ${command}`);
let cmd = require(`./Commands/${command}`);
let Command = new cmd(this);
Command.props.help.file = command;
Expand Down Expand Up @@ -190,8 +190,8 @@ class Client extends DiscordClient {
*/
async execute(msg) {
if (msg.author.equals(this.user) || msg.author.bot) return;
const serverConf = msg.guild && msg.guild.available ? await ServerConfig.forServer(msg.guild) : null;
const prefix = serverConf ? serverConf.prefix : this.prefix;
const serverConf = msg.guild && msg.guild.available && (await Guild.find(msg.guild.id));
const prefix = (serverConf && serverConf.get('prefix')) || this.prefix;
if (
msg.channel.type !== 'dm' &&
!msg.content.startsWith(this.user.toString()) &&
Expand Down
2 changes: 1 addition & 1 deletion lib/Discord/Commands/Announce.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AnnounceCommand extends Command {
if (messagedOwners.includes(owner.id)) return;
messagedOwners.push(owner.id);
let embed = new this.embed()
.setAuthor(msg.author.username, msg.author.avatarURL)
.setAuthor(msg.author.username, msg.author.avatarURL())
.setColor(0xfb5432)
.setTitle(`Announcement to all server owners of servers using Yappy`)
.setDescription([`\u200B`, announcement, `\u200B`].join('\n'))
Expand Down
Loading

0 comments on commit 41884cb

Please sign in to comment.