<commit_before>const Collection = require('discord.js').Collection;
const bot = require('../Discord/index.js');
const fs = require('fs');
const path = require('path');
const Log = require('../Util/Log');

class Events {
  constructor() {
    this.events = {};
    this.eventDir = path.resolve(__dirname, './Events');
    this.eventsList = new Collection();

    this.bot = bot;
    this.setup();
  }

  setGitlab(gitlab) {
    this.gitlab = gitlab;
  }

  setup() {
    fs.readdir(this.eventDir, (err, files) => {
      if (err) throw err;

      files.forEach(file => {
        let eventName = file.replace(`.js`, ``);
        try {
          let event = require(`./Events/${eventName}`);
          this.eventsList.set(eventName, new event(this.gitlab, this.bot));
          Log.info(`GitHub | Loading Event ${eventName.replace(`-`, `/`)} 👌`);
        } catch (e) {
          Log.info(`GitHub | Loading Event ${eventName} ❌`);
          Log.error(e);
        }
      });

      return;
    });
  }

  use(data, eventName) {
    const action = data.object_attributes ? data.object_attributes.action : '';
    const noteableType = data.object_attributes && data.object_attributes.noteable_type ? data.object_attributes.noteable_type.toLowerCase() : '';
    eventName = eventName.replace(` Hook`, '').replace(/ /g, '_').toLowerCase();
    let event = action || noteableType ? `${eventName}-${action || noteableType}` : eventName;
    try {
      event = this.eventsList.get(event) || this.eventsList.get('Unknown');
      let text = event.text(data, eventName, action);
      return {
        embed: this.parseEmbed(event.embed(data, eventName, action), data),
        text: Array.isArray(text) ? text.join('\n') : text,
      };
    } catch (e) {
      Log.error(e);
    }
  }

  parseEmbed(embed, data) {
    switch (embed.color) {
      case 'success':
        embed.color = 0x3CA553;
        break;
      case 'warning':
        embed.color = 0xFB5432;
        break;
      case 'danger':
      case 'error':
        embed.color = 0xCE0814;
        break;
      default:
        if (embed.color) embed.color = typeof embed.color === 'string' ? parseInt(`0x${embed.color.replace(`0x`, ``)}`, 16) : embed.color;
        break;
    }
    embed.author = {
      name: data.user ? data.user.username : data.user_name,
      icon_url: data.user ? data.user.avatar_url : data.user_avatar,
    };
    embed.footer = {
      text: data.project.path_with_namespace,
    };
    embed.url = embed.url || (data.object_attributes && data.object_attributes.url) || data.project.web_url;
    embed.timestamp = new Date();
    return embed;
  }
}

module.exports = new Events();<commit_msg>fix(gitlab: events): fix event handler author icon_url erroring<commit_after>const Collection = require('discord.js').Collection;
const bot = require('../Discord/index.js');
const fs = require('fs');
const path = require('path');
const Log = require('../Util/Log');

class Events {
  constructor() {
    this.events = {};
    this.eventDir = path.resolve(__dirname, './Events');
    this.eventsList = new Collection();

    this.bot = bot;
    this.setup();
  }

  setGitlab(gitlab) {
    this.gitlab = gitlab;
  }

  setup() {
    fs.readdir(this.eventDir, (err, files) => {
      if (err) throw err;

      files.forEach(file => {
        let eventName = file.replace(`.js`, ``);
        try {
          let event = require(`./Events/${eventName}`);
          this.eventsList.set(eventName, new event(this.gitlab, this.bot));
          Log.info(`GitHub | Loading Event ${eventName.replace(`-`, `/`)} 👌`);
        } catch (e) {
          Log.info(`GitHub | Loading Event ${eventName} ❌`);
          Log.error(e);
        }
      });

      return;
    });
  }

  use(data, eventName) {
    const action = data.object_attributes ? data.object_attributes.action : '';
    const noteableType = data.object_attributes && data.object_attributes.noteable_type ? data.object_attributes.noteable_type.toLowerCase() : '';
    eventName = eventName.replace(` Hook`, '').replace(/ /g, '_').toLowerCase();
    let event = action || noteableType ? `${eventName}-${action || noteableType}` : eventName;
    try {
      event = this.eventsList.get(event) || this.eventsList.get('Unknown');
      let text = event.text(data, eventName, action);
      return {
        embed: this.parseEmbed(event.embed(data, eventName, action), data),
        text: Array.isArray(text) ? text.join('\n') : text,
      };
    } catch (e) {
      Log.error(e);
    }
  }

  parseEmbed(embed, data) {
    switch (embed.color) {
      case 'success':
        embed.color = 0x3CA553;
        break;
      case 'warning':
        embed.color = 0xFB5432;
        break;
      case 'danger':
      case 'error':
        embed.color = 0xCE0814;
        break;
      default:
        if (embed.color) embed.color = typeof embed.color === 'string' ? parseInt(`0x${embed.color.replace(`0x`, ``)}`, 16) : embed.color;
        break;
    }
    const avatar = data.user ? data.user.avatar_url : data.user_avatar;

    embed.author = {
      name: data.user ? data.user.username : data.user_name,
      icon_url: avatar && avatar.startsWith('/') ? `https://gitlab.com${avatar}` : avatar,
    };
    embed.footer = {
      text: data.project.path_with_namespace,
    };
    embed.url = embed.url || (data.object_attributes && data.object_attributes.url) || data.project.web_url;
    embed.timestamp = new Date();
    return embed;
  }
}

module.exports = new Events();
{'fraction_non_alphanumeric': 0.1654231119199272, 'fraction_numerical': 0.006915377616014558, 'mean_word_length': 2.634920634920635, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4302127', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1755, 'n_words': 348}