<commit_before>"use strict";

const Constants = require("../Constants");
const Events = Constants.Events;
const Utils = require("../core/Utils");
const BaseCollection = require("./BaseCollection");

const Guild = require("../models/Guild");
const Role = require("../models/Role");

function convertRoles(roles) {
  const map = new Map();
  roles.forEach(role => {
    map.set(role.id, new Role(role));
  });
  return map;
}

function createGuild(guild) {
  return new Guild({
    id: guild.id,
    name: guild.name,
    region: guild.region,
    icon: guild.icon,
    splash: guild.splash,
    features: new Set(guild.features),
    owner_id: guild.owner_id,
    roles: convertRoles(guild.roles),
    afk_channel_id: guild.afk_channel_id,
    afk_timeout: guild.afk_timeout,
    verification_level: guild.verification_level,
    embed_enabled: guild.embed_enabled,
    embed_channel_id: guild.embed_channel_id,
    joined_at: guild.joined_at
  });
}

function handleConnectionOpen(data) {
  this.clear();
  data.guilds.forEach(guild => {
    if (guild.unavailable) return;
    this.set(guild.id, createGuild(guild));
  });
  return true;
}

function handleCreateOrUpdateGuild(guild) {
  if (!guild || guild.unavailable) return true;
  this.mergeOrSet(guild.id, createGuild(guild));
  return true;
}

function handleDeleteGuild(guild) {
  this.delete(guild.id);
  return true;
}

function handleGuildRoleCreateOrUpdate(data) {
  let guild = this.get(data.guild_id);
  if (guild)
    guild.roles.set(data.role.id, new Role(data.role));
  return true;
}

function handleGuildRoleDelete(data) {
  let guild = this.get(data.guild_id);
  if (guild)
    guild.roles.delete(data.role_id);
  return true;
}


class GuildCollection extends BaseCollection {
  constructor(discordie, gateway) {
    super();

    if (typeof gateway !== "function")<commit_msg>Reuse guild.joined_at if none received<commit_after>"use strict";

const Constants = require("../Constants");
const Events = Constants.Events;
const Utils = require("../core/Utils");
const BaseCollection = require("./BaseCollection");

const Guild = require("../models/Guild");
const Role = require("../models/Role");

function convertRoles(roles) {
  const map = new Map();
  roles.forEach(role => {
    map.set(role.id, new Role(role));
  });
  return map;
}

function createGuild(guild, old) {
  return new Guild({
    id: guild.id,
    name: guild.name,
    region: guild.region,
    icon: guild.icon,
    splash: guild.splash,
    features: new Set(guild.features),
    owner_id: guild.owner_id,
    roles: convertRoles(guild.roles),
    afk_channel_id: guild.afk_channel_id,
    afk_timeout: guild.afk_timeout,
    verification_level: guild.verification_level,
    embed_enabled: guild.embed_enabled,
    embed_channel_id: guild.embed_channel_id,
    joined_at: guild.joined_at || old.joined_at
  });
}

function handleConnectionOpen(data) {
  this.clear();
  data.guilds.forEach(guild => {
    if (guild.unavailable) return;
    this.set(guild.id, createGuild(guild, {}));
  });
  return true;
}

function handleCreateOrUpdateGuild(guild) {
  if (!guild || guild.unavailable) return true;
  const old = this.get(guild.id) || {};
  this.mergeOrSet(guild.id, createGuild(guild, old));
  return true;
}

function handleDeleteGuild(guild) {
  this.delete(guild.id);
  return true;
}

function handleGuildRoleCreateOrUpdate(data) {
  let guild = this.get(data.guild_id);
  if (guild)
    guild.roles.set(data.role.id, new Role(data.role));
  return true;
}

function handleGuildRoleDelete(data) {
  let guild = this.get(data.guild_id);
  if (guild)
    guild.roles.delete(data.role_id);
  return true;
}


class GuildCollection extends BaseCollection {
  constructor(discordie, gateway) {
    super();

    if (typeof gateway !== "function")
{'fraction_non_alphanumeric': 0.13852242744063326, 'fraction_numerical': 0.0, 'mean_word_length': 4.150815217391305, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, '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': '5544640', 'n_tokens_mistral': 1351, 'n_tokens_neox': 1314, 'n_words': 261}