Skip to content

Commit de50852

Browse files
authored
Discord webhooks (#39)
* Added option DiscordWebhookUrl * Added webhooks
1 parent 3458b98 commit de50852

File tree

7 files changed

+75
-3
lines changed

7 files changed

+75
-3
lines changed

SEWorkshopTool/SEWorkshopTool.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,4 @@
115115
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
116116
<Exec Command="set APPID=244850&#xD;&#xA;if NOT exist &quot;$(TargetDir)steam_appid.txt&quot; echo %APPID% &gt; &quot;$(TargetDir)steam_appid.txt&quot;&#xD;&#xA;if NOT exist &quot;$(TargetDir)\Bin64&quot; mklink /j &quot;$(ProjectDir)\Bin64&quot; &quot;$(TargetDir)\Bin64&quot;&#xD;&#xA;powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -Command &quot;&amp; {Compress-Archive -Force -Path $(TargetDir)$(TargetName).exe, $(TargetDir)$(TargetName).exe.config, $(TargetDir)$(TargetName).pdb, $(TargetDir)CommandLine.dll, $(TargetDir).wtignore, $(TargetDir)steam_appid.txt -DestinationPath $(TargetDir)$(TargetName)-v$(VersionPrefix).zip}&quot;" />
117117
</Target>
118-
</Project>
118+
</Project>

Updater/UpdateChecker.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Reflection;
66
using System.Runtime.Serialization.Json;
77
using System.IO;
8-
using Sandbox;
98

109
namespace Gwindalmir.Updater
1110
{
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net;
4+
using System.Text;
5+
using System.Text.RegularExpressions;
6+
7+
namespace Phoenix.WorkshopTool
8+
{
9+
class DiscordWebhook
10+
{
11+
WorkshopType m_type;
12+
string m_title;
13+
string m_changelog;
14+
15+
//it is a small payload
16+
static readonly string _payload = @"{{""embeds"":[ {{""author"": {{""name"": ""{0}""}}, ""title"": ""{1}"", ""description"": ""{2}""}} ]}}";
17+
18+
//Discord webhook format
19+
static readonly Regex _urlValidator = new Regex("https://discord.com/api/webhooks/[0-9]+/[a-zA-Z0-9-]+", RegexOptions.Compiled, new TimeSpan(0, 0, 1));
20+
21+
public DiscordWebhook(WorkshopType type, string title, string changelog)
22+
{
23+
m_type = type;
24+
m_title = title;
25+
m_changelog = changelog;
26+
}
27+
28+
public bool Call(string url, out string error)
29+
{
30+
if (_urlValidator.IsMatch(url))
31+
{
32+
string requestPayload = string.Format(_payload, m_type, m_title.Replace("\r\n", "\\n"), m_changelog.Replace("\r\n", "\\n"));
33+
var request = (HttpWebRequest)WebRequest.Create(url);
34+
request.ContentType = "application/json";
35+
request.Method = "POST";
36+
37+
byte[] data = Encoding.UTF8.GetBytes(requestPayload);
38+
using (var stream = request.GetRequestStream())
39+
{
40+
stream.Write(data, 0, data.Length);
41+
}
42+
request.GetResponse();
43+
44+
error = string.Empty;
45+
return true;
46+
}
47+
else
48+
error = "Invalid webhook Url";
49+
50+
return false;
51+
}
52+
53+
}
54+
}

WorkshopToolCommon/GameBase.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,8 +665,20 @@ static bool ProcessItemsUpload(WorkshopType type, List<string> paths, ProcessedO
665665

666666
if (options.Upload)
667667
{
668-
if (mod.Publish())
668+
if (mod.Publish())
669+
{
670+
if (!options.DryRun && !string.IsNullOrEmpty(options.DiscordWebhookUrl))
671+
{
672+
MySandboxGame.Log.WriteLineAndConsole(string.Format("Discord-Webhook-Url: {0}", options.DiscordWebhookUrl));
673+
DiscordWebhook hook = new DiscordWebhook(type, mod.Title, changelog);
674+
if (hook.Call(options.DiscordWebhookUrl, out string error))
675+
MySandboxGame.Log.WriteLineAndConsole("Sent payload to discord webhook");
676+
else
677+
MySandboxGame.Log.WriteLineWarning(string.Format("Discord webhook error: {0}", error));
678+
}
679+
669680
MySandboxGame.Log.WriteLineAndConsole(string.Format("Complete: {0}", mod.Title));
681+
}
670682
else
671683
{
672684
success = false;

WorkshopToolCommon/Options/ProcessedOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class ProcessedOptions
2525
public bool UpdateOnly { get; set; }
2626
public bool DryRun { get; set; }
2727
public bool Compile { get; set; }
28+
public string DiscordWebhookUrl { get; set; }
2829
public IList<string> Mods { get; set; }
2930
public IList<string> Blueprints { get; set; }
3031
public IList<string> Scenarios { get; set; }
@@ -158,6 +159,7 @@ public ProcessedOptions(UploadVerb options)
158159

159160
ExcludeExtensions = options.ExcludeExtensions?.ToList();
160161
IgnorePaths = options.IgnorePaths?.ToList();
162+
DiscordWebhookUrl = options.DiscordWebhookUrl;
161163
}
162164

163165
public ProcessedOptions(ChangeVerb options)
@@ -313,6 +315,7 @@ public static explicit operator UploadVerb(ProcessedOptions options)
313315
result.DescriptionFile = options.Changelog;
314316
result.Compile = options.Compile;
315317
result.DryRun = options.DryRun;
318+
result.DiscordWebhookUrl = options.DiscordWebhookUrl;
316319
result.Thumbnail = options.Thumbnail;
317320
result.UpdateOnly = options.UpdateOnly;
318321
result.Visibility = options.Visibility;

WorkshopToolCommon/Options/UploadVerb.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,8 @@ public class UploadVerb : PublishVerbBase
3333

3434
[Option("message", HelpText = "Changelog message (requires actual content update)", MetaValue = "<text or filename>")]
3535
public string Changelog { get; set; }
36+
37+
[Option("discord-webhook", HelpText = "A link to a webhook that will publish update notes", MetaValue = "<url>")]
38+
public string DiscordWebhookUrl { get; set; }
3639
}
3740
}

WorkshopToolCommon/WorkshopToolCommon.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</PropertyGroup>
1111
<ItemGroup>
1212
<Compile Include="$(MSBuildThisFileDirectory)Constants.cs" />
13+
<Compile Include="$(MSBuildThisFileDirectory)DiscordWebhook.cs" />
1314
<Compile Include="$(MSBuildThisFileDirectory)Downloader.cs" />
1415
<Compile Include="$(MSBuildThisFileDirectory)Enums.cs" />
1516
<Compile Include="$(MSBuildThisFileDirectory)Extensions\Extensions.cs" />

0 commit comments

Comments
 (0)