Example #1
0
        private async Task <Option <AfkCacheItem> > GetUserAfkThroughCache(ulong userId)
        {
            // This user was checked and didnt have AFK so we return that. This is to stop the DB being queried for
            // every single mention that didnt have an AFK set.
            if (_cacheService.Contains(CacheId.GetAfkCheckId(userId)))
            {
                return(Option.None <AfkCacheItem>());
            }

            bool inCache = true;
            var  cached  = await _cacheService.TryGetOrSetAndGetAsync <Data.Models.SoraDb.Afk>(
                CacheId.GetAfkId(userId),
                async() =>
            {
                var afk = await _afkRepo.GetUserAfk(userId).ConfigureAwait(false);
                inCache = false;
                return(!afk ? null : afk.Some());
            }, AfkTtl);

            if (!cached)
            {
                // TODO this is not clean at all and pretty bad practice. I just can't be bothered rn to write some form of hasmap to store the checks since i would need to make this singleton.
                _cacheService.Set(CacheId.GetAfkCheckId(userId), new object(), AfkTtl);
                return(Option.None <AfkCacheItem>());
            }

            return(new Option <AfkCacheItem>(new AfkCacheItem(cached.Some(), inCache)));
        }
Example #2
0
        public async Task SetOrToggleAfk([Remainder, Summary("Custom status.")] string status = null)
        {
            if (status == null)
            {
                // We now toggle the status
                var curr = await _afkRepo.GetUserAfk(Context.User.Id);

                if (!curr)
                {
                    // No current afk status set so we enable it
                    await _afkRepo.SetUserAfk(Context.User.Id, null);
                    await ReplySuccessEmbed("Successfully set AFK status");

                    // Cleanup cache
                    _cache.TryRemove(CacheId.GetAfkCheckId(Context.User.Id));

                    return;
                }
                // Otherwise we have a status and we should remove it
                await _afkRepo.RemoveUserAfk(Context.User.Id);
                await ReplySuccessEmbed("Successfully removed AFK status");

                // Cleanup cache
                _cache.TryRemove(CacheId.GetAfkId(Context.User.Id));

                return;
            }
            // Check if status is too long
            if (status.Length > 256)
            {
                await ReplyFailureEmbed("Make sure your custom AFK status is shorter than 256 characters!");

                return;
            }
            // Set the new custom status
            await _afkRepo.SetUserAfk(Context.User.Id, status);

            await ReplySuccessEmbed("Successfully set custom AFK status");

            // Cleanup cache so on new mention the text is updated from the DB
            _cache.TryRemove(CacheId.GetAfkId(Context.User.Id));
            _cache.TryRemove(CacheId.GetAfkCheckId(Context.User.Id));
        }