Skip to content

Commit

Permalink
fix(models): fix no config found errors by creating config on the spot
Browse files Browse the repository at this point in the history
  • Loading branch information
dsevillamartin committed Oct 14, 2017
1 parent bfefc36 commit 0098d23
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 72 deletions.
27 changes: 18 additions & 9 deletions lib/Discord/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ class Client extends DiscordClient {
this.emit('error', `Module | ${f}`, error);
}
});

this.middleware = this.middleware.sort((a, b) => b.priority - a.priority);

return this;
});
}
Expand Down Expand Up @@ -168,30 +171,36 @@ class Client extends DiscordClient {
});
}

run(msg) {
this.execute(msg)
.catch(e => this.middleware.last().run(msg, null, null, null, e))
.catch(Log.error);
}

/**
* Message event handling, uses modules (aka middleware)
* @param {Message} msg
* @return {Client}
*/
run(msg) {
async execute(msg) {
if (msg.author.equals(this.user) || msg.author.bot) return;
let serverConf = msg.guild ? ServerConfig.get(msg.guild.id) : {};
let prefix = serverConf.prefix || Math.random();
const serverConf = msg.guild.available ? ServerConfig.get(msg.guild.id) : await ServerConfig.add(msg.guild);
const prefix = serverConf.prefix || Math.random();
if (msg.channel.type !== 'dm' && !msg.content.startsWith(this.user.toString()) && !msg.content.startsWith(prefix) && !msg.content.startsWith(this.prefix)) return false;

let content = (msg.content.startsWith(prefix) && msg.content.replace(prefix, '')) || (msg.content.startsWith(this.user.toString()) && msg.content.replace(`${this.user.toString()} `, '')) || (msg.content.startsWith(this.prefix) && msg.content.replace(this.prefix, '')) || msg.content;
let command = content.split(' ')[0].toLowerCase();
let args = content.split(' ').slice(1);
const content = (msg.content.startsWith(prefix) && msg.content.replace(prefix, '')) || (msg.content.startsWith(this.user.toString()) && msg.content.replace(`${this.user.toString()} `, '')) || (msg.content.startsWith(this.prefix) && msg.content.replace(this.prefix, '')) || msg.content;
const command = content.split(' ')[0].toLowerCase();
const args = content.split(' ').slice(1);

let middleware = this.middleware.array().sort((a, b) => b.priority - a.priority);
const middleware = this.middleware.array();
let i = 0;

const handleErr = (err, currentMiddleware) =>
middleware[middleware.length - 1].run(msg, args, next, currentMiddleware, err);

const next = (err) => {
let currentMiddleware = middleware[i] || middleware[i - 1];
let nextMiddleware = middleware[i++];
const currentMiddleware = middleware[i] || middleware[i - 1];
const nextMiddleware = middleware[i++];
if (err) return handleErr(err, currentMiddleware);
if (nextMiddleware) {
try {
Expand Down
8 changes: 5 additions & 3 deletions lib/Discord/Commands/Conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ class ConfCommand extends Command {
guildOnly: true,
});
}
run(msg, args) {

async run(msg, args) {
const action = args.filter(e => !e.includes('-'))[0] || 'view';
const serverConf = ServerConfig.get(msg.guild.id);
const channelConf = ChannelConfig.FindByChannel(msg.channel.id);
const channelConf = ChannelConfig.get(msg.channel.id) || await ChannelConfig.add(msg.channel);

if (action === 'view') return this._view(msg, args, channelConf, serverConf);
if (action === 'get') return this._get(msg, args, channelConf, serverConf);
Expand Down Expand Up @@ -61,7 +62,7 @@ class ConfCommand extends Command {
const key = args.filter(e => !e.includes('-'))[1];
const conf = (args.includes('--global') || args.includes('-g')) ? serverConf : channelConf;

let embed = new this.embed()
const embed = new this.embed()
.setColor('#FB9738')
.setAuthor(conf === channelConf ? `${guild.name} #${channel.name}` : guild.name, guild.iconURL)
.setDescription(`This is your current ${conf === channelConf ? 'channel' : 'server'}'s configuration.`)
Expand All @@ -82,6 +83,7 @@ class ConfCommand extends Command {
author: conf === channelConf ? `${guild.name} #${channel.name}` : guild.name,
confName: conf === channelConf ? 'channel' : 'server',
};

if (!validKeys.includes(key)) {
const embed = new this.embed()
.setColor('#CE0814')
Expand Down
12 changes: 6 additions & 6 deletions lib/Discord/Commands/GitlabInit.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,29 @@ class GitlabInitCommand extends Command {
},
});

const conf = ChannelConfig.get(channelid) || await ChannelConfig.add(msg.channel);

if (!repository.isGitlab || isPrivate) {
// GitlabCache.add(repository.repo);
const conf = ChannelConfig.FindByChannel(channelid);
// GitlabCache.add(repository.repo);;
const exists = conf && conf.repos && conf.repos.includes(repoFullName);
if (exists) return this.commandError(msg, `Repository \`${repository.repo}\` is already initialized in this channel`);
return ChannelConfig.AddRepoToChannel(channelid, repoFullName)
return ChannelConfig.addRepoToChannel(channelid, repoFullName)
.then(() => {
let embed = this._successMessage(repository.repo);
return workingMsg.edit({ embed });
});
}

return Gitlab.getRepo(repository.repo).then(() => {
const conf = ChannelConfig.FindByChannel(channelid);
const exists = conf && conf.repos && conf.repos.includes(repoFullName);
if (exists) return this.commandError(msg, `Repository \`${repository.repo}\` is already initialized in this channel`);
return ChannelConfig.AddRepoToChannel(channelid, repoFullName)
return ChannelConfig.addRepoToChannel(channelid, repoFullName)
.then(() => {
let embed = this._successMessage(repository.repo);
return workingMsg.edit({ embed });
});
}).catch(err => {
let errorMessage = err && err.name ? err.name : err || null;
const errorMessage = err && err.name ? err.name : err || null;
if (errorMessage && errorMessage !== 'Gitlab404Error') return this.commandError(msg, `Unable to get repository info for \`${repo}\`\n${err}`);
return this.commandError(msg, `Unable to initialize! The repository \`${repository.repo}\` could not be found!\nIf it's private, please add \`private\` after the command as a separate argument`);
});
Expand Down
8 changes: 4 additions & 4 deletions lib/Discord/Commands/GitlabInitOrg.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ class GitlabInitOrgCommand extends Command {
},
});

return Gitlab.getGroupProjects(orgName).then(data => {
const conf = ChannelConfig.FindByChannel(channelid);
return Gitlab.getGroupProjects(orgName).then(async data => {
const conf = ChannelConfig.get(channelid) || await ChannelConfig.add(msg.channel);
const repos = data.body.filter(e => !e.private && !conf.repos.includes(e.name_with_namespace.toLowerCase())).map(e => e.name_with_namespace);

return Promise.all(repos.map(repo => ChannelConfig.AddRepoToChannel(channelid, repo))).then(() => repos);
return Promise.all(repos.map(repo => ChannelConfig.addRepoToChannel(channelid, repo))).then(() => repos);
})
.then(repos => {
const embed = this._successMessage(orgName, repos);
return workingMsg.edit({ embed });
})
.catch(err => {
let errorMessage = err && err.message ? err.message : err || null;
const errorMessage = err && err.message ? err.message : err || null;

Log.error(err);

Expand Down
4 changes: 2 additions & 2 deletions lib/Discord/Commands/GitlabIssue.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class GitlabIssue extends Command {

_issue(msg, args) {
const issueNumber = parseInt(args[0].replace(/#/g, ''));
let repository = ChannelConfig.FindByChannel(msg.channel.id).repo;
let repository = ChannelConfig.get(msg.channel.id).repo;
if (!repository) return this.commandError(msg, Gitlab.Constants.Errors.NO_REPO_CONFIGURED(this));
if (!issueNumber) return this.errorUsage(msg);

Expand Down Expand Up @@ -75,7 +75,7 @@ class GitlabIssue extends Command {

if (!query) return false;

let repository = ChannelConfig.FindByChannel(channelId).repo;
let repository = ChannelConfig.get(channelId).repo;

if (!repository) return this.commandError(msg, Gitlab.Constants.Errors.NO_REPO_CONFIGURED(this));

Expand Down
4 changes: 2 additions & 2 deletions lib/Discord/Commands/GitlabMergeRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class GitlabIssue extends Command {

_mr(msg, args) {
const mrNumber = parseInt(args[0].replace(/!/g, ''));
let repository = ChannelConfig.FindByChannel(msg.channel.id).repo;
let repository = ChannelConfig.get(msg.channel.id).repo;
if (!repository) return this.commandError(msg, Gitlab.Constants.Errors.NO_REPO_CONFIGURED(this));
if (!mrNumber) return this.errorUsage(msg);

Expand Down Expand Up @@ -75,7 +75,7 @@ class GitlabIssue extends Command {
_list(msg, args) {
let channelId = msg.channel.id;
let page = args[1] ? parseInt(args) : 1;
let repository = ChannelConfig.FindByChannel(channelId).repo;
let repository = ChannelConfig.get(channelId).repo;
if (!repository) return this.commandError(msg, Gitlab.Constants.Errors.NO_REPO_CONFIGURED(this));

return Gitlab.getProjectMergeRequests(repository, null, {
Expand Down
2 changes: 1 addition & 1 deletion lib/Discord/Commands/GitlabRemove.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class GitlabRemoveCommand extends Command {

async run(msg, args) {
const channelid = msg.channel.id;
const conf = ChannelConfig.FindByChannel(channelid);
const conf = ChannelConfig.get(channelid) || await ChannelConfig.add(msg.channel);
const repo = args[0] ? parse(args[0].toLowerCase()) : {};
let repoFound = repo && !!repo.repo;

Expand Down
Loading

0 comments on commit 0098d23

Please sign in to comment.