Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V4: State and Cache providers #2251

Open
wants to merge 12 commits into
base: 4.0
Choose a base branch
from
116 changes: 116 additions & 0 deletions src/Discord.Net.Core/Cache/CacheableEntityExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
internal static class CacheableEntityExtensions
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason why these methods arent returning the generic type parameters? We could downcast it later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, its just whats there atm. it could change once I implement the rest of the entities

{
public static IActivityModel ToModel<TModel>(this RichGame richGame) where TModel : IActivityModel, new()
{
return new TModel()
{
ApplicationId = richGame.ApplicationId,
SmallImage = richGame.SmallAsset?.ImageId,
SmallText = richGame.SmallAsset?.Text,
LargeImage = richGame.LargeAsset?.ImageId,
LargeText = richGame.LargeAsset?.Text,
Details = richGame.Details,
Flags = richGame.Flags,
Name = richGame.Name,
Type = richGame.Type,
JoinSecret = richGame.Secrets?.Join,
SpectateSecret = richGame.Secrets?.Spectate,
MatchSecret = richGame.Secrets?.Match,
State = richGame.State,
PartyId = richGame.Party?.Id,
PartySize = richGame.Party?.Members != null && richGame.Party?.Capacity != null
? new long[] { richGame.Party.Members, richGame.Party.Capacity }
: null,
TimestampEnd = richGame.Timestamps?.End,
TimestampStart = richGame.Timestamps?.Start
};
}

public static IActivityModel ToModel<TModel>(this SpotifyGame spotify) where TModel : IActivityModel, new()
{
return new TModel()
{
Name = spotify.Name,
SessionId = spotify.SessionId,
SyncId = spotify.TrackId,
LargeText = spotify.AlbumTitle,
Details = spotify.TrackTitle,
State = string.Join(";", spotify.Artists),
TimestampEnd = spotify.EndsAt,
TimestampStart = spotify.StartedAt,
LargeImage = spotify.AlbumArt,
Type = ActivityType.Listening,
Flags = spotify.Flags,
};
}

public static IActivityModel ToModel<TModel, TEmoteModel>(this CustomStatusGame custom)
where TModel : IActivityModel, new()
where TEmoteModel : IEmojiModel, new()
{
return new TModel
{
Id = "custom",
Type = ActivityType.CustomStatus,
Name = custom.Name,
State = custom.State,
Emoji = custom.Emote.ToModel<TEmoteModel>(),
CreatedAt = custom.CreatedAt
};
}

public static IActivityModel ToModel<TModel>(this StreamingGame stream) where TModel : IActivityModel, new()
{
return new TModel
{
Name = stream.Name,
Url = stream.Url,
Flags = stream.Flags,
Details = stream.Details
};
}

public static IEmojiModel ToModel(this IEmote emote, IEmojiModel model)
{
if (emote == null)
return null;

model.Name = emote.Name;

if (emote is GuildEmote guildEmote)
{
model.Id = guildEmote.Id;
model.IsAnimated = guildEmote.Animated;
model.IsAvailable = guildEmote.IsAvailable;
model.IsManaged = guildEmote.IsManaged;
model.CreatorId = guildEmote.CreatorId;
model.RequireColons = guildEmote.RequireColons;
model.Roles = guildEmote.RoleIds.ToArray();
}

if (emote is Emote e)
{
model.IsAnimated = e.Animated;
model.Id = e.Id;
}

return model;
}

public static IEmojiModel ToModel<TModel>(this IEmote emote) where TModel : IEmojiModel, new()
{
if (emote == null)
return null;

return emote.ToModel(new TModel());
}
}
}
20 changes: 20 additions & 0 deletions src/Discord.Net.Core/Cache/ICached.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
internal interface ICached<TType> : ICached, IDisposable
{
void Update(TType model);

TType ToModel();
}

public interface ICached
{
bool IsFreed { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
public interface IPartialApplicationModel : IEntityModel<ulong>
{
string Name { get; set; }
string Icon { get; set; }
string Description { get; set; }
string CoverImage { get; set; }
}
}
21 changes: 21 additions & 0 deletions src/Discord.Net.Core/Cache/Models/Emoji/IEmojiModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
public interface IEmojiModel
{
ulong? Id { get; set; }
string Name { get; set; }
ulong[] Roles { get; set; }
bool RequireColons { get; set; }
bool IsManaged { get; set; }
bool IsAnimated { get; set; }
bool IsAvailable { get; set; }

ulong? CreatorId { get; set; }
}
}
13 changes: 13 additions & 0 deletions src/Discord.Net.Core/Cache/Models/IEntityModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
public interface IEntityModel<TId> where TId : IEquatable<TId>
{
TId Id { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
public interface IMessageComponentModel
{
ComponentType Type { get; set; }
string CustomId { get; set; }
bool? Disabled { get; set; }
ButtonStyle? Style { get; set; }
string Label { get; set; }

// emoji
ulong? EmojiId { get; set; }
string EmojiName { get; set; }
bool? EmojiAnimated { get; set; }

string Url { get; set; }

IMessageComponentOptionModel[] Options { get; set; }

string Placeholder { get; set; }
int? MinValues { get; set; }
int? MaxValues { get; set; }
IMessageComponentModel[] Components { get; set; }
int? MinLength { get; set; }
int? MaxLength { get; set; }
bool? Required { get; set; }
string Value { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
public interface IMessageComponentOptionModel
{
string Label { get; set; }
string Value { get; set; }
string Description { get; set; }

// emoji
ulong? EmojiId { get; set; }
string EmojiName { get; set; }
bool? EmojiAnimated { get; set; }

bool? Default { get; set; }
}
}
21 changes: 21 additions & 0 deletions src/Discord.Net.Core/Cache/Models/Message/IAttachmentModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
public interface IAttachmentModel : IEntityModel<ulong>
{
string FileName { get; set; }
string Description { get; set; }
string ContentType { get; set; }
int Size { get; set; }
string Url { get; set; }
string ProxyUrl { get; set; }
int? Height { get; set; }
int? Width { get; set; }
bool Ephemeral { get; set; }
}
}
44 changes: 44 additions & 0 deletions src/Discord.Net.Core/Cache/Models/Message/IEmbedModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
public interface IEmbedModel
{
string Title { get; set; }
EmbedType Type { get; set; }
string Description { get; set; }
string Url { get; set; }
long? Timestamp { get; set; }
uint? Color { get; set; }
string FooterText { get; set; }
string FooterIconUrl { get; set; }
string FooterProxyUrl { get; set; }
string ProviderName { get; set; }
string ProviderUrl { get; set; }
string AuthorName { get; set; }
string AuthorUrl { get; set; }
string AuthorIconUrl { get; set; }
string AuthorProxyIconUrl { get; set; }
IEmbedMediaModel Image { get; set; }
IEmbedMediaModel Thumbnail { get; set; }
IEmbedMediaModel Video { get; set; }
IEmbedFieldModel[] Fields { get; set; }
}
public interface IEmbedMediaModel
{
string Url { get; set; }
string ProxyUrl { get; set; }
int? Height { get; set; }
int? Width { get; set; }
}
public interface IEmbedFieldModel
{
string Name { get; set; }
string Value { get; set; }
bool Inline { get; set; }
}
}
14 changes: 14 additions & 0 deletions src/Discord.Net.Core/Cache/Models/Message/IMessageActivityModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
public interface IMessageActivityModel
{
MessageActivityType? Type { get; set; }
string PartyId { get; set; }
}
}
48 changes: 48 additions & 0 deletions src/Discord.Net.Core/Cache/Models/Message/IMessageModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
public interface IMessageModel : IEntityModel<ulong>
{
MessageType Type { get; set; }
ulong ChannelId { get; set; }
ulong? GuildId { get; set; }
ulong AuthorId { get; set; }
bool IsWebhookMessage { get; set; }
string Content { get; set; }
long Timestamp { get; set; }
long? EditedTimestamp { get; set; }
bool IsTextToSpeech { get; set; }
bool MentionEveryone { get; set; }
ulong[] UserMentionIds { get; set; }
ulong[] RoleMentionIds { get; set; }

IAttachmentModel[] Attachments { get; set; }
IEmbedModel[] Embeds { get; set; }
IReactionMetadataModel[] Reactions { get; set; }
bool Pinned { get; set; }
IMessageActivityModel Activity { get; set; }
IPartialApplicationModel Application { get; set; }
ulong? ApplicationId { get; set; }

// message reference
ulong? ReferenceMessageId { get; set; }
ulong? ReferenceMessageChannelId { get; set; }
ulong? ReferenceMessageGuildId { get; set; }

MessageFlags Flags { get; set; }

// interaction
ulong? InteractionId { get; set; }
string InteractionName { get; set; }
InteractionType? InteractionType { get; set; }
ulong? InteractionUserId { get; set; }

IMessageComponentModel[] Components { get; set; }
IStickerItemModel[] Stickers { get; set; }
}
}
14 changes: 14 additions & 0 deletions src/Discord.Net.Core/Cache/Models/Message/IReactionModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
public interface IReactionMetadataModel
{
IEmojiModel Emoji { get; set; }
ulong[] Users { get; set; }
}
}
Loading