2024-04-13 06:57:46 +00:00
|
|
|
|
using DiscordRewards;
|
2024-01-22 09:27:07 +00:00
|
|
|
|
using Logging;
|
2024-02-19 14:57:28 +00:00
|
|
|
|
using System.Net.Http.Json;
|
2024-01-22 09:27:07 +00:00
|
|
|
|
|
|
|
|
|
namespace TestNetRewarder
|
|
|
|
|
{
|
|
|
|
|
public class BotClient
|
|
|
|
|
{
|
|
|
|
|
private readonly Configuration configuration;
|
|
|
|
|
private readonly ILog log;
|
|
|
|
|
|
|
|
|
|
public BotClient(Configuration configuration, ILog log)
|
|
|
|
|
{
|
|
|
|
|
this.configuration = configuration;
|
|
|
|
|
this.log = log;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> IsOnline()
|
|
|
|
|
{
|
2024-02-19 13:56:49 +00:00
|
|
|
|
var result = await HttpGet();
|
|
|
|
|
log.Log("Is DiscordBot online: " + result);
|
|
|
|
|
return result == "Pong";
|
2024-01-22 09:27:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:56:49 +00:00
|
|
|
|
public async Task<bool> SendRewards(GiveRewardsCommand command)
|
2024-01-22 09:27:07 +00:00
|
|
|
|
{
|
2024-04-12 06:35:20 +00:00
|
|
|
|
if (command == null) return false;
|
2024-02-19 14:57:28 +00:00
|
|
|
|
var result = await HttpPostJson(command);
|
|
|
|
|
log.Log("Reward response: " + result);
|
|
|
|
|
return result == "OK";
|
2024-02-19 13:56:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<string> HttpGet()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var client = new HttpClient();
|
|
|
|
|
var response = await client.GetAsync(GetUrl());
|
|
|
|
|
return await response.Content.ReadAsStringAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
log.Error(ex.ToString());
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
2024-01-22 09:27:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 14:57:28 +00:00
|
|
|
|
private async Task<string> HttpPostJson<T>(T body)
|
2024-01-22 09:27:07 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-02-19 14:57:28 +00:00
|
|
|
|
using var client = new HttpClient();
|
|
|
|
|
using var content = JsonContent.Create(body);
|
|
|
|
|
using var response = await client.PostAsync(GetUrl(), content);
|
2024-01-22 09:27:07 +00:00
|
|
|
|
return await response.Content.ReadAsStringAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
log.Error(ex.ToString());
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetUrl()
|
|
|
|
|
{
|
2024-02-19 13:56:49 +00:00
|
|
|
|
return $"{configuration.DiscordHost}:{configuration.DiscordPort}/api/reward";
|
2024-01-22 09:27:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|