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