Skip to content

Commit

Permalink
added nitro support
Browse files Browse the repository at this point in the history
  • Loading branch information
iLinked1337 committed Sep 22, 2019
1 parent 1a596eb commit 57fae7e
Show file tree
Hide file tree
Showing 36 changed files with 301 additions and 36 deletions.
2 changes: 2 additions & 0 deletions Anarchy.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.29102.190
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Anarchy", "Anarchy\Anarchy.csproj", "{18C13E5D-D4D2-4310-993C-2F2644DB8D54}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testing", "Testing\Testing.csproj", "{CB9C71F7-0E04-4A54-BD7B-64BF778CA874}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
7 changes: 6 additions & 1 deletion Anarchy/Anarchy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@
<Compile Include="REST\Embed\EmbedError.cs" />
<Compile Include="REST\Embed\EmbedException.cs" />
<Compile Include="REST\Invite\Models\InviteType.cs" />
<Compile Include="REST\Nitro\Extensions.cs" />
<Compile Include="REST\Nitro\Models\NitroBoost.cs" />
<Compile Include="REST\Nitro\Models\NitroGift.cs" />
<Compile Include="REST\Nitro\Models\NitroSubscriptionPlan.cs" />
<Compile Include="REST\OAuth2\Models\ApplicationBot.cs" />
<Compile Include="REST\OAuth2\Models\AuthorizedApp.cs" />
<Compile Include="REST\OAuth2\Models\OAuth2Application.cs" />
<Compile Include="REST\OAuth2\Extensions.cs" />
Expand Down Expand Up @@ -184,7 +189,7 @@
<Compile Include="REST\Channel\Channel\ChannelType.cs" />
<Compile Include="REST\Guild\Models\VerificationLevel.cs" />
<Compile Include="REST\User\Profile\Hypesquad.cs" />
<Compile Include="REST\User\User\NitroType.cs" />
<Compile Include="REST\Nitro\Models\NitroType.cs" />
<Compile Include="REST\User\Profile\Status.cs" />
<Compile Include="Multi\Models\DiscordException.cs" />
<Compile Include="REST\RateLimit\RateLimitException.cs" />
Expand Down
55 changes: 33 additions & 22 deletions Anarchy/Multi/Models/DiscordImage.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace Discord
{
Expand All @@ -9,32 +10,42 @@ namespace Discord
/// </summary>
public class DiscordImage
{
public DiscordImage()
{ }

public DiscordImage(string base64)
{
Base64 = base64;

MemoryStream ms = new MemoryStream(Convert.FromBase64String(base64.Split(',')[1]));
Image = Image.FromStream(ms);
ms.Dispose();
}

public string Base64 { get; private set; }

private Image _image;
public Image Image
public Image Image { get; private set; }


public void SetImage(Image img)
{
get { return _image; }
set
if (img == null)
Base64 = null;
else
{
if (value == null)
Base64 = null;
else
{
string type;

if (ImageFormat.Jpeg.Equals(value.RawFormat))
type = "jpeg";
else if (ImageFormat.Png.Equals(value.RawFormat))
type = "png";
else if (ImageFormat.Gif.Equals(value.RawFormat))
type = "gif";
else return;

Base64 = $"data:image/{type};base64," +
Convert.ToBase64String((byte[])new ImageConverter().ConvertTo(value, typeof(byte[])));
_image = value;
}
string type;

if (ImageFormat.Jpeg.Equals(img.RawFormat))
type = "jpeg";
else if (ImageFormat.Png.Equals(img.RawFormat))
type = "png";
else if (ImageFormat.Gif.Equals(img.RawFormat))
type = "gif";
else return;

Base64 = $"data:image/{type};base64," +
Convert.ToBase64String((byte[])new ImageConverter().ConvertTo(img, typeof(byte[])));
Image = img;
}
}

Expand Down
4 changes: 2 additions & 2 deletions Anarchy/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.6.2.2")]
[assembly: AssemblyFileVersion("0.6.2.2")]
[assembly: AssemblyVersion("0.6.3.0")]
[assembly: AssemblyFileVersion("0.6.3.0")]
2 changes: 1 addition & 1 deletion Anarchy/REST/Channel/Private/Group/GroupProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public Image Icon
get { return _image.Image; }
set
{
_image.Image = value;
_image.SetImage(value);
IconSet = true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Anarchy/REST/Emoji/Emoji/EmojiProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Image Image
}
set
{
_image.Image = value;
_image.SetImage(value);
}
}
#endregion
Expand Down
2 changes: 1 addition & 1 deletion Anarchy/REST/Guild/Properties/CreationProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private string _icon
public Image Icon
{
get { return _image.Image; }
set { _image.Image = value; }
set { _image.SetImage(value); ; }
}
#endregion
}
Expand Down
2 changes: 1 addition & 1 deletion Anarchy/REST/Guild/Properties/GuildProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Image Icon
get { return _image.Image; }
set
{
_image.Image = value;
_image.SetImage(value);
_useBase64 = true;
IconSet = true;
}
Expand Down
4 changes: 4 additions & 0 deletions Anarchy/REST/HTTP/DiscordError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public enum DiscordError
UnknownUser,
UnknownEmoji,
UnknownWebhook,
UnknownGiftCode = 10038,
UserOnly = 20001,
BotOnly,
MaximumGuilds = 30001,
Expand All @@ -25,6 +26,7 @@ public enum DiscordError
MaximumRoles,
MaximumReactions = 30010,
MaximumGuildChannels = 30013,
MaximumGuildSubscriptions = 30026,
Unauthorized = 40001,
AccountUnverified,
InvalidInvite = 40007,
Expand Down Expand Up @@ -54,7 +56,9 @@ public enum DiscordError
InvalidOAuthAccessToken = 50034,
InvalidFormBody,
InvalidAPIVersion = 50041,
NitroGiftRedeemed = 50050,
InvalidGuild = 50055,
GuildSubscriptionCooldown = 50069,
IncomingFriendRequestsDisabled = 80000,
ReactionBlocked = 90001,
ResourceOverloaded = 130000
Expand Down
4 changes: 3 additions & 1 deletion Anarchy/REST/HTTP/DiscordHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ private void CheckResponse(HttpResponseMessage resp)
/// <param name="json">JSON content</param>
private HttpResponseMessage Send(HttpMethod method, string endpoint, string json = null)
{
#pragma warning disable IDE0068
HttpRequestMessage msg = new HttpRequestMessage
{
Method = method,
RequestUri = new Uri("https://discordapp.com/api/v6" + endpoint),
Content = json != null ? new StringContent(json, Encoding.UTF8, "application/json") : null
};
#pragma warning restore IDE0068

HttpResponseMessage resp = null;

Expand All @@ -81,7 +83,7 @@ private HttpResponseMessage Send(HttpMethod method, string endpoint, string json
}
catch (JsonReaderException)
{
if (resp.Content.ReadAsStringAsync().Result.StartsWith("<"))
if (resp.Content.ReadAsStringAsync().Result.Contains("<"))
throw new RateLimitException(_discordClient, 0);
}
CheckResponse(resp);
Expand Down
46 changes: 46 additions & 0 deletions Anarchy/REST/Nitro/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;

namespace Discord
{
public static class NitroExtensions
{
public static NitroGift GetNitroGift(this DiscordClient client, string code)
{
return client.HttpClient.Get($"/entitlements/gift-codes/{code}?with_application=false&with_subscription_plan=true")
.Deserialize<NitroGift>().SetClient(client);
}


public static void RedeemNitroGift(this DiscordClient client, string code, ulong? channelId = null)
{
client.HttpClient.Post($"/entitlements/gift-codes/{code}/redeem", channelId.HasValue ? $"{{\"channel_id\":{channelId.Value}}}" : "");
}


public static List<NitroBoost> GetNitroBoosts(this DiscordClient client)
{
return client.HttpClient.Get("/users/@me/guilds/premium/subscriptions").Deserialize<List<NitroBoost>>();
}


public static void BoostGuild(this DiscordClient client, ulong guildId)
{
client.HttpClient.Put($"/guilds/{guildId}/premium/subscriptions");
}


public static void RemoveGuildBoost(this DiscordClient client, ulong guildId, ulong subscriptionId)
{
client.HttpClient.Delete($"/guilds/{guildId}/premium/subscriptions/{subscriptionId}");
}


public static DateTime GetBoostCooldown(this DiscordClient client)
{
return (DateTime)client.HttpClient.Get("/users/@me/guilds/premium/subscriptions/cooldown")
.Deserialize<JObject>().GetValue("ends_at").ToObject(typeof(DateTime));
}
}
}
22 changes: 22 additions & 0 deletions Anarchy/REST/Nitro/Models/NitroBoost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Newtonsoft.Json;

namespace Discord
{
public class NitroBoost
{
[JsonProperty("id")]
public ulong Id { get; private set; }


[JsonProperty("guild_id")]
public ulong GuildId { get; private set; }


[JsonProperty("user_id")]
public ulong UserId { get; private set; }


[JsonProperty("ended")]
public bool Ended { get; private set; }
}
}
66 changes: 66 additions & 0 deletions Anarchy/REST/Nitro/Models/NitroGift.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using Newtonsoft.Json;

namespace Discord
{
public class NitroGift : Controllable
{
[JsonProperty("subscription_plan")]
public NitroSubscriptionPlan SubscriptionPlan { get; private set; }


[JsonProperty("application_id")]
public ulong ApplicationId { get; private set; }


[JsonProperty("redeemed")]
public bool Redeemed { get; private set; }


[JsonProperty("expires_at")]
#pragma warning disable CS0649
private readonly string _expiresAt;
#pragma warning restore CS0649


public DateTime ExpiresAt
{
get { return DiscordTimestamp.FromString(_expiresAt); }
}


[JsonProperty("code")]
public string Code { get; private set; }


[JsonProperty("user")]
public User Gifter { get; private set; }


[JsonProperty("max_uses")]
public int MaxUses { get; private set; }


[JsonProperty("id")]
public ulong Id { get; private set; }


[JsonProperty("summary")]
public string Summary { get; private set; }


public void Redeem(ulong? channelId = null)
{
if (Redeemed)
return;

Client.RedeemNitroGift(Code, channelId);
}


public override string ToString()
{
return SubscriptionPlan.Name;
}
}
}
30 changes: 30 additions & 0 deletions Anarchy/REST/Nitro/Models/NitroSubscriptionPlan.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Newtonsoft.Json;

namespace Discord
{
public class NitroSubscriptionPlan
{
[JsonProperty("sku_id")]
public ulong SkuId { get; private set; }


[JsonProperty("name")]
public string Name { get; private set; }


[JsonProperty("currency")]
public string Currency { get; private set; }


[JsonProperty("price")]
public int Price { get; private set; }


[JsonProperty("tax_inclusive")]
public bool TaxInclusive { get; private set; }


[JsonProperty("id")]
public ulong Id { get; private set; }
}
}
File renamed without changes.
20 changes: 20 additions & 0 deletions Anarchy/REST/OAuth2/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,25 @@ public static void RemoveAuthorizedApp(this DiscordClient client, ulong appId)
{
client.HttpClient.Delete($"/oauth2/tokens/{appId}");
}


public static OAuth2Application CreateApplication(this DiscordClient client, string name)
{
return client.HttpClient.Post("/oauth2/applications", $"{{\"name\":\"{name}\"}}")
.Deserialize<OAuth2Application>().SetClient(client);
}


public static ApplicationBot AddApplicationBot(this DiscordClient client, ulong appId)
{
return client.HttpClient.Post($"/oauth2/applications/{appId}/bot")
.Deserialize<ApplicationBot>();
}


public static void DeleteApplication(this DiscordClient client, ulong appId)
{
client.HttpClient.Delete($"/oauth2/applications/{appId}");
}
}
}
Loading

0 comments on commit 57fae7e

Please sign in to comment.