DmMessage.from constructor

DmMessage.from({
  1. required DmChannel channel,
  2. required dynamic payload,
})

Implementation

factory DmMessage.from({ required DmChannel channel, required dynamic payload }) {
  MineralClient client = ioc.use<MineralClient>();
  User? user = client.users.cache.get(payload['author']['id']);

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

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

  final message = DmMessage(
    payload['id'],
    payload['content'],
    payload['tts'] ?? false,
    embeds,
    payload['allow_mentions'] ?? false,
    payload['reference'],
    componentBuilder,
    stickers,
    payload['payload'],
    messageAttachments,
    payload['flags'],
    payload['pinned'],
    null,
    channel.id,
    MessageReactionManager<DmChannel, DmMessage>(channel),
    payload['timestamp'],
    payload['edited_timestamp'],
    user!,
  );

  message.reactions.message = message;

  return message;
}