Browse Source

Added Application and DiscordClient.GetApplicationInfoAsync()

tags/1.0-rc
RogueException 9 years ago
parent
commit
f923a7f0b3
8 changed files with 122 additions and 7 deletions
  1. +5
    -3
      src/Discord.Net/API/CDN.cs
  2. +22
    -0
      src/Discord.Net/API/Common/Application.cs
  3. +7
    -0
      src/Discord.Net/API/DiscordAPIClient.cs
  4. +11
    -0
      src/Discord.Net/API/Rest/ApplicationInfo.cs
  5. +11
    -4
      src/Discord.Net/DiscordClient.cs
  6. +51
    -0
      src/Discord.Net/Entities/Application.cs
  7. +13
    -0
      src/Discord.Net/Entities/IApplication.cs
  8. +2
    -0
      src/Discord.Net/IDiscordClient.cs

+ 5
- 3
src/Discord.Net/API/CDN.cs View File

@@ -2,12 +2,14 @@
{
internal static class CDN
{
public static string GetApplicationIconUrl(ulong appId, string iconId)
=> iconId != null ? $"{DiscordConfig.ClientAPIUrl}app-icons/{appId}/{iconId}.jpg" : null;
public static string GetUserAvatarUrl(ulong userId, string avatarId)
=> avatarId != null ? $"{DiscordConfig.ClientAPIUrl}users/{userId}/avatars/{avatarId}.jpg" : null;
=> avatarId != null ? $"{DiscordConfig.ClientAPIUrl}avatars/{userId}/{avatarId}.jpg" : null;
public static string GetGuildIconUrl(ulong guildId, string iconId)
=> iconId != null ? $"{DiscordConfig.ClientAPIUrl}guilds/{guildId}/icons/{iconId}.jpg" : null;
=> iconId != null ? $"{DiscordConfig.ClientAPIUrl}icons/{guildId}/{iconId}.jpg" : null;
public static string GetGuildSplashUrl(ulong guildId, string splashId)
=> splashId != null ? $"{DiscordConfig.ClientAPIUrl}guilds/{guildId}/splashes/{splashId}.jpg" : null;
=> splashId != null ? $"{DiscordConfig.ClientAPIUrl}splashes/{guildId}/{splashId}.jpg" : null;
public static string GetChannelIconUrl(ulong channelId, string iconId)
=> iconId != null ? $"{DiscordConfig.ClientAPIUrl}channel-icons/{channelId}/{iconId}.jpg" : null;
}


+ 22
- 0
src/Discord.Net/API/Common/Application.cs View File

@@ -0,0 +1,22 @@
using Newtonsoft.Json;

namespace Discord.API
{
public class Application
{
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("rpc_origins")]
public string[] RPCOrigins { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("flags"), Int53]
public ulong Flags { get; set; }
[JsonProperty("owner")]
public User Owner { get; set; }
[JsonProperty("id")]
public ulong Id { get; set; }
[JsonProperty("icon")]
public string Icon { get; set; }
}
}

+ 7
- 0
src/Discord.Net/API/DiscordAPIClient.cs View File

@@ -351,6 +351,13 @@ namespace Discord.API
await _sentGatewayMessageEvent.InvokeAsync(opCode).ConfigureAwait(false);
}

//Application
public async Task<Application> GetMyApplicationInfoAsync(RequestOptions options = null)
{
return await SendAsync<Application>("GET", "oauth2/applications/@me", options: options).ConfigureAwait(false);
}


//Auth
public async Task ValidateTokenAsync(RequestOptions options = null)
{


+ 11
- 0
src/Discord.Net/API/Rest/ApplicationInfo.cs View File

@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Discord.API.Rest
{
public class ApplicationInfo
{
}
}

+ 11
- 4
src/Discord.Net/DiscordClient.cs View File

@@ -145,12 +145,12 @@ namespace Discord
protected virtual Task OnLogoutAsync() => Task.CompletedTask;

/// <inheritdoc />
public async Task<IReadOnlyCollection<IConnection>> GetConnectionsAsync()
public async Task<IApplication> GetApplicationInfoAsync()
{
var models = await ApiClient.GetMyConnectionsAsync().ConfigureAwait(false);
return models.Select(x => new Connection(x)).ToImmutableArray();
var model = await ApiClient.GetMyApplicationInfoAsync().ConfigureAwait(false);
return new Application(this, model);
}
/// <inheritdoc />
public virtual async Task<IChannel> GetChannelAsync(ulong id)
{
@@ -185,6 +185,13 @@ namespace Discord
var models = await ApiClient.GetMyPrivateChannelsAsync().ConfigureAwait(false);
return models.Select(x => new DMChannel(this, new User(x.Recipients.Value[0]), x)).ToImmutableArray();
}
/// <inheritdoc />
public async Task<IReadOnlyCollection<IConnection>> GetConnectionsAsync()
{
var models = await ApiClient.GetMyConnectionsAsync().ConfigureAwait(false);
return models.Select(x => new Connection(x)).ToImmutableArray();
}

/// <inheritdoc />
public virtual async Task<IInvite> GetInviteAsync(string inviteIdOrXkcd)


+ 51
- 0
src/Discord.Net/Entities/Application.cs View File

@@ -0,0 +1,51 @@
using System;
using System.Threading.Tasks;
using Model = Discord.API.Application;

namespace Discord
{
internal class Application : SnowflakeEntity, IApplication
{
protected string _iconId;
public string Name { get; private set; }
public string Description { get; private set; }
public string[] RPCOrigins { get; private set; }
public ulong Flags { get; private set; }

public override DiscordClient Discord { get; }
public IUser Owner { get; private set; }

public string IconUrl => API.CDN.GetApplicationIconUrl(Id, _iconId);

public Application(DiscordClient discord, Model model)
: base(model.Id)
{
Discord = discord;

Update(model, UpdateSource.Creation);
}

internal void Update(Model model, UpdateSource source)
{
if (source == UpdateSource.Rest && IsAttached) return;
Description = model.Description;
RPCOrigins = model.RPCOrigins;
Name = model.Name;
Flags = model.Flags;
Owner = new User(model.Owner);
_iconId = model.Icon;
}

public async Task UpdateAsync()
{
if (IsAttached) throw new NotSupportedException();

var response = await Discord.ApiClient.GetMyApplicationInfoAsync().ConfigureAwait(false);
if (response.Id != Id)
throw new InvalidOperationException("Unable to update this object from a different application token.");
Update(response, UpdateSource.Rest);
}
}
}

+ 13
- 0
src/Discord.Net/Entities/IApplication.cs View File

@@ -0,0 +1,13 @@
namespace Discord
{
public interface IApplication : ISnowflakeEntity, IUpdateable
{
string Name { get; }
string Description { get; }
string[] RPCOrigins { get; }
ulong Flags { get; }
string IconUrl { get; }

IUser Owner { get; }
}
}

+ 2
- 0
src/Discord.Net/IDiscordClient.cs View File

@@ -23,6 +23,8 @@ namespace Discord
Task ConnectAsync();
Task DisconnectAsync();

Task<IApplication> GetApplicationInfoAsync();

Task<IChannel> GetChannelAsync(ulong id);
Task<IReadOnlyCollection<IPrivateChannel>> GetPrivateChannelsAsync();



Loading…
Cancel
Save