mirror of
https://github.com/RPCS3/discord-bot.git
synced 2026-01-31 01:25:22 +01:00
RPCS3 Compatibility Bot reimplemented in C# for .NET Core Current status of this PR: * tested and targeted for .NET Core 2.1 * all functionality is either on par or improved compared to the python version * compatibility with current bot.db should be preserved in all upgrade scenarios * some bot management commands were changed (now under !sudo bot) * standard help generator for the new discord client is ... different; compatibility with old format could be restored through custom formatter if needed * everything has been split in more loosely tied components for easier extensibility and maintenance * log parsing has been rewritten and should work ~2x as fast
111 lines
4.4 KiB
C#
111 lines
4.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Formatting;
|
|
using System.Net.Http.Headers;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using CompatApiClient.Compression;
|
|
using CompatApiClient.POCOs;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace CompatApiClient
|
|
{
|
|
public class Client
|
|
{
|
|
private readonly HttpClient client;
|
|
private readonly MediaTypeFormatterCollection formatters;
|
|
|
|
private static readonly Dictionary<string, PrInfo> prInfoCache = new Dictionary<string, PrInfo>();
|
|
|
|
public Client()
|
|
{
|
|
client = HttpClientFactory.Create(new CompressionMessageHandler());
|
|
var settings = new JsonSerializerSettings
|
|
{
|
|
ContractResolver = new JsonContractResolver(NamingStyles.Underscore),
|
|
NullValueHandling = NullValueHandling.Ignore
|
|
};
|
|
formatters = new MediaTypeFormatterCollection(new[] {new JsonMediaTypeFormatter {SerializerSettings = settings}});
|
|
}
|
|
|
|
//todo: cache results
|
|
public async Task<CompatResult> GetCompatResultAsync(RequestBuilder requestBuilder, CancellationToken cancellationToken)
|
|
{
|
|
var message = new HttpRequestMessage(HttpMethod.Get, requestBuilder.Build());
|
|
var startTime = DateTime.UtcNow;
|
|
CompatResult result;
|
|
try
|
|
{
|
|
var response = await client.SendAsync(message, HttpCompletionOption.ResponseContentRead, cancellationToken).ConfigureAwait(false);
|
|
result = await response.Content.ReadAsAsync<CompatResult>(formatters, cancellationToken).ConfigureAwait(false);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
PrintError(e);
|
|
result = new CompatResult{ReturnCode = -2};
|
|
}
|
|
result.RequestBuilder = requestBuilder;
|
|
result.RequestDuration = DateTime.UtcNow - startTime;
|
|
return result;
|
|
}
|
|
|
|
public async Task<UpdateInfo> GetUpdateAsync(CancellationToken cancellationToken, string commit = "somecommit")
|
|
{
|
|
var message = new HttpRequestMessage(HttpMethod.Get, "https://update.rpcs3.net/?c=" + commit);
|
|
UpdateInfo result;
|
|
try
|
|
{
|
|
var response = await client.SendAsync(message, HttpCompletionOption.ResponseContentRead, cancellationToken).ConfigureAwait(false);
|
|
result = await response.Content.ReadAsAsync<UpdateInfo>(formatters, cancellationToken).ConfigureAwait(false);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
PrintError(e);
|
|
result = new UpdateInfo { ReturnCode = -2 };
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public async Task<PrInfo> GetPrInfoAsync(string pr, CancellationToken cancellationToken)
|
|
{
|
|
if (prInfoCache.TryGetValue(pr, out var result))
|
|
return result;
|
|
|
|
var message = new HttpRequestMessage(HttpMethod.Get, "https://api.github.com/repos/RPCS3/rpcs3/pulls/" + pr);
|
|
HttpContent content = null;
|
|
try
|
|
{
|
|
message.Headers.UserAgent.Add(new ProductInfoHeaderValue("RPCS3CompatibilityBot", "2.0"));
|
|
var response = await client.SendAsync(message, HttpCompletionOption.ResponseContentRead, cancellationToken).ConfigureAwait(false);
|
|
content = response.Content;
|
|
await content.LoadIntoBufferAsync().ConfigureAwait(false);
|
|
result = await content.ReadAsAsync<PrInfo>(formatters, cancellationToken).ConfigureAwait(false);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
PrintError(e);
|
|
if (content != null)
|
|
try { Console.WriteLine(await content.ReadAsStringAsync().ConfigureAwait(false)); } catch {}
|
|
int.TryParse(pr, out var prnum);
|
|
return new PrInfo{Number = prnum};
|
|
}
|
|
|
|
lock (prInfoCache)
|
|
{
|
|
if (prInfoCache.TryGetValue(pr, out var cachedResult))
|
|
return cachedResult;
|
|
|
|
prInfoCache[pr] = result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
private void PrintError(Exception e)
|
|
{
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
Console.WriteLine("Error communicating with api: " + e.Message);
|
|
Console.ResetColor();
|
|
}
|
|
}
|
|
} |