Skip to content

Commit

Permalink
fix(models): hopefully fix duplicating config items on start
Browse files Browse the repository at this point in the history
and hopefully fixes double posting :/

See #7
  • Loading branch information
dsevillamartin committed May 15, 2017
1 parent ee9d4f3 commit e3a36b1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
5 changes: 3 additions & 2 deletions lib/Models/ChannelConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions lib/Models/ServerConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}"`);
Expand Down
11 changes: 9 additions & 2 deletions lib/Web.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}
};
Expand Down

0 comments on commit e3a36b1

Please sign in to comment.