Message.from constructor

Message.from({
  1. required GuildChannel channel,
  2. required dynamic payload,
})

Implementation

factory Message.from({ required GuildChannel channel, required dynamic payload }) {
  List<EmbedBuilder> embeds = [];
  for (dynamic element in payload['embeds']) {
    List<Field> fields = [];
    if (element['fields'] != null) {
      for (dynamic item in element['fields']) {
        Field field = Field(name: item['name'], value: item['value'], inline: item['inline'] ?? false);
        fields.add(field);
      }
    }

    EmbedBuilder embed = EmbedBuilder(
      title: element['title'],
      description: element['description'],
      url: element['url'],
      timestamp: element['timestamp'] != null ? DateTime.parse(element['timestamp']) : null,
      footer: element['footer'] != null ? Footer(
        text: element['footer']['text'],
        iconUrl: element['footer']['icon_url'],
        proxyIconUrl: element['footer']['proxy_icon_url'],
      ) : null,
      image: element['image'] != null ? Image(
        url: element['image']['url'],
        proxyUrl: element['image']['proxy_url'],
        height: element['image']['height'],
        width: element['image']['width'],
      ) : null,
      author: element['author'] != null ? Author(
        name: element['author']['name'],
        url: element['author']['url'],
        proxyIconUrl: element['author']['proxy_icon_url'],
        iconUrl: element['author']['icon_url'],
      ) : null,
      fields: fields,
    );

    embeds.add(embed);
  }

  List<MessageStickerItem> stickers = [];
  if (payload['sticker_items'] != null) {
    for (dynamic element in payload['sticker_items']) {
      MessageStickerItem sticker = MessageStickerItem.from(element);
      stickers.add(sticker);
    }
  }

  List<MessageAttachment> messageAttachments = [];
  if (payload['attachments'] != null) {
    for (dynamic element in payload['attachments']) {
      MessageAttachment attachment = MessageAttachment.from(element);
      messageAttachments.add(attachment);
    }
  }

  List<Component> components = [];
  for (dynamic payload in payload['components']) {
    final component = Component.from(payload: payload);
    components.add(component);
  }

  List<Snowflake> memberMentions = [];
  if (payload['mentions'] != null) {
    for (final element in payload['mentions']) {
      memberMentions.add(element['id']);
    }
  }

  List<Snowflake> roleMentions = [];
  if (payload['mention_roles'] != null) {
    for (final element in payload['mention_roles']) {
      roleMentions.add(element);
    }
  }

  List<Snowflake> channelMentions = [];
  if (payload['mention_channels'] != null) {
    for (final element in payload['mention_channels']) {
      channelMentions.add(element['id']);
    }
  }

  final message = Message(
    payload['id'],
    payload['content'],
    payload['tts'] ?? false,
    embeds,
    payload['allow_mentions'] ?? false,
    payload['reference'],
    components,
    stickers,
    payload['payload'],
    messageAttachments,
    payload['flags'],
    payload['pinned'],
    payload['guild_id'],
    payload['channel_id'],
    MessageReactionManager<GuildChannel, Message>(channel),
    payload['author']['id'],
    MessageMention(channel, channelMentions, memberMentions, roleMentions, payload['mention_everyone'] ?? false)
  );

  message.reactions.message = message;

  return message;
}