コード例 #1
0
        public static ValueTask HandleGatewayEvent(this IDiscordCache cache, IGatewayEvent evt)
        {
            switch (evt)
            {
            case GuildCreateEvent gc:
                return(cache.SaveGuildCreate(gc));

            case GuildUpdateEvent gu:
                return(cache.SaveGuild(gu));

            case GuildDeleteEvent gd:
                return(cache.RemoveGuild(gd.Id));

            case ChannelCreateEvent cc:
                return(cache.SaveChannel(cc));

            case ChannelUpdateEvent cu:
                return(cache.SaveChannel(cu));

            case ChannelDeleteEvent cd:
                return(cache.RemoveChannel(cd.Id));

            case GuildRoleCreateEvent grc:
                return(cache.SaveRole(grc.GuildId, grc.Role));

            case GuildRoleUpdateEvent gru:
                return(cache.SaveRole(gru.GuildId, gru.Role));

            case GuildRoleDeleteEvent grd:
                return(cache.RemoveRole(grd.GuildId, grd.RoleId));

            case MessageReactionAddEvent mra:
                return(cache.TrySaveDmChannelStub(mra.GuildId, mra.ChannelId));

            case MessageCreateEvent mc:
                return(cache.SaveMessageCreate(mc));

            case MessageUpdateEvent mu:
                return(cache.TrySaveDmChannelStub(mu.GuildId.Value, mu.ChannelId));

            case MessageDeleteEvent md:
                return(cache.TrySaveDmChannelStub(md.GuildId, md.ChannelId));

            case MessageDeleteBulkEvent md:
                return(cache.TrySaveDmChannelStub(md.GuildId, md.ChannelId));

            case ThreadCreateEvent tc:
                return(cache.SaveChannel(tc));

            case ThreadUpdateEvent tu:
                return(cache.SaveChannel(tu));

            case ThreadDeleteEvent td:
                return(cache.RemoveChannel(td.Id));

            case ThreadListSyncEvent tls:
                return(cache.SaveThreadListSync(tls));
            }

            return(default);
コード例 #2
0
ファイル: ServerConfig.cs プロジェクト: guccigang17/PluralKit
 public ServerConfig(LoggerCleanService cleanService, IDatabase db, ModelRepository repo, IDiscordCache cache)
 {
     _cleanService = cleanService;
     _db           = db;
     _repo         = repo;
     _cache        = cache;
 }
コード例 #3
0
    public static async Task <PermissionSet> PermissionsFor(this IDiscordCache cache, ulong channelId, ulong userId,
                                                            GuildMemberPartial?member, bool isWebhook = false)
    {
        if (!(await cache.TryGetChannel(channelId) is Channel channel))
        {
            // todo: handle channel not found better
            return(PermissionSet.Dm);
        }

        if (channel.GuildId == null)
        {
            return(PermissionSet.Dm);
        }

        var rootChannel = await cache.GetRootChannel(channelId);

        var guild = await cache.GetGuild(channel.GuildId.Value);

        if (isWebhook)
        {
            return(EveryonePermissions(guild));
        }

        return(PermissionsFor(guild, rootChannel, userId, member));
    }
コード例 #4
0
 public EmbedService(IDatabase db, ModelRepository repo, IDiscordCache cache, DiscordApiClient rest)
 {
     _db    = db;
     _repo  = repo;
     _cache = cache;
     _rest  = rest;
 }
コード例 #5
0
 public static Channel?GetChannelOrNull(this IDiscordCache cache, ulong channelId)
 {
     if (cache.TryGetChannel(channelId, out var channel))
     {
         return(channel);
     }
     return(null);
 }
コード例 #6
0
 public static Channel GetChannel(this IDiscordCache cache, ulong channelId)
 {
     if (!cache.TryGetChannel(channelId, out var channel))
     {
         throw new KeyNotFoundException($"Channel {channelId} not found in cache");
     }
     return(channel);
 }
コード例 #7
0
 public static Guild GetGuild(this IDiscordCache cache, ulong guildId)
 {
     if (!cache.TryGetGuild(guildId, out var guild))
     {
         throw new KeyNotFoundException($"Guild {guildId} not found in cache");
     }
     return(guild);
 }
コード例 #8
0
 // todo: make sure everything uses the minimum amount of REST calls necessary
 public Checks(DiscordApiClient rest, IDiscordCache cache, BotConfig botConfig, ProxyService proxy, ProxyMatcher matcher)
 {
     _rest      = rest;
     _cache     = cache;
     _botConfig = botConfig;
     _proxy     = proxy;
     _matcher   = matcher;
 }
コード例 #9
0
 public static async Task <Guild> GetGuild(this IDiscordCache cache, ulong guildId)
 {
     if (!(await cache.TryGetGuild(guildId) is Guild guild))
     {
         throw new KeyNotFoundException($"Guild {guildId} not found in cache");
     }
     return(guild);
 }
コード例 #10
0
 public WebhookCacheService(ILogger logger, IMetrics metrics, DiscordApiClient rest, IDiscordCache cache)
 {
     _metrics  = metrics;
     _rest     = rest;
     _cache    = cache;
     _logger   = logger.ForContext <WebhookCacheService>();
     _webhooks = new ConcurrentDictionary <ulong, Lazy <Task <Webhook> > >();
 }
コード例 #11
0
 public static async Task <Role> GetRole(this IDiscordCache cache, ulong roleId)
 {
     if (!(await cache.TryGetRole(roleId) is Role role))
     {
         throw new KeyNotFoundException($"Role {roleId} not found in cache");
     }
     return(role);
 }
コード例 #12
0
 public static async Task <User> GetUser(this IDiscordCache cache, ulong userId)
 {
     if (!(await cache.TryGetUser(userId) is User user))
     {
         throw new KeyNotFoundException($"User {userId} not found in cache");
     }
     return(user);
 }
コード例 #13
0
 public static async Task <Channel> GetChannel(this IDiscordCache cache, ulong channelId)
 {
     if (!(await cache.TryGetChannel(channelId) is Channel channel))
     {
         throw new KeyNotFoundException($"Channel {channelId} not found in cache");
     }
     return(channel);
 }
コード例 #14
0
 public static Role GetRole(this IDiscordCache cache, ulong roleId)
 {
     if (!cache.TryGetRole(roleId, out var role))
     {
         throw new KeyNotFoundException($"User {roleId} not found in cache");
     }
     return(role);
 }
コード例 #15
0
 public static User GetUser(this IDiscordCache cache, ulong userId)
 {
     if (!cache.TryGetUser(userId, out var user))
     {
         throw new KeyNotFoundException($"User {userId} not found in cache");
     }
     return(user);
 }
コード例 #16
0
 public Misc(BotConfig botConfig, IMetrics metrics, CpuStatService cpu, ModelRepository repo, ShardInfoService shards, IDiscordCache cache)
 {
     _botConfig = botConfig;
     _metrics   = metrics;
     _cpu       = cpu;
     _repo      = repo;
     _shards    = shards;
     _cache     = cache;
 }
コード例 #17
0
 public WebhookExecutorService(IMetrics metrics, WebhookCacheService webhookCache, ILogger logger,
                               HttpClient client, IDiscordCache cache, DiscordApiClient rest)
 {
     _metrics      = metrics;
     _webhookCache = webhookCache;
     _client       = client;
     _cache        = cache;
     _rest         = rest;
     _logger       = logger.ForContext <WebhookExecutorService>();
 }
コード例 #18
0
 public PeriodicStatCollector(IMetrics metrics, ILogger logger, WebhookCacheService webhookCache, DbConnectionCountHolder countHolder, CpuStatService cpu, IDatabase db, IDiscordCache cache)
 {
     _metrics      = metrics;
     _webhookCache = webhookCache;
     _countHolder  = countHolder;
     _cpu          = cpu;
     _db           = db;
     _cache        = cache;
     _logger       = logger.ForContext <PeriodicStatCollector>();
 }
コード例 #19
0
 public LogChannelService(EmbedService embed, ILogger logger, IDatabase db, ModelRepository repo, IDiscordCache cache, DiscordApiClient rest, Bot bot)
 {
     _embed  = embed;
     _db     = db;
     _repo   = repo;
     _cache  = cache;
     _rest   = rest;
     _bot    = bot;
     _logger = logger.ForContext <LogChannelService>();
 }
コード例 #20
0
 public MessageEdited(LastMessageCacheService lastMessageCache, ProxyService proxy, IDatabase db, IMetrics metrics, ModelRepository repo, Cluster client, IDiscordCache cache, Bot bot)
 {
     _lastMessageCache = lastMessageCache;
     _proxy            = proxy;
     _db      = db;
     _metrics = metrics;
     _repo    = repo;
     _client  = client;
     _cache   = cache;
     _bot     = bot;
 }
コード例 #21
0
 public ReactionAdded(ILogger logger, IDatabase db, ModelRepository repo, CommandMessageService commandMessageService, IDiscordCache cache, Bot bot, DiscordApiClient rest, EmbedService embeds)
 {
     _db   = db;
     _repo = repo;
     _commandMessageService = commandMessageService;
     _cache  = cache;
     _bot    = bot;
     _rest   = rest;
     _embeds = embeds;
     _logger = logger.ForContext <ReactionAdded>();
 }
コード例 #22
0
    public static async Task <Channel> GetRootChannel(this IDiscordCache cache, ulong channelOrThread)
    {
        var channel = await cache.GetChannel(channelOrThread);

        if (!channel.IsThread())
        {
            return(channel);
        }

        var parent = await cache.GetChannel(channel.ParentId !.Value);

        return(parent);
    }
コード例 #23
0
 public MessageEdited(LastMessageCacheService lastMessageCache, ProxyService proxy, IDatabase db, IMetrics metrics, ModelRepository repo, Cluster client, IDiscordCache cache, Bot bot, DiscordApiClient rest, ILogger logger)
 {
     _lastMessageCache = lastMessageCache;
     _proxy            = proxy;
     _db      = db;
     _metrics = metrics;
     _repo    = repo;
     _client  = client;
     _cache   = cache;
     _bot     = bot;
     _rest    = rest;
     _logger  = logger.ForContext <MessageEdited>();
 }
コード例 #24
0
        public static PermissionSet PermissionsFor(this IDiscordCache cache, ulong channelId, ulong userId, ICollection <ulong>?userRoles)
        {
            var channel = cache.GetChannel(channelId);

            if (channel.GuildId == null)
            {
                return(PermissionSet.Dm);
            }

            var guild = cache.GetGuild(channel.GuildId.Value);

            return(PermissionsFor(guild, channel, userId, userRoles));
        }
コード例 #25
0
        public static async Task <Channel> GetOrCreateDmChannel(this IDiscordCache cache, DiscordApiClient rest, ulong recipientId)
        {
            if (cache.TryGetDmChannel(recipientId, out var cacheChannel))
            {
                return(cacheChannel);
            }

            var restChannel = await rest.CreateDm(recipientId);

            await cache.SaveChannel(restChannel);

            return(restChannel);
        }
コード例 #26
0
ファイル: ProxyService.cs プロジェクト: guccigang17/PluralKit
 public ProxyService(LogChannelService logChannel, ILogger logger,
                     WebhookExecutorService webhookExecutor, IDatabase db, ProxyMatcher matcher, IMetrics metrics, ModelRepository repo, IDiscordCache cache, DiscordApiClient rest)
 {
     _logChannel      = logChannel;
     _webhookExecutor = webhookExecutor;
     _db      = db;
     _matcher = matcher;
     _metrics = metrics;
     _repo    = repo;
     _cache   = cache;
     _rest    = rest;
     _logger  = logger.ForContext <ProxyService>();
 }
コード例 #27
0
ファイル: Misc.cs プロジェクト: guccigang17/PluralKit
 public Misc(BotConfig botConfig, IMetrics metrics, CpuStatService cpu, ShardInfoService shards, EmbedService embeds, ModelRepository repo, IDatabase db, IDiscordCache cache, DiscordApiClient rest, Bot bot, Cluster cluster)
 {
     _botConfig = botConfig;
     _metrics   = metrics;
     _cpu       = cpu;
     _shards    = shards;
     _embeds    = embeds;
     _repo      = repo;
     _db        = db;
     _cache     = cache;
     _rest      = rest;
     _bot       = bot;
     _cluster   = cluster;
 }
コード例 #28
0
 public PeriodicStatCollector(IMetrics metrics, ILogger logger, WebhookCacheService webhookCache,
                              DbConnectionCountHolder countHolder, CpuStatService cpu, ModelRepository repo,
                              BotConfig botConfig, CoreConfig config, RedisService redis, IDiscordCache cache)
 {
     _metrics      = metrics;
     _webhookCache = webhookCache;
     _countHolder  = countHolder;
     _cpu          = cpu;
     _repo         = repo;
     _cache        = cache;
     _botConfig    = botConfig;
     _config       = config;
     _redis        = redis;
     _logger       = logger.ForContext <PeriodicStatCollector>();
 }
コード例 #29
0
        public static async ValueTask <Channel?> GetOrFetchChannel(this IDiscordCache cache, DiscordApiClient rest, ulong channelId)
        {
            if (cache.TryGetChannel(channelId, out var cacheChannel))
            {
                return(cacheChannel);
            }

            var restChannel = await rest.GetChannel(channelId);

            if (restChannel != null)
            {
                await cache.SaveChannel(restChannel);
            }
            return(restChannel);
        }
コード例 #30
0
        public static async ValueTask <User?> GetOrFetchUser(this IDiscordCache cache, DiscordApiClient rest, ulong userId)
        {
            if (cache.TryGetUser(userId, out var cacheUser))
            {
                return(cacheUser);
            }

            var restUser = await rest.GetUser(userId);

            if (restUser != null)
            {
                await cache.SaveUser(restUser);
            }
            return(restUser);
        }