Skip to content

Commit

Permalink
Merge pull request #125 from mineral-dart/fix/nullables
Browse files Browse the repository at this point in the history
fix: allow nullables
  • Loading branch information
LeadcodeDev authored Aug 3, 2023
2 parents ee5215b + 2ed58e8 commit 96987ad
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 80 deletions.
41 changes: 40 additions & 1 deletion lib/src/api/builders/embed/embed_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,45 @@ class EmbedBuilder {
};
}

factory EmbedBuilder.from(final payload) {
List<EmbedField> fields = [];
if (payload['fields'] != null) {
for (dynamic item in payload['fields']) {
fields.add(EmbedField(
name: item['name'],
value: item['value'],
inline: item['inline'] ?? false
));
}
}

return EmbedBuilder(
title: payload['title'],
description: payload['description'],
url: payload['url'],
timestamp: payload['timestamp'] != null ? DateTime.parse(payload['timestamp']) : null,
footer: payload['footer'] != null ? EmbedFooter(
text: payload['footer']['text'],
iconUrl: payload['footer']['icon_url'],
proxyIconUrl: payload['footer']['proxy_icon_url'],
) : null,
image: payload['image'] != null ? EmbedImage(
url: payload['image']['url'],
proxyUrl: payload['image']['proxy_url'],
height: payload['image']['height'],
width: payload['image']['width'],
) : null,
author: payload['author'] != null ? EmbedAuthor(
name: payload['author']['name'],
url: payload['author']['url'],
proxyIconUrl: payload['author']['proxy_icon_url'],
iconUrl: payload['author']['icon_url'],
) : null,
fields: fields,
color: Color("#${payload['color'].toRadixString(16)}"),
);
}

/// Create new instance of this to build an embed from a [GuildPreview]
factory EmbedBuilder.fromGuildPreview(GuildPreview preview) {
MineralClient client = ioc.use<MineralClient>();
Expand All @@ -211,7 +250,7 @@ class EmbedBuilder {
embed.addField(name: 'Identifier', value: preview.id);
embed.addField(name: 'Features', value: preview.features.map((feature) => '• $feature').join('\n'), inline: true);

if (preview.stickers.isNotEmpty) {
if (preview.stickers.isNotEmpty || preview.emojis.isNotEmpty) {
embed.addField(name: 'Emojis', value: preview.emojis.values.map((emoji) => emoji).join(' '), inline: true);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/api/guilds/guild_member.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class GuildMember {
/// Returns a clone of this
GuildMember clone () => GuildMember(user, nickname, _avatar, joinedAt, _premiumSince, _permissions, pending, _timeoutDuration, roles, voice, guild, presence);

factory GuildMember.from({ required user, required GuildRoleManager roles, required Guild guild, dynamic member, required VoiceManager voice }) {
factory GuildMember.from({ required user, required GuildRoleManager roles, required Guild guild, required dynamic member, required VoiceManager voice }) {
MemberRoleManager memberRoleManager = MemberRoleManager(manager: roles, memberId: user.id);
for (var element in (member['roles'] as List<dynamic>)) {
Role? role = roles.cache.get(element);
Expand Down
2 changes: 2 additions & 0 deletions lib/src/api/managers/member_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class MemberManager extends CacheManager<GuildMember> {
user: User.from(element['user']),
roles: guild.roles,
guild: guild,
member: element,
voice: voiceManager ?? VoiceManager.empty(element['deaf'], element['mute'], element['user']['id'], _guildId)
);

Expand All @@ -60,6 +61,7 @@ class MemberManager extends CacheManager<GuildMember> {
user: User.from(payload['user']),
roles: guild.roles,
guild: guild,
member: payload,
voice: VoiceManager.empty(payload['deaf'], payload['mute'], payload['user']['id'], _guildId)
);

Expand Down
41 changes: 3 additions & 38 deletions lib/src/api/messages/dm_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,46 +68,11 @@ class DmMessage extends PartialMessage<DmChannel> {
User? user = client.users.cache.get(payload['author']['id']);

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

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 ? EmbedFooter(
text: element['footer']['text'],
iconUrl: element['footer']['icon_url'],
proxyIconUrl: element['footer']['proxy_icon_url'],
) : null,
image: element['image'] != null ? EmbedImage(
url: element['image']['url'],
proxyUrl: element['image']['proxy_url'],
height: element['image']['height'],
width: element['image']['width'],
) : null,
author: element['author'] != null ? EmbedAuthor(
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']) {
Expand Down
44 changes: 4 additions & 40 deletions lib/src/api/messages/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -161,43 +161,7 @@ class Message extends PartialMessage<TextBasedChannel> {
List<EmbedBuilder> embeds = [];
if (payload['embeds'] != null) {
for (dynamic element in payload['embeds']) {
List<EmbedField> fields = [];
if (element['fields'] != null) {
for (dynamic item in element['fields']) {
fields.add(EmbedField(
name: item['name'],
value: item['value'],
inline: item['inline'] ?? false
));
}
}

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 ? EmbedFooter(
text: element['footer']['text'],
iconUrl: element['footer']['icon_url'],
proxyIconUrl: element['footer']['proxy_icon_url'],
) : null,
image: element['image'] != null ? EmbedImage(
url: element['image']['url'],
proxyUrl: element['image']['proxy_url'],
height: element['image']['height'],
width: element['image']['width'],
) : null,
author: element['author'] != null ? EmbedAuthor(
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);
embeds.add(EmbedBuilder.from(element));
}
}

Expand Down Expand Up @@ -247,7 +211,7 @@ class Message extends PartialMessage<TextBasedChannel> {

final message = Message(
payload['id'],
payload['content'],
payload['content'] ?? '',
payload['tts'] ?? false,
embeds,
payload['allow_mentions'] ?? false,
Expand All @@ -257,11 +221,11 @@ class Message extends PartialMessage<TextBasedChannel> {
payload['payload'],
messageAttachments,
payload['flags'],
payload['pinned'],
payload['pinned'] ?? false,
channel.guild.id,
payload['channel_id'],
MessageReactionManager<GuildChannel, Message>(channel),
payload['timestamp'],
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']))
Expand Down

0 comments on commit 96987ad

Please sign in to comment.