Skip to content

Commit ecbedc4

Browse files
2 parents 71c6173 + f2f1650 commit ecbedc4

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using JetBrains.Annotations;
2+
using MEC;
3+
using SER.Code.ArgumentSystem.Arguments;
4+
using SER.Code.ArgumentSystem.BaseArguments;
5+
using SER.Code.Helpers.Exceptions;
6+
using SER.Code.MethodSystem.BaseMethods.Synchronous;
7+
using SER.Code.MethodSystem.MethodDescriptors;
8+
using SER.Code.MethodSystem.Methods.HTTPMethods;
9+
10+
namespace SER.Code.MethodSystem.Methods.DiscordMethods;
11+
12+
[UsedImplicitly]
13+
public class DeleteDiscordMessageMethod : SynchronousMethod, ICanError
14+
{
15+
public override string Description => "Deletes a message sent by a discord webhook (with that same webhook).";
16+
17+
public string[] ErrorReasons =>
18+
[
19+
..HTTPPostMethod.HttpErrorReasons,
20+
"Message ID must not be empty.",
21+
"Provided URL is not a valid webhook URL."
22+
];
23+
24+
public override Argument[] ExpectedArguments { get; } =
25+
[
26+
new TextArgument("webhook url"),
27+
new TextArgument("message id")
28+
{
29+
Description = "You can get it by right-clicking on a message and clicking \"Copy message ID\""
30+
}
31+
];
32+
33+
public override void Execute()
34+
{
35+
var webhookUrl = Args.GetText("webhook url");
36+
var messageId = Args.GetText("message id");
37+
38+
if (messageId.IsEmpty())
39+
throw new ScriptRuntimeError(this, ErrorReasons[^2]);
40+
41+
if (!webhookUrl.StartsWith("https://discord.com/api/webhooks/"))
42+
throw new ScriptRuntimeError(this, ErrorReasons.Last());
43+
44+
var messageURL = webhookUrl + "/messages/" + messageId;
45+
46+
Timing.RunCoroutine(HTTPPostMethod.RequestSend(this, messageURL, null, "DELETE"));
47+
}
48+
}

Code/MethodSystem/Methods/DiscordMethods/EditDiscordMessageMethod.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ public class EditDiscordMessageMethod : SynchronousMethod, ICanError
2424
public override Argument[] ExpectedArguments { get; } =
2525
[
2626
new TextArgument("webhook url"),
27-
new TextArgument("message id"),
27+
new TextArgument("message id")
28+
{
29+
Description = "You can get it by right-clicking on a message and clicking \"Copy message ID\""
30+
},
2831
new ReferenceArgument<DiscordMessageMethod.DMessage>("message object")
2932
];
3033

Code/MethodSystem/Methods/HTTPMethods/HTTPPostMethod.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,16 @@ public override void Execute()
4040
Timing.RunCoroutine(RequestSend(this, address, jsonData));
4141
}
4242

43-
public static IEnumerator<float> RequestSend(Method caller, string url, JObject jsonData, string method = "POST")
43+
public static IEnumerator<float> RequestSend(Method caller, string url, JObject? jsonData, string method = "POST")
4444
{
45-
using UnityWebRequest request = new UnityWebRequest(url, method);
46-
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonData.ToString());
47-
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
48-
request.downloadHandler = new DownloadHandlerBuffer();
49-
request.SetRequestHeader("Content-Type", "application/json");
45+
using var request = new UnityWebRequest(url, method);
46+
47+
if (jsonData is not null)
48+
{
49+
var bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonData.ToString());
50+
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
51+
request.SetRequestHeader("Content-Type", "application/json");
52+
}
5053

5154
yield return Timing.WaitUntilDone(request.SendWebRequest());
5255

0 commit comments

Comments
 (0)