Skip to content

Commit 4820da6

Browse files
Merge branch 'main' into dev
2 parents 06b8720 + 9ca45d7 commit 4820da6

File tree

7 files changed

+73
-57
lines changed

7 files changed

+73
-57
lines changed

Code/ArgumentSystem/BaseArguments/Argument.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ public abstract class Argument(string name)
66
{
77
public string Name { get; } = name;
88

9+
public bool IsRequired => DefaultValue is null;
10+
911
/// <summary>
1012
/// Allows for this argument to get an unlimited amount of values of this type
1113
/// Every value after this argument also counts towards this one.

Code/ContextSystem/Contexter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ private static Result TryAddResult(
4848
Stack<StatementContext> statementStack,
4949
List<Context> contexts
5050
) {
51-
Result rs = $"Invalid context {context} in line {lineNum}.";
51+
Result rs = $"Invalid context {context}";
5252

53-
Log.Debug($"Trying to add context {context} in line {lineNum}");
53+
Log.Debug($"Trying to add context {context}");
5454

5555
switch (context)
5656
{

Code/ContextSystem/Contexts/MethodContext.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,12 @@ public override TryAddTokenRes TryAddToken(BaseToken token)
5353

5454
public override Result VerifyCurrentState()
5555
{
56-
return Result.Assert(_providedArguments >= Method.ExpectedArguments.Count(arg => arg.DefaultValue is null),
56+
return Result.Assert(_providedArguments >= Method.ExpectedArguments.Count(arg => arg.IsRequired),
5757
$"Method '{Method.Name}' is missing required arguments: " +
58-
$"{", ".Join(Method.ExpectedArguments.Skip(_providedArguments).Select(arg => arg.Name))}");
58+
$"{", ".Join(Method.ExpectedArguments
59+
.Skip(_providedArguments)
60+
.Where(arg => arg.IsRequired)
61+
.Select(arg => arg.Name))}");
5962
}
6063

6164
protected override IEnumerator<float> Execute()
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using JetBrains.Annotations;
2+
using MEC;
3+
using Newtonsoft.Json.Linq;
4+
using SER.Code.ArgumentSystem.Arguments;
5+
using SER.Code.ArgumentSystem.BaseArguments;
6+
using SER.Code.Helpers.Extensions;
7+
using SER.Code.MethodSystem.BaseMethods.Synchronous;
8+
using SER.Code.MethodSystem.MethodDescriptors;
9+
using SER.Code.MethodSystem.Methods.HTTPMethods;
10+
11+
namespace SER.Code.MethodSystem.Methods.DiscordMethods;
12+
13+
[UsedImplicitly]
14+
public class SendDiscordMessageMethod : SynchronousMethod, ICanError
15+
{
16+
public override string Description => "Sends a message using a discord webhook.";
17+
18+
public string[] ErrorReasons => HTTPPostMethod.HttpErrorReasons;
19+
20+
public override Argument[] ExpectedArguments { get; } =
21+
[
22+
new TextArgument("webhook url"),
23+
new TextArgument("message content"),
24+
new TextArgument("webhook name")
25+
{
26+
DefaultValue = new(null, "default")
27+
},
28+
new TextArgument("avatar url")
29+
{
30+
DefaultValue = new(null, "default")
31+
}
32+
];
33+
34+
public override void Execute()
35+
{
36+
var webhookUrl = Args.GetText("webhook url");
37+
var messageContent = Args.GetText("message content");
38+
var webhookName = Args.GetText("webhook name").MaybeNull();
39+
var avatarUrl = Args.GetText("avatar url").MaybeNull();
40+
41+
JObject json = new()
42+
{
43+
["content"] = messageContent
44+
};
45+
46+
if (webhookName != null) json["username"] = webhookName;
47+
if (avatarUrl != null) json["avatar_url"] = avatarUrl;
48+
49+
Timing.RunCoroutine(HTTPPostMethod.SendPost(this, webhookUrl, json.ToString()));
50+
}
51+
}

Code/MethodSystem/Methods/GeneralVariableMethods/GlobalVariableMethod.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,12 @@ public class GlobalVariableMethod : SynchronousMethod, ICanError
2121

2222
public override Argument[] ExpectedArguments { get; } =
2323
[
24-
new TokenArgument<VariableToken>("variable to make global")
24+
new VariableArgument("variable to make global")
2525
];
2626

2727
public override void Execute()
2828
{
29-
var variableToken = Args.GetToken<VariableToken>("variable to make global");
30-
if (variableToken.TryGetVariable().HasErrored(out var error, out var variable))
31-
{
32-
throw new ScriptRuntimeError(this, error);
33-
}
34-
29+
var variable = Args.GetVariable("variable to make global");
3530
VariableIndex.GlobalVariables.RemoveAll(existingVar => existingVar.Name == variable.Name);
3631
VariableIndex.GlobalVariables.Add(variable);
3732
}

Code/MethodSystem/Methods/HTTPMethods/HTTPPostMethod.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using Newtonsoft.Json.Linq;
44
using SER.Code.ArgumentSystem.Arguments;
55
using SER.Code.ArgumentSystem.BaseArguments;
6+
using SER.Code.Helpers;
67
using SER.Code.Helpers.Exceptions;
8+
using SER.Code.MethodSystem.BaseMethods;
79
using SER.Code.MethodSystem.BaseMethods.Synchronous;
810
using SER.Code.MethodSystem.MethodDescriptors;
911
using UnityEngine.Networking;
@@ -21,23 +23,25 @@ public class HTTPPostMethod : SynchronousMethod, ICanError
2123
new TextArgument("address"),
2224
new ReferenceArgument<JObject>("json data to post")
2325
];
24-
25-
public string[] ErrorReasons =>
26+
27+
public static string[] HttpErrorReasons { get; } =
2628
[
2729
nameof(UnityWebRequest.Result.ConnectionError),
2830
nameof(UnityWebRequest.Result.DataProcessingError),
2931
nameof(UnityWebRequest.Result.ProtocolError)
3032
];
31-
33+
34+
public string[] ErrorReasons => HttpErrorReasons;
35+
3236
public override void Execute()
3337
{
3438
var address = Args.GetText("address");
3539
var jsonData = Args.GetReference<JObject>("json data to post");
3640

37-
Timing.RunCoroutine(SendPost(address, jsonData.ToString()));
41+
Timing.RunCoroutine(SendPost(this, address, jsonData.ToString()));
3842
}
3943

40-
private IEnumerator<float> SendPost(string url, string jsonData)
44+
public static IEnumerator<float> SendPost(Method caller, string url, string jsonData)
4145
{
4246
using UnityWebRequest request = new UnityWebRequest(url, "POST");
4347
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonData);
@@ -47,10 +51,11 @@ private IEnumerator<float> SendPost(string url, string jsonData)
4751

4852
yield return Timing.WaitUntilDone(request.SendWebRequest());
4953

54+
Log.Signal($"{request.error} error");
5055
if (request.result != UnityWebRequest.Result.Success)
5156
{
5257
throw new ScriptRuntimeError(
53-
this,
58+
caller,
5459
$"Address {url} has returned {request.result} ({request.responseCode}): {request.error}"
5560
);
5661
}

Code/MethodSystem/Methods/HTTPMethods/ParseJSONMethod.cs

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)