/// <summary> /// Modify a guild channel. /// </summary> /// <param name="config">Configuration file containing optional values to change.</param> public Channel ModifyChannel(TokenResponse tokenResponse, string channelId, ChannelConfig config) { Channel channel = new Channel(); using (WebClient client = GetWebClient(tokenResponse, ContentType.Json)) { if (config != null && config.IsValid()) { try { IDiscordUriBuilder uriBuilder = new DiscordUriBuilder(); string uri = uriBuilder.AddPath(config.PathUrl.Replace("{channel}", channelId)) .Build(); string json = Serializer.Serialize(config); string response = client.UploadString(uri, "PATCH", json); channel = Serializer.Deserialize <Channel>(response); } catch (Exception) { } } } return(channel); }
/// <summary> /// Get OAuth2 access token. /// </summary> /// <param name="code">Code received from Discord in exchange for the client Id.</param> /// <param name="redirectUri">Redirect Uri configured on a Discord application.</param> public TokenResponse GetAccessToken(string clientId, string clientSecret, string code, string redirectUri) { TokenResponse tokenResponse = new TokenResponse(); using (WebClient client = new WebClient()) { client.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded"); try { IDiscordUriBuilder uriBuilder = new DiscordUriBuilder(); string uri = uriBuilder.AddPath(tokenResponse) .AddEndOfPath() .AddParameter("client_id", clientId, true) .AddParameter("client_secret", clientSecret, true) .AddParameter("grant_type", GrantType.AuthorizationCode, true) .AddParameter("code", code, true) .AddParameter("redirect_uri", redirectUri, false) .Build(); string response = client.UploadString(uri, "POST", ""); tokenResponse = deserializer.Deserialize <TokenResponse>(response); } catch (Exception) { } } return(tokenResponse); }
/// <summary> /// Create a reaction for a message. /// </summary> public bool CreateReaction(TokenResponse tokenResponse, string channelId, string messageId, string emoji) { using (WebClient client = GetWebClient(tokenResponse)) { try { IDiscordUriBuilder uriBuilder = new DiscordUriBuilder(); string uri = uriBuilder.AddPath($"channels/{channelId}/messages/{messageId}/reactions/{emoji}/@me") .Build(); string response = client.UploadString(uri, "PUT", ""); return(true); } catch (Exception) { } } return(false); }
/// <summary> /// Delete a message from a channel. /// </summary> public bool DeleteMessage(TokenResponse tokenResponse, string channelId, string messageId) { using (WebClient client = GetWebClient(tokenResponse)) { try { IDiscordUriBuilder uriBuilder = new DiscordUriBuilder(); string uri = uriBuilder.AddPath($"channels/{channelId}/messages/{messageId}") .Build(); string response = client.UploadString(uri, "DELETE", ""); return(true); } catch (Exception) { } } return(false); }
/// <summary> /// Post a message in a channel. /// </summary> public bool CreateMessage(TokenResponse tokenResponse, string channelId, string message) { using (WebClient client = GetWebClient(tokenResponse, ContentType.Json)) { try { IDiscordUriBuilder uriBuilder = new DiscordUriBuilder(); string uri = uriBuilder.AddPath($"channels/{channelId}/messages") .Build(); string json = $"{{ \"content\":\"{message}\" }}"; string response = client.UploadString(uri, "POST", json); return(true); } catch (Exception) { } } return(false); }
/// <summary> /// Get a specific message from a channel. /// </summary> public Message GetMessage(TokenResponse tokenResponse, string channelId, string messageId) { Message message = new Message(); using (WebClient client = GetWebClient(tokenResponse)) { try { IDiscordUriBuilder uriBuilder = new DiscordUriBuilder(); string uri = uriBuilder.AddPath(message.PathUrl.Replace("{channel}", channelId).Replace("{message}", messageId)) .Build(); string response = client.DownloadString(uri); message = Serializer.Deserialize <Message>(response); } catch (Exception) { } } return(message); }
/// <summary> /// Post a message through a Webhook. /// </summary> public bool CreateMessage(string webhookId, string webhookToken, string message) { using (WebClient client = new WebClient()) { client.Headers.Add(HttpRequestHeader.ContentType, "application/json"); try { IDiscordUriBuilder uriBuilder = new DiscordUriBuilder(); string uri = uriBuilder.AddPath($"webhooks/{webhookId}/{webhookToken}") .Build(); string json = $"{{ \"content\":\"{message}\" }}"; string response = client.UploadString(uri, "POST", json); return(true); } catch (Exception) { } } return(false); }
public Channel GetChannel(TokenResponse tokenResponse, string channelId) { Channel channel = new Channel(); using (WebClient client = GetWebClient(tokenResponse)) { try { IDiscordUriBuilder uriBuilder = new DiscordUriBuilder(); string uri = uriBuilder.AddPath(channel.PathUrl.Replace("{channel}", channelId)) .Build(); string response = client.DownloadString(uri); channel = Serializer.Deserialize <Channel>(response); } catch (Exception) { } } return(channel); }
public GuildRoles GetRoles(TokenResponse tokenResponse, string guildId) { GuildRoles roles = new GuildRoles(); using (WebClient client = new WebClient()) { client.Headers.Add(HttpRequestHeader.Authorization, tokenResponse.Type + " " + tokenResponse.AccessToken); try { IDiscordUriBuilder uriBuilder = new DiscordUriBuilder(); string uri = uriBuilder.AddPath(roles.PathUrl.Replace("{guild}", guildId)) .Build(); string response = client.DownloadString(uri); roles = serializer.Deserialize <GuildRoles>(response); } catch (Exception) { } } return(roles); }
public DiscordUser GetCurrentUser(TokenResponse tokenResponse) { DiscordUser user = new DiscordUser(); using (WebClient client = new WebClient()) { client.Headers.Add(HttpRequestHeader.Authorization, tokenResponse.Type + " " + tokenResponse.AccessToken); try { IDiscordUriBuilder uriBuilder = new DiscordUriBuilder(); string uri = uriBuilder.AddPath(user) .Build(); string response = client.DownloadString(uri); user = deserializer.Deserialize <DiscordUser>(response); } catch (Exception) { } } return(user); }
/// <summary> /// Delete a channel. /// </summary> public bool DeleteChannel(TokenResponse tokenResponse, string channelId) { using (WebClient client = GetWebClient(tokenResponse)) { try { IDiscordUriBuilder uriBuilder = new DiscordUriBuilder(); string uri = uriBuilder.AddPath($"channels/{channelId}") .Build(); string response = client.UploadString(uri, "DELETE", ""); if (!string.IsNullOrEmpty(response)) { return(true); } } catch (Exception) { } } return(false); }
public GatewayBot GetGatewayBot(TokenResponse tokenResponse) { GatewayBot bot = new GatewayBot(); using (WebClient client = new WebClient()) { client.Headers.Add(HttpRequestHeader.Authorization, tokenResponse.Type + " " + tokenResponse.AccessToken); try { IDiscordUriBuilder uriBuilder = new DiscordUriBuilder(); string uri = uriBuilder.AddPath(bot) .Build(); string response = client.DownloadString(uri); bot = deserializer.Deserialize <GatewayBot>(response); } catch (Exception) { } } return(bot); }
/// <summary> /// Return a list of Guild Members. /// </summary> /// <param name="limit">Max number of members to return (1-1000)</param> /// <returns></returns> public GuildMembers GetMembers(TokenResponse tokenResponse, string guildId, int limit) { GuildMembers members = new GuildMembers(); string memberLimit = limit.ToString(); using (WebClient client = new WebClient()) { client.Headers.Add(HttpRequestHeader.Authorization, tokenResponse.Type + " " + tokenResponse.AccessToken); try { IDiscordUriBuilder uriBuilder = new DiscordUriBuilder(); string uri = uriBuilder.AddPath(members.PathUrl.Replace("{guild}", guildId)) .AddEndOfPath() .AddParameter("limit", memberLimit, false) .Build(); string response = client.DownloadString(uri); members = serializer.Deserialize <GuildMembers>(response); } catch (Exception) { } } return(members); }
public bool AddUserToRole(TokenResponse tokenResponse, string guildId, string userId, string roleId) { using (WebClient client = new WebClient()) { client.Headers.Add(HttpRequestHeader.Authorization, tokenResponse.Type + " " + tokenResponse.AccessToken); try { IDiscordUriBuilder uriBuilder = new DiscordUriBuilder(); string uri = uriBuilder.AddPath(string.Format("guilds/{0}/members/{1}/roles/{2}", guildId, userId, roleId)) .Build(); string response = client.UploadString(uri, "PUT", ""); if (response == "") { return(true); } } catch (Exception) { } return(false); } }