Skip to content

Commit 9857b31

Browse files
committed
new EditDiscordMessage method
1 parent bc792f8 commit 9857b31

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 EditDiscordMessageMethod : SynchronousMethod, ICanError
14+
{
15+
public override string Description => "Edits 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+
new ReferenceArgument<DiscordMessageMethod.DMessage>("message object")
29+
];
30+
31+
public override void Execute()
32+
{
33+
var webhookUrl = Args.GetText("webhook url");
34+
var messageId = Args.GetText("message id");
35+
var messageObject = Args.GetReference<DiscordMessageMethod.DMessage>("message object");
36+
37+
if (messageId.IsEmpty())
38+
throw new ScriptRuntimeError(this, ErrorReasons[^2]);
39+
40+
if (!webhookUrl.StartsWith("https://discord.com/api/webhooks/"))
41+
throw new ScriptRuntimeError(this, ErrorReasons.Last());
42+
43+
var messageURL = webhookUrl + "/messages/" + messageId;
44+
45+
Timing.RunCoroutine(HTTPPostMethod.RequestSend(this, messageURL, messageObject, "PATCH"));
46+
}
47+
}

0 commit comments

Comments
 (0)