Set up endpoints for user and guilds
This commit is contained in:
parent
4cf3f6c288
commit
f56e9c7e42
@ -1,5 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
@ -42,6 +43,49 @@ namespace ChaosBot.WebServer.App
|
|||||||
|
|
||||||
return LocalRedirect($"/#/?access_token={responseObject.access_token}");
|
return LocalRedirect($"/#/?access_token={responseObject.access_token}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet("user")]
|
||||||
|
public IActionResult GetUser(string access_token)
|
||||||
|
{
|
||||||
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", access_token);
|
||||||
|
|
||||||
|
HttpResponseMessage response;
|
||||||
|
using (HttpRequestMessage requestMessage =
|
||||||
|
new HttpRequestMessage(HttpMethod.Get, "https://discord.com/api/v7/users/@me"))
|
||||||
|
{
|
||||||
|
requestMessage.Headers.Authorization =
|
||||||
|
new AuthenticationHeaderValue("Bearer", access_token);
|
||||||
|
response = client.SendAsync(requestMessage).GetAwaiter().GetResult();
|
||||||
|
}
|
||||||
|
string responseString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
|
||||||
|
DiscordUserResponse userResponse = JsonConvert.DeserializeObject<DiscordUserResponse>(responseString);
|
||||||
|
|
||||||
|
return Json(new
|
||||||
|
{
|
||||||
|
id = userResponse.id,
|
||||||
|
username = $"{userResponse.username}#{userResponse.discriminator}",
|
||||||
|
avatar = userResponse.avatar
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("guilds")]
|
||||||
|
public IActionResult GetGuilds(string access_token)
|
||||||
|
{
|
||||||
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", access_token);
|
||||||
|
|
||||||
|
HttpResponseMessage response;
|
||||||
|
using (HttpRequestMessage requestMessage =
|
||||||
|
new HttpRequestMessage(HttpMethod.Get, "https://discord.com/api/v7/users/@me/guilds"))
|
||||||
|
{
|
||||||
|
requestMessage.Headers.Authorization =
|
||||||
|
new AuthenticationHeaderValue("Bearer", access_token);
|
||||||
|
response = client.SendAsync(requestMessage).GetAwaiter().GetResult();
|
||||||
|
}
|
||||||
|
string responseString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
|
||||||
|
List<DiscordGuildResponse> userResponse = JsonConvert.DeserializeObject<List<DiscordGuildResponse>>(responseString);
|
||||||
|
|
||||||
|
return Json(userResponse);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class DiscordOauthResponse
|
internal class DiscordOauthResponse
|
||||||
@ -55,4 +99,31 @@ namespace ChaosBot.WebServer.App
|
|||||||
public string scope = null;
|
public string scope = null;
|
||||||
public string token_type = null;
|
public string token_type = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class DiscordUserResponse
|
||||||
|
{
|
||||||
|
public string id;
|
||||||
|
public string username;
|
||||||
|
public string discriminator;
|
||||||
|
public string avatar = null;
|
||||||
|
public bool bot = false;
|
||||||
|
public bool system = false;
|
||||||
|
public bool mfa_enabled = false;
|
||||||
|
public string locale = null;
|
||||||
|
public bool verified = false;
|
||||||
|
public string email = null;
|
||||||
|
public int flags = 0;
|
||||||
|
public int premium_type = 0;
|
||||||
|
public int public_flags = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DiscordGuildResponse
|
||||||
|
{
|
||||||
|
public string id;
|
||||||
|
public string name;
|
||||||
|
public string icon;
|
||||||
|
public bool owner;
|
||||||
|
public int permissions;
|
||||||
|
public int permissions_new;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user