Make JsonObject init code nicer

This commit is contained in:
UnknownShadow200 2021-04-16 18:50:26 +10:00
parent 4f90c43d74
commit a25f00b239
2 changed files with 49 additions and 39 deletions

View File

@ -23,9 +23,9 @@ using MCGalaxy.Network;
namespace MCGalaxy.Modules.Relay.Discord {
/// <summary> Implements a basic web client for communicating with Discord's API </summary>
/// <remarks> https://discord.com/developers/docs/reference </remarks>
/// <remarks> https://discord.com/developers/docs/resources/channel#create-message </remarks>
/// <summary> Implements a basic web client for communicating with Discord's API </summary>
/// <remarks> https://discord.com/developers/docs/reference </remarks>
/// <remarks> https://discord.com/developers/docs/resources/channel#create-message </remarks>
public sealed class DiscordApiClient {
public string Token;
@ -35,21 +35,23 @@ namespace MCGalaxy.Modules.Relay.Discord {
// TODO HttpWebRequest
string data = Json.SerialiseObject(obj);
using (WebClient client = HttpUtil.CreateWebClient()) {
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.Headers[HttpRequestHeader.Authorization] = "Bot " + Token;
using (WebClient client = HttpUtil.CreateWebClient()) {
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.Headers[HttpRequestHeader.Authorization] = "Bot " + Token;
string resp = client.UploadString(host + path, data);
Logger.Log(LogType.SystemActivity, resp);
}
string resp = client.UploadString(host + path, data);
Logger.Log(LogType.SystemActivity, resp);
}
}
public void SendMessage(string channelID, string message) {
JsonObject obj = new JsonObject();
obj["content"] = message;
string path = "/channels/" + channelID + "/messages";
MakeRequest(path, obj);
JsonObject obj = new JsonObject()
{
{ "content", message },
};
string path = "/channels/" + channelID + "/messages";
MakeRequest(path, obj);
}
}
}

View File

@ -26,9 +26,9 @@ using MCGalaxy.Tasks;
namespace MCGalaxy.Modules.Relay.Discord {
/// <summary> Implements a basic websocket for communicating with Discord's gateway </summary>
/// <remarks> https://discord.com/developers/docs/topics/gateway </remarks>
/// <remarks> https://i.imgur.com/Lwc5Wde.png </remarks>
/// <summary> Implements a basic websocket for communicating with Discord's gateway </summary>
/// <remarks> https://discord.com/developers/docs/topics/gateway </remarks>
/// <remarks> https://i.imgur.com/Lwc5Wde.png </remarks>
public sealed class DiscordWebsocket : ClientWebSocket {
public string Token;
public Action OnReady;
@ -136,9 +136,11 @@ namespace MCGalaxy.Modules.Relay.Discord {
public void SendMessage(int opcode, JsonObject data) {
JsonObject obj = new JsonObject();
obj["op"] = opcode;
obj["d"] = data;
JsonObject obj = new JsonObject()
{
{ "op", opcode },
{ "d", data }
};
SendMessage(obj);
}
@ -166,17 +168,20 @@ namespace MCGalaxy.Modules.Relay.Discord {
const int INTENT_GUILD_MESSAGES = 1 << 9;
public void SendIdentify() {
JsonObject data = new JsonObject();
JsonObject props = new JsonObject()
{
{ "$os", "linux" },
{ "$browser", Server.SoftwareName },
{ "$device", Server.SoftwareName }
};
JsonObject props = new JsonObject();
props["$os"] = "linux";
props["$browser"] = Server.SoftwareName;
props["$device"] = Server.SoftwareName;
data["token"] = Token;
data["intents"] = INTENT_GUILD_MESSAGES;
data["properties"] = props;
data["presence"] = MakePresence();
JsonObject data = new JsonObject()
{
{ "token", Token },
{ "intents", INTENT_GUILD_MESSAGES },
{ "properties", props },
{ "presence", MakePresence() }
};
SendMessage(OPCODE_IDENTIFY, data);
}
@ -186,18 +191,21 @@ namespace MCGalaxy.Modules.Relay.Discord {
}
JsonObject MakePresence() {
JsonObject activity = new JsonObject();
activity["name"] = GetStatus();
activity["type"] = 0;
JsonObject activity = new JsonObject()
{
{ "name", GetStatus() },
{ "type", 0 }
};
JsonArray activites = new JsonArray();
activites.Add(activity);
JsonObject obj = new JsonObject();
obj["since"] = null;
obj["activities"] = activites;
obj["status"] = "online";
obj["afk"] = false;
JsonObject obj = new JsonObject()
{
{ "since", null },
{ "activities", activites },
{ "status", "online" },
{ "afk", false }
};
return obj;
}
}