Skip to content

Commit

Permalink
Discord bot: Avoid hardcoding API/WebSocket hosts and paths
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Sep 27, 2023
1 parent a4cc8fb commit f326fd3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
4 changes: 2 additions & 2 deletions MCGalaxy/Modules/Relay/Discord/DiscordApiClient.cs
Expand Up @@ -136,7 +136,7 @@ public class ChannelSendEmbed : DiscordApiMessage
public sealed class DiscordApiClient : AsyncWorker<DiscordApiMessage>
{
public string Token;
const string host = "https://discord.com/api/v10";
public string Host;

DiscordApiMessage GetNextRequest() {
if (queue.Count == 0) return null;
Expand All @@ -162,7 +162,7 @@ public sealed class DiscordApiClient : AsyncWorker<DiscordApiMessage>
for (int retry = 0; retry < 10; retry++)
{
try {
HttpWebRequest req = HttpUtil.CreateRequest(host + msg.Path);
HttpWebRequest req = HttpUtil.CreateRequest(Host + msg.Path);
req.Method = msg.Method;
req.ContentType = "application/json";
req.Headers[HttpRequestHeader.Authorization] = "Bot " + Token;
Expand Down
11 changes: 8 additions & 3 deletions MCGalaxy/Modules/Relay/Discord/DiscordBot.cs
Expand Up @@ -54,15 +54,20 @@ public sealed class DiscordBot : RelayBot
"// example:http://example.org",
"// That would replace 'example' in messages sent to Discord with 'http://example.org'");

public string APIHost = "https://discord.com/api/v10";
public string WSHost = "gateway.discord.gg";
public string WSPath = "/?v=10&encoding=json";


protected override bool CanReconnect {
get { return canReconnect && (socket == null || socket.CanReconnect); }
}

protected override void DoConnect() {
socket = new DiscordWebsocket();
socket = new DiscordWebsocket(WSPath);
socket.Session = session;
socket.Token = Config.BotToken;
socket.Host = WSHost;
socket.Presence = Config.PresenceEnabled;
socket.Status = Config.Status;
socket.Activity = Config.Activity;
Expand Down Expand Up @@ -203,6 +208,7 @@ public sealed class DiscordBot : RelayBot
if (api == null) {
api = new DiscordApiClient();
api.Token = Config.BotToken;
api.Host = APIHost;
api.RunAsync();
}
OnReady();
Expand Down Expand Up @@ -243,8 +249,7 @@ public sealed class DiscordBot : RelayBot
// "Bots no longer receive Channel Create Gateway Event for DMs"
// Therefore the code is now forced to instead calculate which
// channels are probably text channels, and which aren't
if (!channelTypes.TryGetValue(channel, out type))
{
if (!channelTypes.TryGetValue(channel, out type)) {
type = GuessChannelType(data);
// channel is definitely a text/normal channel
if (type == CHANNEL_TEXT) channelTypes[channel] = type;
Expand Down
13 changes: 7 additions & 6 deletions MCGalaxy/Modules/Relay/Discord/DiscordWebsocket.cs
Expand Up @@ -42,6 +42,8 @@ public sealed class DiscordWebsocket : ClientWebSocket
{
/// <summary> Authorisation token for the bot account </summary>
public string Token;
public string Host;

public bool CanReconnect = true, SentIdentify;
public DiscordSession Session;

Expand Down Expand Up @@ -86,28 +88,27 @@ public sealed class DiscordWebsocket : ClientWebSocket
const int OPCODE_HEARTBEAT_ACK = 11;


public DiscordWebsocket() {
path = "/?v=10&encoding=json";
public DiscordWebsocket(string apiPath) {
path = apiPath;
}

const string host = "gateway.discord.gg";
// stubs
public override bool LowLatency { set { } }
public override IPAddress IP { get { return null; } }

public void Connect() {
client = new TcpClient();
client.Connect(host, 443);
client.Connect(Host, 443);
readable = true;

stream = HttpUtil.WrapSSLStream(client.GetStream(), host);
stream = HttpUtil.WrapSSLStream(client.GetStream(), Host);
protocol = this;
Init();
}

protected override void WriteCustomHeaders() {
WriteHeader("Authorization: Bot " + Token);
WriteHeader("Host: " + host);
WriteHeader("Host: " + Host);
}

public override void Close() {
Expand Down

0 comments on commit f326fd3

Please sign in to comment.