コード例 #1
0
        public async Task <DiscordGuildMemberPacket> GetGuildUserAsync(ulong userId, ulong guildId)
        {
            string key = $"discord:guild:{guildId}:user:{userId}";

            DiscordGuildMemberPacket packet = null;

            if (await cache.ExistsAsync(key))
            {
                packet = await cache.GetAsync <DiscordGuildMemberPacket>(key);
            }
            else
            {
                var rc = await RatelimitHelper.ProcessRateLimitedAsync(
                    $"guilds:{guildId}", cache,
                    async() =>
                {
                    return(await RestClient.GetAsync <DiscordGuildMemberPacket>($"/guilds/{guildId}/members/{userId}"));
                });

                packet = rc.Data;
                await cache.AddAsync(key, rc.Data);
            }

            packet.User = await GetUserAsync(userId);

            packet.GuildId = guildId;
            packet.UserId  = userId;

            return(packet);
        }
コード例 #2
0
ファイル: DiscordApiClient.cs プロジェクト: wingertge/Miki
        public async Task <DiscordGuildPacket> GetGuildAsync(ulong guildId)
        {
            string key         = $"discord:guild:{guildId}";
            var    cacheClient = cache.Get;

            if (await cacheClient.ExistsAsync(key))
            {
                var packet = await cacheClient.GetAsync <DiscordGuildPacket>(key);

                if (packet == null)
                {
                    Log.Debug($"cache hit on '{key}', but object was invalid");
                    await cacheClient.RemoveAsync(key);

                    return(await GetGuildAsync(guildId));
                }
                return(packet);
            }
            else
            {
                var data = await RatelimitHelper.ProcessRateLimitedAsync(
                    $"guilds:{guildId}", cacheClient,
                    async() =>
                {
                    return(await RestClient.GetAsync <DiscordGuildPacket>($"/guilds/{guildId}"));
                });

                await cacheClient.UpsertAsync(key, data.Data);

                return(data.Data);
            }
        }
コード例 #3
0
 public async Task RemoveGuildMemberAsync(ulong guildId, ulong userId)
 {
     await RatelimitHelper.ProcessRateLimitedAsync(
         $"guilds:{guildId}", cache,
         async() => await RestClient.DeleteAsync($"/guilds/{guildId}/members/{userId}")
         );
 }
コード例 #4
0
ファイル: DiscordApiClient.cs プロジェクト: wingertge/Miki
        public async Task <DiscordChannelPacket> GetChannelAsync(ulong channelId)
        {
            string key = $"discord:channel:{channelId}";
            DiscordChannelPacket packet = null;

            var cacheClient = cache.Get;

            if (await cacheClient.ExistsAsync(key))
            {
                packet = await cacheClient.GetAsync <DiscordChannelPacket>(key);

                if (packet == null)
                {
                    Log.Debug($"cache hit on '{key}', but object was invalid");
                    await cacheClient.RemoveAsync(key);

                    return(await GetChannelAsync(channelId));
                }
            }
            else
            {
                var data = await RatelimitHelper.ProcessRateLimitedAsync(
                    $"channels:{channelId}", cache.Get,
                    async() => await RestClient.GetAsync <DiscordChannelPacket>($"/channels/{channelId}")
                    );

                await cacheClient.UpsertAsync(key, data.Data);

                packet = data.Data;
            }
            return(packet);
        }
コード例 #5
0
        public async Task <DiscordChannelPacket> GetChannelAsync(ulong channelId)
        {
            string key = $"discord:channel:{channelId}";
            DiscordChannelPacket packet = null;

            if (await cache.ExistsAsync(key))
            {
                packet = await cache.GetAsync <DiscordChannelPacket>(key);

                if (packet == null)
                {
                    Console.WriteLine("CACHE HIT -- null, removed");
                    await cache.RemoveAsync(key);

                    return(await GetChannelAsync(channelId));
                }
            }
            else
            {
                var data = await RatelimitHelper.ProcessRateLimitedAsync(
                    $"channels:{channelId}", cache,
                    async() => await RestClient.GetAsync <DiscordChannelPacket>($"/channels/{channelId}")
                    );

                await cache.AddAsync(key, data.Data);

                packet = data.Data;
            }

            return(packet);
        }
コード例 #6
0
 public async Task DeleteMessageAsync(ulong channelId, ulong messageId)
 {
     await RatelimitHelper.ProcessRateLimitedAsync(
         $"channels:{channelId}:delete", cache,
         async() =>
     {
         return(await RestClient.DeleteAsync($"/channels/{channelId}/messages/{messageId}"));
     });
 }
コード例 #7
0
 public async Task AddGuildMemberRoleAsync(ulong guildId, ulong userId, ulong roleId)
 {
     await RatelimitHelper.ProcessRateLimitedAsync(
         $"guilds:{guildId}", cache,
         async() =>
     {
         return(await RestClient.PutAsync($"/guilds/{guildId}/members/{userId}/roles/{roleId}"));
     });
 }
コード例 #8
0
 public async Task <DiscordRolePacket> EditRoleAsync(ulong guildId, DiscordRolePacket role)
 {
     return((await RatelimitHelper.ProcessRateLimitedAsync(
                 $"guilds:{guildId}", cache,
                 async() => await RestClient.PutAsync <DiscordRolePacket>(
                     $"/guilds/{guildId}/roles/{role.Id}",
                     JsonConvert.SerializeObject(role)
                     )
                 )).Data);
 }
コード例 #9
0
 public async Task RemoveGuildMemberRoleAsync(ulong guildId, ulong userId, ulong roleId)
 {
     await RatelimitHelper.ProcessRateLimitedAsync(
         $"guilds:{guildId}", cache,
         async() =>
     {
         var rc = await RestClient.DeleteAsync($"/guilds/{guildId}/members/{userId}/roles/{roleId}");
         return(rc);
     });
 }
コード例 #10
0
ファイル: DiscordApiClient.cs プロジェクト: wingertge/Miki
 public async Task RemoveGuildBanAsync(ulong guildId, ulong userId)
 {
     var cacheClient = cache.Get;
     {
         await RatelimitHelper.ProcessRateLimitedAsync(
             $"guilds:{guildId}", cacheClient,
             async() => await RestClient.DeleteAsync($"/guilds/{guildId}/bans/{userId}")
             );
     }
 }
コード例 #11
0
ファイル: DiscordApiClient.cs プロジェクト: wingertge/Miki
        public async Task <DiscordMessagePacket> SendFileAsync(ulong channelId, Stream stream, string fileName, MessageArgs args, bool toChannel = true)
        {
            args.embed = new DiscordEmbed
            {
                Image = new EmbedImage
                {
                    Url = "attachment://" + fileName
                }
            };

            string json = JsonConvert.SerializeObject(args, serializer);

            List <MultiformItem> items = new List <MultiformItem>();

            var content = new StringContent(args.content);

            items.Add(new MultiformItem {
                Name = "content", Content = content
            });

            if (stream.CanSeek)
            {
                var memoryStream = new MemoryStream();
                await stream.CopyToAsync(memoryStream).ConfigureAwait(false);

                memoryStream.Position = 0;
                stream = memoryStream;
            }

            var image = new StreamContent(stream);

            items.Add(new MultiformItem {
                Name = "file", Content = image, FileName = fileName
            });
            image.Headers.Add("Content-Type", "image/png");

            image.Headers.Add("Content-Disposition", "form-data; name=\"file\"; filename=\"" + fileName + "\"");

            var cacheClient = cache.Get;

            {
                await RatelimitHelper.ProcessRateLimitedAsync(
                    $"channels:{channelId}",
                    cacheClient, async() =>
                {
                    RestResponse rc = await RestClient
                                      .PostMultipartAsync($"/channels/{channelId}/messages",
                                                          items.ToArray()
                                                          );
                    return(rc);
                });
            }
            // TODO: fix returns
            return(null);
        }
コード例 #12
0
 public async Task ModifyGuildMemberAsync(ulong guildId, ulong userId, ModifyGuildMemberArgs packet)
 {
     await RatelimitHelper.ProcessRateLimitedAsync(
         $"guilds:{guildId}", cache,
         async() =>
     {
         return(await RestClient.PatchAsync($"/guilds/{guildId}/members/{userId}",
                                            JsonConvert.SerializeObject(packet, serializer)
                                            ));
     });
 }
コード例 #13
0
 public async Task <DiscordRolePacket> CreateGuildRoleAsync(ulong guildId, CreateRoleArgs args)
 {
     return((await RatelimitHelper.ProcessRateLimitedAsync(
                 $"guilds:{guildId}", cache,
                 async() =>
     {
         return await RestClient.PostAsync <DiscordRolePacket>(
             $"/guilds/{guildId}/roles",
             JsonConvert.SerializeObject(args) ?? ""
             );
     })).Data);
 }
コード例 #14
0
        public async Task <DiscordMessagePacket> SendMessageAsync(ulong channelId, MessageArgs args, bool toChannel = true)
        {
            string json = JsonConvert.SerializeObject(args, new JsonSerializerSettings()
            {
                NullValueHandling = NullValueHandling.Ignore
            });

            return((await RatelimitHelper.ProcessRateLimitedAsync(
                        $"channels:{channelId}",
                        cache, async() => {
                return await RestClient.PostAsync <DiscordMessagePacket>($"/channels/{channelId}/messages", json);
            })).Data);
        }
コード例 #15
0
 public async Task <DiscordMessagePacket> EditMessageAsync(ulong channelId, ulong messageId, EditMessageArgs args)
 {
     return((await RatelimitHelper.ProcessRateLimitedAsync(
                 $"channels:{channelId}", cache,
                 async() =>
     {
         return await RestClient
         .PatchAsync <DiscordMessagePacket>($"/channels/{channelId}/messages/{messageId}", JsonConvert.SerializeObject(args, new JsonSerializerSettings()
         {
             NullValueHandling = NullValueHandling.Ignore
         }));
     })).Data);
 }
コード例 #16
0
        public async Task AddGuildBanAsync(ulong guildId, ulong userId, int pruneDays = 7, string reason = null)
        {
            QueryString qs = new QueryString();

            if (!string.IsNullOrWhiteSpace(reason))
            {
                qs.Add("reason", reason);
            }

            if (pruneDays != 0)
            {
                qs.Add("delete-message-days", pruneDays);
            }

            await RatelimitHelper.ProcessRateLimitedAsync(
                $"guilds:{guildId}", cache,
                async() =>
            {
                return(await RestClient.PutAsync($"/guilds/{guildId}/bans/{userId}" + qs.Query));
            });
        }
コード例 #17
0
ファイル: DiscordApiClient.cs プロジェクト: wingertge/Miki
        public async Task <DiscordGuildMemberPacket> GetGuildUserAsync(ulong userId, ulong guildId)
        {
            string key = $"discord:guild:{guildId}:user:{userId}";

            DiscordGuildMemberPacket packet = null;
            var cacheClient = cache.Get;

            {
                if (await cacheClient.ExistsAsync(key))
                {
                    packet = await cacheClient.GetAsync <DiscordGuildMemberPacket>(key);

                    if (packet == null)
                    {
                        Log.Debug($"cache hit on '{key}', but object was invalid");
                        await cacheClient.RemoveAsync(key);

                        return(await GetGuildUserAsync(userId, guildId));
                    }
                }
                else
                {
                    var rc = await RatelimitHelper.ProcessRateLimitedAsync(
                        $"guilds:{guildId}", cacheClient,
                        async() =>
                    {
                        return(await RestClient.GetAsync <DiscordGuildMemberPacket>($"/guilds/{guildId}/members/{userId}"));
                    });

                    packet = rc.Data;
                    await cacheClient.UpsertAsync(key, rc.Data);
                }

                packet.User = await GetUserAsync(userId);

                packet.GuildId = guildId;
                packet.UserId  = userId;
            }
            return(packet);
        }
コード例 #18
0
        public async Task <DiscordGuildPacket> GetGuildAsync(ulong guildId)
        {
            string key = $"discord:guild:{guildId}";

            if (await cache.ExistsAsync(key))
            {
                return(await cache.GetAsync <DiscordGuildPacket>(key));
            }
            else
            {
                var data = await RatelimitHelper.ProcessRateLimitedAsync(
                    $"guilds:{guildId}", cache,
                    async() =>
                {
                    return(await RestClient.GetAsync <DiscordGuildPacket>($"/guilds/{guildId}"));
                });

                await cache.AddAsync(key, data.Data);

                return(data.Data);
            }
        }
コード例 #19
0
        public async Task <List <DiscordChannelPacket> > GetChannelsAsync(ulong guildId)
        {
            string key = $"discord:guilds:{guildId}:channels";
            List <DiscordChannelPacket> packet = null;

            if (await cache.ExistsAsync(key))
            {
                packet = await cache.GetAsync <List <DiscordChannelPacket> >(key);
            }
            else
            {
                var data = await RatelimitHelper.ProcessRateLimitedAsync(
                    $"guilds:{guildId}", cache,
                    async() =>
                {
                    return(await RestClient.GetAsync <List <DiscordChannelPacket> >($"/guild/{guildId}/channels"));
                });

                await cache.AddAsync(key, data.Data);

                packet = data.Data;
            }
            return(packet);
        }