From e3a36b1902652866e553145b859073140f40c0a8 Mon Sep 17 00:00:00 2001 From: David Sevilla Martin Date: Mon, 15 May 2017 18:21:37 -0400 Subject: [PATCH] fix(models): hopefully fix duplicating config items on start and hopefully fixes double posting :/ See #7 --- lib/Models/ChannelConfig.js | 5 +++-- lib/Models/ServerConfig.js | 5 +++-- lib/Web.js | 11 +++++++++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/Models/ChannelConfig.js b/lib/Models/ChannelConfig.js index 6f36d63..79d2f26 100644 --- a/lib/Models/ChannelConfig.js +++ b/lib/Models/ChannelConfig.js @@ -99,13 +99,14 @@ class ChannelConfig { configs.forEach(row => { this._data.set(row.channelID, new ChannelConfigItem(this, row._doc)); }); - }); + }).catch(Log.error); } /** * Initialize configuration and Discord bot events * @param {external:Client} bot */ init(bot) { + if (this._data.size < 10) return setTimeout(() => this.init(bot), 5000); for (const ch of bot.channels) { const channel = ch[1]; if (!channel || channel.type !== 'text') continue; @@ -119,7 +120,7 @@ class ChannelConfig { bot.on('channelDelete', channel => { if (!channel || channel.type !== 'text') return; Log.info(`ChannelConf | Deleting "${channel.guild.name}"'s #${channel.name} (${channel.id})`); - this.DeleteChannel(channel.id); + this.DeleteChannel(channel.id).catch(Log.error); }); bot.on('channelCreate', channel => { if (!channel || channel.type !== 'text') return; diff --git a/lib/Models/ServerConfig.js b/lib/Models/ServerConfig.js index d4fa64f..0eac91a 100644 --- a/lib/Models/ServerConfig.js +++ b/lib/Models/ServerConfig.js @@ -87,6 +87,7 @@ class ServerConfig { * @param {external:Client} bot */ init(bot) { + if (this._data.size < 2) return setTimeout(() => this.init(bot), 5000); for (const g of bot.guilds) { const guild = g[1]; if (!guild) continue; @@ -98,12 +99,12 @@ class ServerConfig { if (this.setupEvents) return; this.setupEvents = true; bot.on('guildDelete', (guild) => { - if (!guild) return; + if (!guild || !guild.available) return; Log.info(`ServerConf | Deleting "${guild.name}"`); this.delete(guild.id).catch(e => bot.emit('error', e)); }); bot.on('guildCreate', (guild) => { - if (!guild) return; + if (!guild || !guild.available) return; let g = this.get(guild.id); if (g) return; Log.info(`ServerConf | Adding "${guild.name}"`); diff --git a/lib/Web.js b/lib/Web.js index 5be28ff..a24e74e 100644 --- a/lib/Web.js +++ b/lib/Web.js @@ -33,7 +33,7 @@ app.post('/', (req, res) => { const repo = data.project.path_with_namespace; const channels = ChannelConfig.FindByRepo(repo); const actionText = data.object_attributes && data.object_attributes.action ? `/${data.object_attributes.action}` : ''; - Log.verbose(`GitLab | ${repo} - ${eventName}${actionText}`); + Log.verbose(`GitLab | ${repo} - ${eventName}${actionText} (${channels.size} channels)`); res.send(`${repo} : Received ${eventName}${actionText}, emitting to ${channels.size} channels...`); const eventResponse = GitlabEventHandler.use(data, event); @@ -42,8 +42,15 @@ app.post('/', (req, res) => { const errors = ['Forbidden', 'Missing Access']; if (!res || !err) return; if (errors.includes(err.message) || (err.error && errors.includes(err.error.message))) { - channel.guild.owner.send(`**ERROR: ** Yappy GitLab doesn't have permissions to read/send messages in ${channel}`); + channel.guild.owner.send(`**ERROR:** Yappy GitLab doesn't have permissions to read/send messages in ${channel}`); } else { + channel.guild.owner.send([ + `**ERROR:** An error occurred when trying to read/send messages in ${channel}.`, + 'Please report this to the bot\'s developer\n', + '```js\n', + err, + '\n```', + ].join(' ')); Log.error(err); } };