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 = [];
  if (payload['embeds'] != null) {
    for (dynamic element in payload['embeds']) {
       embeds.add(EmbedBuilder.from(element));
    }
  }

  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);
    }
  }

  ComponentBuilder componentBuilder = ComponentBuilder();
  if (payload['components'] != null) {
    for (dynamic element in payload['components']) {
      componentBuilder.rows.add(ComponentWrapper.wrap(element, payload['guild_id']));
    }
  }

  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'],
    componentBuilder,
    stickers,
    payload['payload'],
    messageAttachments,
    payload['flags'],
    payload['pinned'] ?? false,
    channel.guild.id,
    payload['channel_id'],
    MessageReactionManager<GuildChannel, Message>(channel),
    payload['timestamp'] ?? DateTime.now().toIso8601String(),
    payload['edited_timestamp'],
    MessageMention(channel, channelMentions, memberMentions, roleMentions, payload['mention_everyone'] ?? false),
    MessageAuthor(channel.guild.id, User.from(payload['author']))
  );

  message.reactions.message = message;

  return message;
}