Discord Plugin Source

Added discord plugin source to github
This commit is contained in:
Brett Hewitson 2020-07-11 13:09:27 +10:00
parent 4a163627b8
commit 6f671a9d57
50 changed files with 3673 additions and 0 deletions

View file

@ -0,0 +1,90 @@
using System;
using System.Diagnostics;
using System.Net;
using System.Threading.Tasks;
namespace ServerManagerTool.Plugin.Discord
{
internal static class NetworkUtils
{
public static async Task<IPAddress> DiscoverPublicIPAsync()
{
using (var webClient = new WebClient())
{
try
{
var publicIP = await webClient.DownloadStringTaskAsync(Config.Default.PublicIPCheckUrl);
if (IPAddress.TryParse(publicIP, out IPAddress address))
return address;
return IPAddress.None;
}
catch (Exception ex)
{
Debug.WriteLine($"ERROR: {nameof(DiscoverPublicIPAsync)}\r\n{ex.Message}");
return IPAddress.None;
}
}
}
public static async Task<Version> CheckLatestVersionAsync(bool betaEnabled)
{
try
{
using (var webClient = new WebClient())
{
string latestVersion = null;
if (betaEnabled)
latestVersion = await webClient.DownloadStringTaskAsync(Config.Default.LatestBetaVersionUrl);
else
latestVersion = await webClient.DownloadStringTaskAsync(Config.Default.LatestVersionUrl);
if (Version.TryParse(latestVersion, out Version version))
return version;
return new Version();
}
}
catch (Exception ex)
{
Debug.WriteLine($"ERROR: {nameof(CheckLatestVersionAsync)}\r\n{ex.Message}");
return new Version();
}
}
public static bool DownloadLatestVersion(string sourceUrl, string destinationFile)
{
try
{
using (var client = new WebClient())
{
client.DownloadFile(sourceUrl, destinationFile);
return true;
}
}
catch (Exception ex)
{
Debug.WriteLine($"ERROR: {nameof(DownloadLatestVersion)}\r\n{ex.Message}");
return false;
}
}
public static async Task PerformCallToAPIAsync(string pluginCode, IPAddress ipAddress)
{
try
{
using (var client = new WebClient())
{
var url = string.Format(Config.Default.PluginCallUrlFormat, pluginCode, ipAddress);
await client.DownloadStringTaskAsync(url);
}
}
catch (Exception ex)
{
Debug.WriteLine($"ERROR: {nameof(PerformCallToAPIAsync)} - {pluginCode}; {ipAddress}\r\n{ex.Message}");
}
}
}
}

View file

@ -0,0 +1,15 @@
using System;
using System.Threading.Tasks;
namespace ServerManagerTool.Plugin.Discord
{
internal static class TaskUtils
{
public static readonly Task FinishedTask = Task.FromResult(true);
public static void DoNotWait(this Task task)
{
// Do nothing, let the task continue. Eliminates compiler warning about non-awaited tasks in an async method.
}
}
}

View file

@ -0,0 +1,53 @@
using System;
using System.ServiceModel.Syndication;
using System.Xml;
namespace ServerManagerTool.Plugin.Discord
{
public static class VersionFeedUtils
{
public static VersionFeed LoadVersionFeed(string inputUri, string currentVersion)
{
try
{
var reader = XmlReader.Create(inputUri);
var feed = SyndicationFeed.Load(reader);
var versionFeed = new VersionFeed
{
Id = feed.Id,
Title = feed.Title?.Text,
SubTitle = feed.Description?.Text,
Link = feed.Links?[0].Uri,
Updated = feed.LastUpdatedTime.ToLocalTime(),
};
//Loop through all items in the SyndicationFeed
foreach (var item in feed.Items)
{
var textContent = item.Content as TextSyndicationContent;
var versionFeedEntry = new VersionFeedEntry
{
Id = item.Id,
Title = item.Title?.Text,
Summary = item.Summary?.Text,
Link = item.Links?[0].Uri,
Updated = item.LastUpdatedTime.ToLocalTime(),
Content = textContent?.Text,
Author = item.Authors?[0].Name,
IsCurrent = (item.Summary?.Text ?? string.Empty).Equals(currentVersion),
};
versionFeed.Entries.Add(versionFeedEntry);
}
return versionFeed;
}
catch (Exception)
{
return new VersionFeed();
}
}
}
}