Skip to content

Commit fa5d345

Browse files
committed
[DiscordCache] Add support for caching IAttachment in a message
1 parent e10cf66 commit fa5d345

2 files changed

Lines changed: 135 additions & 51 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using Discord;
2+
using MongoDB.Bson.Serialization.Attributes;
3+
4+
namespace XeniaBot.DiscordCache.Models;
5+
6+
public class CacheMessageAttachment
7+
{
8+
public string AttachmentId { get; set; } = "0";
9+
public DateTimeOffset AttachmentCreatedAt { get; set; }
10+
11+
public string Filename { get; set; }
12+
public string Url { get; set; }
13+
public string ProxyUrl { get; set; }
14+
public int Size { get; set; }
15+
[BsonIgnoreIfNull]
16+
public int? Height { get; set; }
17+
[BsonIgnoreIfNull]
18+
public int? Width { get; set; }
19+
public bool Ephemeral { get; set; }
20+
[BsonIgnoreIfNull]
21+
public string? Description { get; set; }
22+
[BsonIgnoreIfNull]
23+
public string? ContentType { get; set; }
24+
[BsonIgnoreIfNull]
25+
public double? Duration { get; set; }
26+
[BsonIgnoreIfNull]
27+
public string? Waveform { get; set; }
28+
[BsonIgnoreIfNull]
29+
public byte[]? WaveformBytes { get; set; }
30+
public AttachmentFlags Flags { get; set; }
31+
[BsonIgnoreIfNull]
32+
public string[]? ClipParticipantUserIds { get; set; }
33+
[BsonIgnoreIfNull]
34+
public string? Title { get; set; }
35+
[BsonIgnoreIfNull]
36+
public DateTimeOffset? ClipCreatedAt { get; set; }
37+
38+
public CacheMessageAttachment Update(IAttachment other)
39+
{
40+
AttachmentId = other.Id.ToString();
41+
AttachmentCreatedAt = other.CreatedAt;
42+
43+
Filename = other.Filename;
44+
Url = other.Url;
45+
ProxyUrl = other.ProxyUrl;
46+
Size = other.Size;
47+
Height = other.Height;
48+
Width = other.Width;
49+
Ephemeral = other.Ephemeral;
50+
Description = other.Description;
51+
ContentType = other.ContentType;
52+
Duration = other.Duration;
53+
Waveform = other.Waveform;
54+
WaveformBytes = other.WaveformBytes;
55+
Flags = other.Flags;
56+
ClipParticipantUserIds = other.ClipParticipants == null
57+
? null
58+
: other.ClipParticipants.Select(e => e.Id.ToString()).ToArray();
59+
Title = other.Title;
60+
ClipCreatedAt = other.ClipCreatedAt;
61+
return this;
62+
}
63+
64+
public static CacheMessageAttachment? FromExisting(IAttachment? other)
65+
{
66+
if (other == null)
67+
return null;
68+
69+
return new CacheMessageAttachment().Update(other);
70+
}
71+
}

XeniaBot.DiscordCache/Models/Message/CacheMessageModel.cs

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,22 @@ public class CacheMessageModel : DiscordCacheBaseModel
2323
public bool IsSuppressed { get; set; }
2424
public bool MentionedEveryone { get; set; }
2525

26-
public ulong[] MentionedChannelIds { get; set; }
27-
public ulong[] MentionedRoleIds { get; set; }
28-
public ulong[] MentionedUserIds { get; set; }
26+
[BsonIgnoreIfNull]
27+
public ulong[]? MentionedChannelIds { get; set; }
28+
[BsonIgnoreIfNull]
29+
public ulong[]? MentionedRoleIds { get; set; }
30+
[BsonIgnoreIfNull]
31+
public ulong[]? MentionedUserIds { get; set; }
2932
[BsonIgnoreIfNull]
3033
public CacheMessageActivity? Activity { get; set; }
3134
[BsonIgnoreIfNull]
3235
public CacheMessageApplication? Application { get; set; }
3336
[BsonIgnoreIfNull]
3437
public CacheMessageReference? Reference { get; set; }
35-
public Dictionary<CacheEmote, CacheReactionMetadata> Reactions { get; set; }
36-
public CacheMessageComponent[] Components { get; set; }
38+
[BsonIgnoreIfNull]
39+
public Dictionary<CacheEmote, CacheReactionMetadata>? Reactions { get; set; }
40+
[BsonIgnoreIfNull]
41+
public CacheMessageComponent[]? Components { get; set; }
3742
[BsonIgnoreIfNull]
3843
public CacheStickerItem[]? Stickers { get; set; }
3944
[BsonIgnoreIfNull]
@@ -42,6 +47,8 @@ public class CacheMessageModel : DiscordCacheBaseModel
4247
public CacheMessageInteraction? Interaction { get; set; }
4348
[BsonIgnoreIfNull]
4449
public CacheMessageInteractionMetadata? InteractionMetadata { get; set; }
50+
[BsonIgnoreIfNull]
51+
public List<CacheMessageAttachment>? Attachments { get; set; }
4552
#endregion
4653

4754
#region ISnowflakeEntity
@@ -58,71 +65,77 @@ public CacheMessageModel()
5865
{
5966
Content = "";
6067
ContentClean = "";
61-
Embeds = Array.Empty<CacheMessageEmbed>();
62-
Tags = Array.Empty<CacheMessageTag>();
6368
IsDeleted = false;
6469
DeletedTimestamp = DateTimeOffset.FromUnixTimeMilliseconds(0);
65-
MentionedChannelIds = [];
66-
MentionedRoleIds = [];
67-
MentionedUserIds = [];
68-
Reactions = [];
69-
Components = [];
7070
}
7171

7272
public CacheMessageModel Update(IMessage message)
7373
{
74-
this.Content = message.Content;
75-
this.Timestamp = message.Timestamp;
76-
this.EditedTimestamp = message.EditedTimestamp;
77-
this.Embeds = message.Embeds.Select(CacheMessageEmbed.FromExisting)
78-
.Where(v => v != null)
79-
.Cast<CacheMessageEmbed>()
80-
.ToArray();
81-
this.Tags = message.Tags.Select(CacheMessageTag.FromExisting)
82-
.Where(v => v != null)
83-
.Cast<CacheMessageTag>()
84-
.ToArray();
85-
this.Source = message.Source;
86-
this.IsTTS = message.IsTTS;
87-
this.Pinned = message.IsPinned;
88-
this.IsSuppressed = message.IsSuppressed;
89-
this.MentionedEveryone = message.MentionedEveryone;
90-
this.MentionedChannelIds = message.MentionedChannelIds.ToArray();
91-
this.MentionedRoleIds = message.MentionedRoleIds.ToArray();
92-
this.MentionedUserIds = message.MentionedUserIds.ToArray();
93-
this.Activity = CacheMessageActivity.FromExisting(message.Activity);
94-
this.Application = CacheMessageApplication.FromExisting(message.Application);
95-
this.Reference = CacheMessageReference.FromExisting(message.Reference);
96-
this.Components = message.Components
74+
Content = message.Content;
75+
Timestamp = message.Timestamp;
76+
EditedTimestamp = message.EditedTimestamp;
77+
Embeds = message.Embeds.Count == 0 ? null
78+
: message.Embeds.Select(CacheMessageEmbed.FromExisting)
79+
.Where(v => v != null)
80+
.Cast<CacheMessageEmbed>()
81+
.ToArray();
82+
Tags = message.Tags.Count == 0 ? null
83+
: message.Tags.Select(CacheMessageTag.FromExisting)
84+
.Where(v => v != null)
85+
.Cast<CacheMessageTag>()
86+
.ToArray();
87+
Source = message.Source;
88+
IsTTS = message.IsTTS;
89+
Pinned = message.IsPinned;
90+
IsSuppressed = message.IsSuppressed;
91+
MentionedEveryone = message.MentionedEveryone;
92+
MentionedChannelIds = message.MentionedChannelIds.Count == 0
93+
? null : message.MentionedChannelIds.ToArray();
94+
MentionedRoleIds = message.MentionedRoleIds.Count == 0
95+
? null : message.MentionedRoleIds.ToArray();
96+
MentionedUserIds = message.MentionedUserIds.Count == 0
97+
? null : message.MentionedUserIds.ToArray();
98+
Activity = CacheMessageActivity.FromExisting(message.Activity);
99+
Application = CacheMessageApplication.FromExisting(message.Application);
100+
Reference = CacheMessageReference.FromExisting(message.Reference);
101+
Components = message.Components.Count == 0 ? null
102+
: message.Components
97103
.Select(CacheMessageComponent.FromExisting)
98104
.Where(v => v != null)
99105
.Cast<CacheMessageComponent>()
100106
.ToArray();
101-
this.Stickers = message.Stickers
102-
.Select(CacheStickerItem.FromExisting)
103-
.Where(v => v != null)
104-
.Cast<CacheStickerItem>()
105-
.ToArray();
106-
this.Flags = message.Flags;
107+
Stickers = message.Stickers.Count == 0 ? null
108+
: message.Stickers
109+
.Select(CacheStickerItem.FromExisting)
110+
.Where(v => v != null)
111+
.Cast<CacheStickerItem>()
112+
.ToArray();
113+
Flags = message.Flags;
107114
if (message is IUserMessage usrMsg)
108115
{
109-
this.InteractionMetadata = CacheMessageInteractionMetadata.FromExisting(usrMsg.InteractionMetadata);
116+
InteractionMetadata = CacheMessageInteractionMetadata.FromExisting(usrMsg.InteractionMetadata);
110117
}
111118
else
112119
{
113-
this.InteractionMetadata = null;
120+
InteractionMetadata = null;
114121
}
115-
this.Interaction = null;
116-
this.CreatedAt = message.CreatedAt;
117-
this.Snowflake = message.Id;
118-
this.AuthorId = message.Author.Id;
119-
this.ChannelId = message.Channel.Id;
120-
this.GuildId = 0;
122+
Interaction = null;
123+
CreatedAt = message.CreatedAt;
124+
Snowflake = message.Id;
125+
AuthorId = message.Author.Id;
126+
ChannelId = message.Channel.Id;
127+
GuildId = 0;
121128

122-
if (message != null && message.Channel is SocketGuildChannel sgc)
129+
if (message.Channel is SocketGuildChannel sgc)
123130
{
124-
this.GuildId = sgc.Guild.Id;
131+
GuildId = sgc.Guild.Id;
125132
}
133+
134+
Attachments = message.Attachments == null || message.Attachments.Count < 1
135+
? null
136+
: message.Attachments.Select(CacheMessageAttachment.FromExisting)
137+
.Where(e => e != null)
138+
.Cast<CacheMessageAttachment>().ToList();
126139
return this;
127140
}
128141
public static CacheMessageModel? FromExisting(IMessage? message)

0 commit comments

Comments
 (0)