/// <inheritdoc /> public override async Task <Result <IGuild> > ModifyGuildAsync ( Snowflake guildID, Optional <string> name = default, Optional <VerificationLevel?> verificationLevel = default, Optional <MessageNotificationLevel?> defaultMessageNotifications = default, Optional <ExplicitContentFilterLevel?> explicitContentFilter = default, Optional <Snowflake?> afkChannelID = default, Optional <TimeSpan> afkTimeout = default, Optional <Stream?> icon = default, Optional <Snowflake> ownerID = default, Optional <Stream?> splash = default, Optional <Stream?> discoverySplash = default, Optional <Stream?> banner = default, Optional <Snowflake?> systemChannelID = default, Optional <SystemChannelFlags> systemChannelFlags = default, Optional <Snowflake?> rulesChannelID = default, Optional <Snowflake?> publicUpdatesChannelID = default, Optional <string?> preferredLocale = default, Optional <IReadOnlyList <GuildFeature> > features = default, Optional <string?> description = default, CancellationToken ct = default ) { var modifyResult = await base.ModifyGuildAsync ( guildID, name, verificationLevel, defaultMessageNotifications, explicitContentFilter, afkChannelID, afkTimeout, icon, ownerID, splash, discoverySplash, banner, systemChannelID, systemChannelFlags, rulesChannelID, publicUpdatesChannelID, preferredLocale, features, description, ct ); if (!modifyResult.IsSuccess) { return(modifyResult); } var guild = modifyResult.Entity; var key = KeyHelpers.CreateGuildCacheKey(guild.ID); _cacheService.Cache(key, guild); return(modifyResult); }
/// <inheritdoc /> public override async Task <IRetrieveRestEntityResult <IUser> > GetCurrentUserAsync(CancellationToken ct = default) { var key = KeyHelpers.CreateCurrentUserCacheKey(); if (_cacheService.TryGetValue <IUser>(key, out var cachedInstance)) { return(RetrieveRestEntityResult <IUser> .FromSuccess(cachedInstance)); } var getUser = await base.GetCurrentUserAsync(ct); if (!getUser.IsSuccess) { return(getUser); } var user = getUser.Entity; var userKey = KeyHelpers.CreateUserCacheKey(user.ID); // Cache this as both a normal user and our current user _cacheService.Cache(key, user); _cacheService.Cache(userKey, user); return(getUser); }
/// <inheritdoc /> public override async Task <IRetrieveRestEntityResult <IReadOnlyList <IChannel> > > GetUserDMsAsync ( CancellationToken ct = default ) { var key = KeyHelpers.CreateCurrentUserDMsCacheKey(); if (_cacheService.TryGetValue <IReadOnlyList <IChannel> >(key, out var cachedInstance)) { return(RetrieveRestEntityResult <IReadOnlyList <IChannel> > .FromSuccess(cachedInstance)); } var getUserDMs = await base.GetUserDMsAsync(ct); if (!getUserDMs.IsSuccess) { return(getUserDMs); } var userDMs = getUserDMs.Entity; _cacheService.Cache(key, userDMs); foreach (var dm in userDMs) { var channelKey = KeyHelpers.CreateChannelCacheKey(dm.ID); _cacheService.Cache(channelKey, dm); } return(getUserDMs); }
/// <inheritdoc /> public override async Task <Result <IReadOnlyList <IWebhook> > > GetGuildWebhooksAsync ( Snowflake guildID, CancellationToken ct = default ) { var key = KeyHelpers.CreateGuildWebhooksCacheKey(guildID); if (_cacheService.TryGetValue <IReadOnlyList <IWebhook> >(key, out var cachedInstance)) { return(Result <IReadOnlyList <IWebhook> > .FromSuccess(cachedInstance)); } var getWebhooks = await base.GetGuildWebhooksAsync(guildID, ct); if (!getWebhooks.IsSuccess) { return(getWebhooks); } var webhooks = getWebhooks.Entity; _cacheService.Cache(key, webhooks); foreach (var webhook in webhooks) { var webhookKey = KeyHelpers.CreateWebhookCacheKey(webhook.ID); _cacheService.Cache(webhookKey, webhook); } return(getWebhooks); }
/// <inheritdoc /> public override async Task <Result <IEmoji> > GetGuildEmojiAsync ( Snowflake guildID, Snowflake emojiID, CancellationToken ct = default ) { var key = KeyHelpers.CreateEmojiCacheKey(guildID, emojiID); if (_cacheService.TryGetValue <IEmoji>(key, out var cachedInstance)) { return(Result <IEmoji> .FromSuccess(cachedInstance)); } var getResult = await base.GetGuildEmojiAsync(guildID, emojiID, ct); if (!getResult.IsSuccess) { return(getResult); } var emoji = getResult.Entity; _cacheService.Cache(key, emoji); return(getResult); }
/// <inheritdoc /> public override async Task <Result <IChannel> > StartThreadWithoutMessageAsync ( Snowflake channelID, string name, TimeSpan autoArchiveDuration, Optional <ChannelType> type, CancellationToken ct = default ) { var createResult = await base.StartThreadWithoutMessageAsync ( channelID, name, autoArchiveDuration, type, ct ); if (!createResult.IsSuccess) { return(createResult); } var key = KeyHelpers.CreateChannelCacheKey(channelID); _cacheService.Cache(key, createResult.Entity); return(createResult); }
/// <inheritdoc /> public override async Task <Result <IApplication> > GetCurrentBotApplicationInformationAsync ( CancellationToken ct = default ) { var key = KeyHelpers.CreateCurrentApplicationCacheKey(); if (_cacheService.TryGetValue <IApplication>(key, out var cachedInstance)) { return(Result <IApplication> .FromSuccess(cachedInstance)); } var getCurrent = await base.GetCurrentBotApplicationInformationAsync(ct); if (!getCurrent.IsSuccess) { return(getCurrent); } var application = getCurrent.Entity; _cacheService.Cache(key, application); return(getCurrent); }
/// <inheritdoc /> public override async Task <Result <IGuild> > GetGuildAsync ( Snowflake guildID, Optional <bool> withCounts = default, CancellationToken ct = default ) { var key = KeyHelpers.CreateGuildCacheKey(guildID); if (_cacheService.TryGetValue <IGuild>(key, out var cachedInstance)) { return(Result <IGuild> .FromSuccess(cachedInstance)); } var getResult = await base.GetGuildAsync(guildID, withCounts, ct); if (!getResult.IsSuccess) { return(getResult); } var guild = getResult.Entity; _cacheService.Cache(key, guild); return(getResult); }
/// <inheritdoc /> public override async Task <Result <IReadOnlyList <IChannel> > > GetGuildChannelsAsync ( Snowflake guildID, CancellationToken ct = default ) { var key = KeyHelpers.CreateGuildChannelsCacheKey(guildID); if (_cacheService.TryGetValue <IReadOnlyList <IChannel> >(key, out var cachedInstance)) { return(Result <IReadOnlyList <IChannel> > .FromSuccess(cachedInstance)); } var getResult = await base.GetGuildChannelsAsync(guildID, ct); if (!getResult.IsSuccess) { return(getResult); } var channels = getResult.Entity; _cacheService.Cache(key, channels); foreach (var channel in channels) { var channelKey = KeyHelpers.CreateChannelCacheKey(channel.ID); _cacheService.Cache(channelKey, channel); } return(getResult); }
/// <inheritdoc /> public override async Task <IRetrieveRestEntityResult <IGuildPreview> > GetGuildPreviewAsync ( Snowflake guildID, CancellationToken ct = default ) { var key = KeyHelpers.CreateGuildPreviewCacheKey(guildID); if (_cacheService.TryGetValue <IGuildPreview>(key, out var cachedInstance)) { return(RetrieveRestEntityResult <IGuildPreview> .FromSuccess(cachedInstance)); } var getResult = await base.GetGuildPreviewAsync(guildID, ct); if (!getResult.IsSuccess) { return(getResult); } var guildPreview = getResult.Entity; _cacheService.Cache(key, guildPreview); return(getResult); }
/// <inheritdoc /> public override async Task <Result <IInvite> > GetInviteAsync ( string inviteCode, Optional <bool> withCounts = default, Optional <bool> withExpiration = default, CancellationToken ct = default ) { var key = KeyHelpers.CreateInviteCacheKey(inviteCode); if (_cacheService.TryGetValue <IInvite>(key, out var cachedInstance)) { return(Result <IInvite> .FromSuccess(cachedInstance)); } var getInvite = await base.GetInviteAsync(inviteCode, withCounts, withExpiration, ct); if (!getInvite.IsSuccess) { return(getInvite); } var invite = getInvite.Entity; _cacheService.Cache(key, invite); return(getInvite); }
/// <inheritdoc/> public Task <Result> RespondAsync(IMessageReactionAdd gatewayEvent, CancellationToken ct = default) { if (!gatewayEvent.GuildID.HasValue) { return(Task.FromResult(Result.FromSuccess())); } if (!gatewayEvent.Member.HasValue) { return(Task.FromResult(Result.FromSuccess())); } var member = gatewayEvent.Member.Value; if (!member.User.HasValue) { return(Task.FromResult(Result.FromSuccess())); } var key = KeyHelpers.CreateGuildMemberKey(gatewayEvent.GuildID.Value, member.User.Value.ID); _cacheService.Cache(key, member); return(Task.FromResult(Result.FromSuccess())); }
/// <inheritdoc /> public override async Task <IModifyRestEntityResult <IMessage> > EditMessageAsync ( Snowflake channelID, Snowflake messageID, Optional <string?> content = default, Optional <IEmbed?> embed = default, Optional <MessageFlags?> flags = default, Optional <IAllowedMentions?> allowedMentions = default, CancellationToken ct = default ) { var editResult = await base.EditMessageAsync ( channelID, messageID, content, embed, flags, allowedMentions, ct ); if (!editResult.IsSuccess) { return(editResult); } var message = editResult.Entity; var key = KeyHelpers.CreateMessageCacheKey(channelID, messageID); _cacheService.Cache(key, message); return(editResult); }
/// <inheritdoc /> public override async Task <Result <ITemplate> > GetTemplateAsync ( string templateCode, CancellationToken ct = default ) { var key = KeyHelpers.CreateTemplateCacheKey(templateCode); if (_cacheService.TryGetValue <ITemplate>(key, out var cachedInstance)) { return(Result <ITemplate> .FromSuccess(cachedInstance)); } var getTemplate = await base.GetTemplateAsync(templateCode, ct); if (!getTemplate.IsSuccess) { return(getTemplate); } var template = getTemplate.Entity; _cacheService.Cache(key, template); return(getTemplate); }
/// <inheritdoc /> public override async Task <Result <IMessage> > GetChannelMessageAsync ( Snowflake channelID, Snowflake messageID, CancellationToken ct = default ) { var key = KeyHelpers.CreateMessageCacheKey(channelID, messageID); if (_cacheService.TryGetValue <IMessage>(key, out var cachedInstance)) { return(Result <IMessage> .FromSuccess(cachedInstance)); } var getMessage = await base.GetChannelMessageAsync(channelID, messageID, ct); if (!getMessage.IsSuccess) { return(getMessage); } var message = getMessage.Entity; _cacheService.Cache(key, message); return(getMessage); }
/// <inheritdoc /> public override async Task <Result <IGuildMember> > GetGuildMemberAsync ( Snowflake guildID, Snowflake userID, CancellationToken ct = default ) { var key = KeyHelpers.CreateGuildMemberKey(guildID, userID); if (_cacheService.TryGetValue <IGuildMember>(key, out var cachedInstance)) { return(Result <IGuildMember> .FromSuccess(cachedInstance)); } var getResult = await base.GetGuildMemberAsync(guildID, userID, ct); if (!getResult.IsSuccess) { return(getResult); } var guildMember = getResult.Entity; if (!guildMember.User.HasValue) { return(getResult); } _cacheService.Cache(key, guildMember); return(getResult); }
/// <inheritdoc /> public override async Task <Result <IReadOnlyList <IMessage> > > GetPinnedMessagesAsync ( Snowflake channelID, CancellationToken ct = default ) { var key = KeyHelpers.CreatePinnedMessagesCacheKey(channelID); if (_cacheService.TryGetValue <IReadOnlyList <IMessage> >(key, out var cachedInstance)) { return(Result <IReadOnlyList <IMessage> > .FromSuccess(cachedInstance)); } var getResult = await base.GetPinnedMessagesAsync(channelID, ct); if (!getResult.IsSuccess) { return(getResult); } var messages = getResult.Entity; _cacheService.Cache(key, messages); foreach (var message in messages) { var messageKey = KeyHelpers.CreateMessageCacheKey(channelID, message.ID); _cacheService.Cache(messageKey, message); } return(getResult); }
/// <inheritdoc /> public override async Task <Result <IReadOnlyList <IBan> > > GetGuildBansAsync ( Snowflake guildID, CancellationToken ct = default ) { var collectionKey = KeyHelpers.CreateGuildBansCacheKey(guildID); if (_cacheService.TryGetValue <IReadOnlyList <IBan> >(collectionKey, out var cachedInstance)) { return(Result <IReadOnlyList <IBan> > .FromSuccess(cachedInstance)); } var getResult = await base.GetGuildBansAsync(guildID, ct); if (!getResult.IsSuccess) { return(getResult); } var bans = getResult.Entity; _cacheService.Cache(collectionKey, bans); foreach (var ban in bans) { var key = KeyHelpers.CreateGuildBanCacheKey(guildID, ban.User.ID); _cacheService.Cache(key, ban); } return(getResult); }
/// <inheritdoc /> public override async Task <Result <IChannel> > GetChannelAsync ( Snowflake channelID, CancellationToken ct = default ) { var key = KeyHelpers.CreateChannelCacheKey(channelID); if (_cacheService.TryGetValue <IChannel>(key, out var cachedInstance)) { return(Result <IChannel> .FromSuccess(cachedInstance)); } var getChannel = await base.GetChannelAsync(channelID, ct); if (!getChannel.IsSuccess) { return(getChannel); } var channel = getChannel.Entity; _cacheService.Cache(key, channel); return(getChannel); }
/// <inheritdoc /> public override async Task <Result <IBan> > GetGuildBanAsync ( Snowflake guildID, Snowflake userID, CancellationToken ct = default ) { var key = KeyHelpers.CreateGuildBanCacheKey(guildID, userID); if (_cacheService.TryGetValue <IBan>(key, out var cachedInstance)) { return(Result <IBan> .FromSuccess(cachedInstance)); } var getResult = await base.GetGuildBanAsync(guildID, userID, ct); if (!getResult.IsSuccess) { return(getResult); } var ban = getResult.Entity; _cacheService.Cache(key, ban); return(getResult); }
/// <inheritdoc /> public override async Task <Result <IReadOnlyList <IVoiceRegion> > > ListVoiceRegionsAsync ( CancellationToken ct = default ) { var key = KeyHelpers.CreateVoiceRegionsCacheKey(); if (_cacheService.TryGetValue <IReadOnlyList <IVoiceRegion> >(key, out var cachedInstance)) { return(Result <IReadOnlyList <IVoiceRegion> > .FromSuccess(cachedInstance)); } var listRegions = await base.ListVoiceRegionsAsync(ct); if (!listRegions.IsSuccess) { return(listRegions); } var regions = listRegions.Entity; _cacheService.Cache(key, regions); foreach (var voiceRegion in regions) { var regionKey = KeyHelpers.CreateVoiceRegionCacheKey(voiceRegion.ID); _cacheService.Cache(regionKey, voiceRegion); } return(listRegions); }
/// <inheritdoc /> public override async Task <Result <IReadOnlyList <IRole> > > GetGuildRolesAsync ( Snowflake guildID, CancellationToken ct = default ) { var collectionKey = KeyHelpers.CreateGuildRolesCacheKey(guildID); if (_cacheService.TryGetValue <IReadOnlyList <IRole> >(collectionKey, out var cachedInstance)) { return(Result <IReadOnlyList <IRole> > .FromSuccess(cachedInstance)); } var getRoles = await base.GetGuildRolesAsync(guildID, ct); if (!getRoles.IsSuccess) { return(getRoles); } var roles = getRoles.Entity; _cacheService.Cache(collectionKey, roles); foreach (var role in roles) { var key = KeyHelpers.CreateGuildRoleCacheKey(guildID, role.ID); _cacheService.Cache(key, role); } return(getRoles); }
/// <inheritdoc /> public override async Task <Result <IWebhook> > GetWebhookWithTokenAsync ( Snowflake webhookID, string token, CancellationToken ct = default ) { var key = KeyHelpers.CreateWebhookCacheKey(webhookID); if (_cacheService.TryGetValue <IWebhook>(key, out var cachedInstance)) { return(Result <IWebhook> .FromSuccess(cachedInstance)); } var getWebhook = await base.GetWebhookWithTokenAsync(webhookID, token, ct); if (!getWebhook.IsSuccess) { return(getWebhook); } var webhook = getWebhook.Entity; _cacheService.Cache(key, webhook); return(getWebhook); }
/// <inheritdoc /> public override async Task <Result <IRole> > CreateGuildRoleAsync ( Snowflake guildID, Optional <string> name = default, Optional <IDiscordPermissionSet> permissions = default, Optional <Color> colour = default, Optional <bool> isHoisted = default, Optional <bool> isMentionable = default, CancellationToken ct = default ) { var createResult = await base.CreateGuildRoleAsync ( guildID, name, permissions, colour, isHoisted, isMentionable, ct ); if (!createResult.IsSuccess) { return(createResult); } var role = createResult.Entity; var key = KeyHelpers.CreateGuildRoleCacheKey(guildID, role.ID); _cacheService.Cache(key, role); return(createResult); }
/// <inheritdoc /> public override async Task <Result <IEmoji> > CreateGuildEmojiAsync ( Snowflake guildID, string name, Stream image, IReadOnlyList <Snowflake> roles, CancellationToken ct = default ) { var createResult = await base.CreateGuildEmojiAsync(guildID, name, image, roles, ct); if (!createResult.IsSuccess) { return(createResult); } var emoji = createResult.Entity; if (emoji.ID is null) { // We can't, or shouldn't, cache this return(createResult); } var key = KeyHelpers.CreateEmojiCacheKey(guildID, emoji.ID.Value); _cacheService.Cache(key, emoji); return(createResult); }
/// <inheritdoc/> public Task <Result> RespondAsync(IGuildMembersChunk gatewayEvent, CancellationToken ct = default) { foreach (var member in gatewayEvent.Members) { if (!member.User.HasValue) { continue; } var key = KeyHelpers.CreateGuildMemberKey(gatewayEvent.GuildID, member.User.Value.ID); _cacheService.Cache(key, member); } if (!gatewayEvent.Presences.HasValue) { return(Task.FromResult(Result.FromSuccess())); } foreach (var presence in gatewayEvent.Presences.Value !) { if (!presence.User.ID.HasValue) { continue; } var key = KeyHelpers.CreatePresenceCacheKey(gatewayEvent.GuildID, presence.User.ID.Value); _cacheService.Cache(key, presence); } return(Task.FromResult(Result.FromSuccess())); }
/// <inheritdoc /> public override async Task <IRetrieveRestEntityResult <IReadOnlyList <IConnection> > > GetUserConnectionsAsync ( CancellationToken ct = default ) { var key = KeyHelpers.CreateCurrentUserConnectionsCacheKey(); if (_cacheService.TryGetValue <IReadOnlyList <IConnection> >(key, out var cachedInstance)) { return(RetrieveRestEntityResult <IReadOnlyList <IConnection> > .FromSuccess(cachedInstance)); } var getUserConnections = await base.GetUserConnectionsAsync(ct); if (!getUserConnections.IsSuccess) { return(getUserConnections); } var connections = getUserConnections.Entity; _cacheService.Cache(key, connections); foreach (var connection in connections) { var connectionKey = KeyHelpers.CreateConnectionCacheKey(connection.ID); _cacheService.Cache(connectionKey, connection); } return(getUserConnections); }
/// <inheritdoc/> public Task <Result> RespondAsync(IGuildMemberUpdate gatewayEvent, CancellationToken ct = default) { var key = KeyHelpers.CreateGuildMemberKey(gatewayEvent.GuildID, gatewayEvent.User.ID); // Since this event isn't playing nice, we'll have to update by creating an object of our own. if (_cacheService.TryGetValue <IGuildMember>(key, out var cachedInstance)) { cachedInstance = new GuildMember ( new Optional <IUser>(gatewayEvent.User), gatewayEvent.Nickname.HasValue ? gatewayEvent.Nickname : cachedInstance.Nickname, gatewayEvent.Roles, gatewayEvent.JoinedAt, gatewayEvent.PremiumSince.HasValue ? gatewayEvent.PremiumSince.Value : cachedInstance.PremiumSince, cachedInstance.IsDeafened, cachedInstance.IsMuted, cachedInstance.IsPending, cachedInstance.Permissions ); } else if (gatewayEvent.PremiumSince.HasValue) { cachedInstance = new GuildMember ( new Optional <IUser>(gatewayEvent.User), gatewayEvent.Nickname, gatewayEvent.Roles, gatewayEvent.JoinedAt, gatewayEvent.PremiumSince.Value, false, false,
/// <inheritdoc /> public override async Task <IRetrieveRestEntityResult <IUser> > GetUserAsync ( Snowflake userID, CancellationToken ct = default ) { var key = KeyHelpers.CreateUserCacheKey(userID); if (_cacheService.TryGetValue <IUser>(key, out var cachedInstance)) { return(RetrieveRestEntityResult <IUser> .FromSuccess(cachedInstance)); } var getUser = await base.GetUserAsync(userID, ct); if (!getUser.IsSuccess) { return(getUser); } var user = getUser.Entity; _cacheService.Cache(key, user); return(getUser); }
/// <inheritdoc /> public override async Task <IRetrieveRestEntityResult <IReadOnlyList <ITemplate> > > GetGuildTemplatesAsync ( Snowflake guildID, CancellationToken ct = default ) { var key = KeyHelpers.CreateGuildTemplatesCacheKey(guildID); if (_cacheService.TryGetValue <IReadOnlyList <ITemplate> >(key, out var cachedInstance)) { return(RetrieveRestEntityResult <IReadOnlyList <ITemplate> > .FromSuccess(cachedInstance)); } var getTemplates = await base.GetGuildTemplatesAsync(guildID, ct); if (!getTemplates.IsSuccess) { return(getTemplates); } var templates = getTemplates.Entity; _cacheService.Cache(key, templates); foreach (var template in templates) { var templateKey = KeyHelpers.CreateTemplateCacheKey(template.Code); _cacheService.Cache(templateKey, template); } return(getTemplates); }