예제 #1
0
        /// <summary>
        /// Returns the embed for the specified guild.
        /// <para>Requires <see cref="DiscordPermission.ManageGuild"/>.</para>
        /// </summary>
        /// <exception cref="DiscordHttpApiException"></exception>
        public async Task <DiscordGuildEmbed> GetGuildEmbed(Snowflake guildId)
        {
            DiscordApiData data = await rest.Get($"guilds/{guildId}/embed",
                                                 $"guilds/{guildId}/embed").ConfigureAwait(false);

            return(new DiscordGuildEmbed(this, guildId, data));
        }
예제 #2
0
        /// <summary>
        /// Gets a webhook via its ID.
        /// </summary>
        /// <exception cref="DiscordHttpApiException"></exception>
        public async Task <DiscordWebhook> GetWebhook(Snowflake webhookId)
        {
            DiscordApiData apiData = await rest.Get($"webhooks/{webhookId}",
                                                    $"webhooks/{webhookId}").ConfigureAwait(false);

            return(new DiscordWebhook(this, apiData));
        }
예제 #3
0
        /// <summary>
        /// Modifies an existing webhook.
        /// </summary>
        /// <exception cref="ArgumentException">Thrown if the token is empty or only contains whitespace characters.</exception>
        /// <exception cref="ArgumentNullException">Thrown if token is null.</exception>
        /// <exception cref="DiscordHttpApiException"></exception>
        public async Task ModifyWebhookWithToken(Snowflake webhookId, string token,
                                                 string name = null, DiscordImageData avatar = null)
        {
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }
            if (string.IsNullOrWhiteSpace(token))
            {
                throw new ArgumentException("Token cannot be empty or only contain whitespace characters.", nameof(token));
            }

            DiscordApiData postData = DiscordApiData.CreateContainer();

            if (name != null)
            {
                postData.Set("name", name);
            }
            if (avatar != null)
            {
                postData.Set("avatar", avatar);
            }

            await rest.Patch($"webhooks/{webhookId}/{token}", postData,
                             $"webhooks/{webhookId}/token").ConfigureAwait(false);
        }
        internal DiscordApiData Build()
        {
            DiscordApiData data = new DiscordApiData(DiscordApiDataType.Container);

            if (Name != null)
            {
                data.Set("name", Name);
            }
            if (Position.HasValue)
            {
                data.Set("position", Position.Value);
            }

            if (PermissionOverwrites != null)
            {
                DiscordApiData permissionOverwritesArray = new DiscordApiData(DiscordApiDataType.Array);
                foreach (OverwriteOptions overwriteParam in PermissionOverwrites)
                {
                    permissionOverwritesArray.Values.Add(overwriteParam.Build());
                }

                data.Set("permission_overwrites", permissionOverwritesArray);
            }

            return(data);
        }
예제 #5
0
        /// <summary>
        /// Returns the number of members that would be kicked from a guild prune operation.
        /// <para>Requires <see cref="DiscordPermission.KickMembers"/>.</para>
        /// </summary>
        /// <param name="days">The number of days to count prune for (1 or more).</param>
        /// <exception cref="DiscordHttpApiException"></exception>
        public async Task <int> GetGuildPruneCount(Snowflake guildId, int days)
        {
            DiscordApiData data = await rest.Get($"guilds/{guildId}/prune?days={days}",
                                                 $"guilds/{guildId}/prune").ConfigureAwait(false);

            return(data.GetInteger("pruned").Value);
        }
예제 #6
0
        void HandleChannelDeleteEvent(DiscordApiData data)
        {
            Snowflake          id   = data.GetSnowflake("id").Value;
            DiscordChannelType type = (DiscordChannelType)data.GetInteger("type").Value;

            if (type == DiscordChannelType.DirectMessage)
            {
                // DM channel
                DiscordDMChannel dm;
                if (cache.DMChannels.TryRemove(id, out MutableDMChannel mutableDM))
                {
                    mutableDM.ClearReferences();

                    dm = mutableDM.ImmutableEntity;
                }
                else
                {
                    dm = new DiscordDMChannel(http, data);
                }

                OnDMChannelRemoved?.Invoke(this, new DMChannelEventArgs(shard, dm));
            }
            else if (type == DiscordChannelType.GuildText || type == DiscordChannelType.GuildVoice ||
                     type == DiscordChannelType.GuildCategory)
            {
                // Guild channel
                Snowflake guildId = data.GetSnowflake("guild_id").Value;

                DiscordGuildChannel channel;

                if (type == DiscordChannelType.GuildText)
                {
                    if (!cache.GuildChannels.TryRemove(id, out channel))
                    {
                        channel = new DiscordGuildTextChannel(http, data, guildId);
                    }
                }
                else if (type == DiscordChannelType.GuildVoice)
                {
                    if (!cache.GuildChannels.TryRemove(id, out channel))
                    {
                        channel = new DiscordGuildVoiceChannel(http, data, guildId);
                    }
                }
                else if (type == DiscordChannelType.GuildCategory)
                {
                    if (!cache.GuildChannels.TryRemove(id, out channel))
                    {
                        channel = new DiscordGuildCategoryChannel(http, data, guildId);
                    }
                }
                else
                {
                    throw new NotImplementedException($"Guild channel type \"{type}\" has no implementation!");
                }

                OnGuildChannelRemoved?.Invoke(this, new GuildChannelEventArgs(shard, guildId, channel));
            }
        }
예제 #7
0
        void HandleSpeaking(DiscordApiData payload, DiscordApiData data)
        {
            Snowflake userId     = data.GetSnowflake("user_id").Value;
            int       ssrc       = data.GetInteger("ssrc").Value;
            bool      isSpeaking = data.GetBoolean("speaking").Value;

            OnUserSpeaking?.Invoke(this, new VoiceSpeakingEventArgs(userId, ssrc, isSpeaking));
        }
예제 #8
0
        void HandleResumedEvent(DiscordApiData data)
        {
            // Signal that the connection is ready
            handshakeCompleteEvent.Set();

            log.LogInfo("[Resumed] Successfully resumed.");
            LogServerTrace("Resumed", data);
        }
예제 #9
0
        internal override DiscordApiData Build()
        {
            DiscordApiData data = base.Build();

            data.SetSnowflake("id", TemporaryId);

            return(data);
        }
예제 #10
0
 internal DiscordMessageApplication(DiscordApiData data)
     : base(data)
 {
     CoverImage  = data.GetString("cover_image");
     Description = data.GetString("description");
     Icon        = data.GetString("icon");
     Name        = data.GetString("name");
 }
예제 #11
0
        internal DiscordApiData Build()
        {
            DiscordApiData data = new DiscordApiData(DiscordApiDataType.Container);
            data.Set("enabled", Enabled);
            data.SetSnowflake("channel_id", ChannelId);

            return data;
        }
예제 #12
0
        void HandleTypingStartEvent(DiscordApiData data)
        {
            Snowflake userId    = data.GetSnowflake("user_id").Value;
            Snowflake channelId = data.GetSnowflake("channel_id").Value;
            int       timestamp = data.GetInteger("timestamp").Value;

            OnTypingStarted?.Invoke(this, new TypingStartEventArgs(shard, userId, channelId, timestamp));
        }
예제 #13
0
        void HandleChannelCreateEvent(DiscordApiData data)
        {
            Snowflake          id   = data.GetSnowflake("id").Value;
            DiscordChannelType type = (DiscordChannelType)data.GetInteger("type").Value;

            if (type == DiscordChannelType.DirectMessage)
            {
                // DM channel
                DiscordApiData recipientData = data.GetArray("recipients").First();
                Snowflake      recipientId   = recipientData.GetSnowflake("id").Value;

                MutableUser recipient;
                if (!cache.Users.TryGetValue(recipientId, out recipient))
                {
                    recipient = new MutableUser(recipientId, false, http);
                    cache.Users[recipientId] = recipient;
                }

                recipient.Update(recipientData);

                MutableDMChannel mutableDMChannel;
                if (!cache.DMChannels.TryGetValue(id, out mutableDMChannel))
                {
                    mutableDMChannel     = new MutableDMChannel(id, recipient, http);
                    cache.DMChannels[id] = mutableDMChannel;
                }

                OnDMChannelCreated?.Invoke(this, new DMChannelEventArgs(shard, mutableDMChannel.ImmutableEntity));
            }
            else if (type == DiscordChannelType.GuildText || type == DiscordChannelType.GuildVoice)
            {
                // Guild channel
                Snowflake guildId = data.GetSnowflake("guild_id").Value;

                DiscordGuildChannel channel;

                if (type == DiscordChannelType.GuildText)
                {
                    channel = new DiscordGuildTextChannel(http, data, guildId);
                }
                else if (type == DiscordChannelType.GuildVoice)
                {
                    channel = new DiscordGuildVoiceChannel(http, data, guildId);
                }
                else if (type == DiscordChannelType.GuildCategory)
                {
                    channel = new DiscordGuildCategoryChannel(http, data, guildId);
                }
                else
                {
                    throw new NotImplementedException($"Guild channel type \"{type}\" has no implementation!");
                }

                cache.GuildChannels[id] = channel;

                OnGuildChannelCreated?.Invoke(this, new GuildChannelEventArgs(shard, guildId, channel));
            }
        }
예제 #14
0
        internal DiscordApiData Build()
        {
            DiscordApiData data = new DiscordApiData(DiscordApiDataType.Container);

            data.Set("name", Name);
            data.Set("image", Image.ToDataUriScheme());

            return(data);
        }
예제 #15
0
        /// <summary>
        /// Creates a new guild.
        /// </summary>
        /// <exception cref="DiscordHttpApiException"></exception>
        public async Task <DiscordGuild> CreateGuild(CreateGuildOptions options)
        {
            DiscordApiData requestdata = options.Build();

            DiscordApiData returnData = await rest.Post("guilds", requestdata,
                                                        "guilds").ConfigureAwait(false);

            return(new DiscordGuild(this, returnData));
        }
예제 #16
0
        internal DiscordApiData Build()
        {
            DiscordApiData data = new DiscordApiData(DiscordApiDataType.Container);

            data.SetSnowflake("id", Id);
            data.Set("position", Position);

            return(data);
        }
예제 #17
0
        /// <exception cref="DiscordWebSocketException">Thrown if the payload fails to send because of a WebSocket error.</exception>
        /// <exception cref="InvalidOperationException">Thrown if the socket is not connected.</exception>
        /// <exception cref="JsonWriterException">Thrown if the given data cannot be serialized as JSON.</exception>
        Task SendPayload(VoiceOPCode op, DiscordApiData data)
        {
            DiscordApiData payload = new DiscordApiData();

            payload.Set("op", (int)op);
            payload.Set("d", data);

            return(SendAsync(payload));
        }
예제 #18
0
        /// <summary>
        /// Attaches an integration from the current bot to the specified guild.
        /// <para>Requires <see cref="DiscordPermission.ManageGuild"/>.</para>
        /// </summary>
        /// <exception cref="DiscordHttpApiException"></exception>
        public async Task CreateGuildIntegration(Snowflake guildId, Snowflake integrationId, string type)
        {
            DiscordApiData requestData = new DiscordApiData(DiscordApiDataType.Container);

            requestData.SetSnowflake("id", integrationId);
            requestData.Set("type", type);

            await rest.Post($"guilds/{guildId}/integrations",
                            $"guilds/{guildId}/integrations").ConfigureAwait(false);
        }
        /// <exception cref="DiscordWebSocketException">Thrown if the payload fails to send because of a WebSocket error.</exception>
        /// <exception cref="InvalidOperationException">Thrown if the socket is not connected.</exception>
        public Task SendRequestGuildMembersPayload(Snowflake guildId, string query, int limit)
        {
            DiscordApiData data = new DiscordApiData(DiscordApiDataType.Container);

            data.SetSnowflake("guild_id", guildId);
            data.Set("query", query);
            data.Set("limit", limit);

            return(SendPayload(GatewayOPCode.RequestGuildMembers, data));
        }
예제 #20
0
 internal DiscordAttachment(DiscordApiData data)
     : base(data)
 {
     FileName = data.GetString("filename");
     Size     = data.GetInteger("size").Value;
     Url      = data.GetString("url");
     ProxyUrl = data.GetString("proxy_url");
     Width    = data.GetInteger("width");
     Height   = data.GetInteger("height");
 }
예제 #21
0
        internal DiscordApiData Build()
        {
            DiscordApiData data = new DiscordApiData(DiscordApiDataType.Container);

            data.Set("expire_behavior", ExpireBehavior);
            data.Set("expire_grace_period", ExpireGracePeriod);
            data.Set("enable_emoticons", EnableEmoticons);

            return(data);
        }
예제 #22
0
        /// <exception cref="DiscordWebSocketException">Thrown if the payload fails to send because of a WebSocket error.</exception>
        /// <exception cref="InvalidOperationException">Thrown if the socket is not connected.</exception>
        public Task SendSpeakingPayload(bool speaking, int ssrc)
        {
            DiscordApiData data = new DiscordApiData();

            data.Set("speaking", speaking);
            data.Set("delay", value: 0);
            data.Set("ssrc", ssrc);

            return(SendPayload(VoiceOPCode.Speaking, data));
        }
        /// <summary>
        /// Opens a DM channel with the specified user.
        /// </summary>
        /// <exception cref="DiscordHttpApiException"></exception>
        public async Task <DiscordDMChannel> CreateDM(Snowflake recipientId)
        {
            DiscordApiData requestData = new DiscordApiData(DiscordApiDataType.Container);

            requestData.SetSnowflake("recipient_id", recipientId);

            DiscordApiData returnData = await rest.Post("users/@me/channels", requestData,
                                                        "users/@me/channels").ConfigureAwait(false);

            return(new DiscordDMChannel(this, returnData));
        }
        /// <exception cref="DiscordWebSocketException">Thrown if the payload fails to send because of a WebSocket error.</exception>
        /// <exception cref="InvalidOperationException">Thrown if the socket is not connected.</exception>
        public Task SendVoiceStateUpdatePayload(Snowflake guildId, Snowflake?channelId, bool isMute, bool isDeaf)
        {
            DiscordApiData data = new DiscordApiData(DiscordApiDataType.Container);

            data.SetSnowflake("guild_id", guildId);
            data.SetSnowflake("channel_id", channelId);
            data.Set("self_mute", isMute);
            data.Set("self_deaf", isDeaf);

            return(SendPayload(GatewayOPCode.VoiceStateUpdate, data));
        }
예제 #25
0
        /// <summary>
        /// Modifies the current bot's nickname in the specified guild.
        /// <para>Requires <see cref="DiscordPermission.ChangeNickname"/>.</para>
        /// </summary>
        /// <param name="nickname">The new nickname (or null or an empty string to remove nickname).</param>
        /// <returns>Returns the new nickname (or null if the nickname was removed).</returns>
        /// <exception cref="DiscordHttpApiException"></exception>
        public async Task <string> ModifyCurrentUsersNickname(Snowflake guildId, string nickname)
        {
            DiscordApiData requestData = new DiscordApiData();

            requestData.Set("nick", nickname);

            DiscordApiData returnData = await rest.Patch($"guilds/{guildId}/members/@me/nick", requestData,
                                                         $"guilds/{guildId}/members/@me/nick").ConfigureAwait(false);

            return(returnData.GetString("nick"));
        }
예제 #26
0
        /// <summary>
        /// Edits an existing message in a text channel.
        /// <para>Note: only messages created by the current bot can be edited.</para>
        /// </summary>
        /// <exception cref="DiscordHttpApiException"></exception>
        public async Task <DiscordMessage> EditMessage(Snowflake channelId, Snowflake messageId, string content)
        {
            DiscordApiData requestData = new DiscordApiData(DiscordApiDataType.Container);

            requestData.Set("content", content);

            DiscordApiData returnData = await rest.Patch($"channels/{channelId}/messages/{messageId}", requestData,
                                                         $"channels/{channelId}/messages/message").ConfigureAwait(false);

            return(new DiscordMessage(this, returnData));
        }
예제 #27
0
        void HandleMessageReactionRemoveEvent(DiscordApiData data)
        {
            Snowflake      userId    = data.GetSnowflake("user_id").Value;
            Snowflake      channelId = data.GetSnowflake("channel_id").Value;
            Snowflake      messageId = data.GetSnowflake("message_id").Value;
            DiscordApiData emojiData = data.Get("emoji");

            DiscordReactionEmoji emoji = new DiscordReactionEmoji(emojiData);

            OnMessageReactionRemoved?.Invoke(this, new MessageReactionEventArgs(shard, messageId, channelId, userId, emoji));
        }
예제 #28
0
        public void Update(DiscordApiData data)
        {
            Nickname = data.GetString("nick");
            JoinedAt = data.GetDateTime("joined_at").Value;
            IsDeaf   = data.GetBoolean("deaf") ?? false;
            IsMute   = data.GetBoolean("mute") ?? false;

            UpdateRoles(data);

            Dirty();
        }
예제 #29
0
        public void PartialUpdate(DiscordApiData data)
        {
            Nickname = data.GetString("nick") ?? Nickname;

            if (data.ContainsKey("roles"))
            {
                UpdateRoles(data);
            }

            Dirty();
        }
예제 #30
0
        /// <exception cref="DiscordWebSocketException">Thrown if the payload fails to send because of a WebSocket error.</exception>
        /// <exception cref="InvalidOperationException">Thrown if the socket is not connected.</exception>
        public Task SendResumePayload(Snowflake serverId, string sessionId, string token)
        {
            DiscordApiData data = new DiscordApiData();

            data.SetSnowflake("server_id", serverId);
            data.Set("session_id", sessionId);
            data.Set("token", token);

            log.LogVerbose("[Resume] Sending payload...");
            return(SendPayload(VoiceOPCode.Resume, data));
        }