ModerationRule.fromPayload constructor

ModerationRule.fromPayload(
  1. dynamic payload
)

Implementation

factory ModerationRule.fromPayload (dynamic payload) {
  Guild guild = ioc.use<MineralClient>().guilds.cache.getOrFail(payload['guild_id']);

  List<ModerationAction> actions = [];
  if (payload['actions'] != null) {
    for (dynamic item in payload['actions']) {
      ModerationAction action = ModerationAction(
        ModerationActionType.values.firstWhere((element) => element.value == item['type']),
        item['metadata'].toString() != '{}'
          ? ModerationActionMetadata(
            item['metadata']['channel_id'],
            item['metadata']['duration_seconds']
          )
          : null
      );

      actions.add(action);
    }
  }

  List<String> keywordFilter = [];
  if (payload['trigger_metadata']?['keyword_filter'] != null) {
    for (dynamic keyword in payload['trigger_metadata']['keyword_filter']) {
      keywordFilter.add(keyword);
    }
  }

  List<Role> roles = [];
  if (payload['exempt_roles'] != null) {
    for (Snowflake id in payload['exempt_roles']) {
      Role? role = guild.roles.cache.get(id);
      if (role != null) {
        roles.add(role);
      }
    }
  }

  List<GuildChannel> channels = [];
  if (payload['exempt_channels'] != null) {
    for (Snowflake id in payload['exempt_channels']) {
      GuildChannel? channel = guild.channels.cache.get(id);
      if (channel != null) {
        channels.add(channel);
      }
    }
  }

  return ModerationRule(
    id: payload['id'],
    guildId: payload['guild_id'],
    guild: guild,
    label: payload['name'],
    eventType: ModerationEventType.values.firstWhere((element) => element.value == payload['event_type']),
    actions: actions,
    triggerType: ModerationTriggerType.values.firstWhere((element) => element.value == payload['trigger_type']),
    triggerMetadata: ModerationTriggerMetadata(
      keywordFilter,
      payload['trigger_metadata'] != null && payload['trigger_metadata']['presets'] != null
        ? (payload['trigger_metadata']['presets'] as List<dynamic>).map((preset) {
            return ModerationPresetType.values.firstWhere((element) => element.value == preset);
          }).toList()
        : [],
      payload['allow_list'],
      payload['mention_total_limit']
    ),
    enabled: payload['enabled'] ?? false,
    exemptRoles: roles,
    exemptChannels: channels,
  );
}