Skip to content

Commit bb7ecff

Browse files
update discord methods
1 parent 7a9889f commit bb7ecff

File tree

13 files changed

+221
-104
lines changed

13 files changed

+221
-104
lines changed

Code/MethodSystem/BaseMethods/Method.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,6 @@ public override string ToString()
6868
? $"{Name} method in line {LineNum}"
6969
: $"{Name} method";
7070
}
71+
72+
public static string GetFriendlyName(Type type) => type.Name[..^"Method".Length];
7173
}

Code/MethodSystem/Methods/DiscordMethods/CreateDiscordEmbedAuthorMethod.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,44 @@
33
using SER.Code.ArgumentSystem.Arguments;
44
using SER.Code.ArgumentSystem.BaseArguments;
55
using SER.Code.MethodSystem.BaseMethods.Synchronous;
6-
using SER.Code.MethodSystem.Methods.DiscordMethods.Structures;
76

87
namespace SER.Code.MethodSystem.Methods.DiscordMethods;
98

109
[UsedImplicitly]
11-
public class CreateDiscordEmbedAuthorMethod : ReferenceReturningMethod<DEmbedAuthor>
10+
public class CreateDiscordEmbedAuthorMethod : ReferenceReturningMethod<CreateDiscordEmbedAuthorMethod.DEmbedAuthor>
1211
{
12+
public class DEmbedAuthor : JObject;
13+
1314
public override string Description => "Creates an author object that can be used in discord embeds.";
1415

1516
public override Argument[] ExpectedArguments { get; } =
1617
[
17-
new TextArgument("name"),
18-
new TextArgument("url")
18+
new TextArgument("name")
1919
{
20-
DefaultValue = new(null, "none")
20+
Description = "Small text at the top of the embed."
2121
},
2222
new TextArgument("icon url")
2323
{
24-
DefaultValue = new(null, "none")
24+
DefaultValue = new(null, "none"),
25+
Description = "Small round image next to the author name."
2526
},
26-
new TextArgument("proxy icon url")
27+
new TextArgument("clickable url")
2728
{
28-
DefaultValue = new(null, "none")
29-
}
29+
DefaultValue = new(null, "none"),
30+
Description = "Link that turns the author name into a hyperlink."
31+
},
3032
];
3133

3234
public override void Execute()
3335
{
34-
var author = new JObject
36+
var author = new DEmbedAuthor
3537
{
3638
["name"] = Args.GetText("name")
3739
};
3840

39-
if (Args.GetText("url") is {} url) author["url"] = url;
41+
if (Args.GetText("clickable url") is {} url) author["url"] = url;
4042
if (Args.GetText("icon url") is {} iconUrl) author["icon_url"] = iconUrl;
41-
if (Args.GetText("proxy icon url") is {} proxyIconUrl) author["proxy_icon_url"] = proxyIconUrl;
4243

43-
ReturnValue = new(author);
44+
ReturnValue = author;
4445
}
4546
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using JetBrains.Annotations;
2+
using Newtonsoft.Json.Linq;
3+
using SER.Code.ArgumentSystem.Arguments;
4+
using SER.Code.ArgumentSystem.BaseArguments;
5+
using SER.Code.MethodSystem.BaseMethods.Synchronous;
6+
7+
namespace SER.Code.MethodSystem.Methods.DiscordMethods;
8+
9+
[UsedImplicitly]
10+
public class CreateDiscordEmbedFieldMethod : ReferenceReturningMethod<CreateDiscordEmbedFieldMethod.DEmbedField>
11+
{
12+
public class DEmbedField : JObject;
13+
14+
public override string Description => "Creates a field object that can be used in discord embeds.";
15+
16+
public override Argument[] ExpectedArguments { get; } =
17+
[
18+
new TextArgument("name")
19+
{
20+
Description = "The bold \"header\" of a specific field."
21+
},
22+
new TextArgument("value")
23+
{
24+
Description = "The main text content for that specific field.",
25+
},
26+
new BoolArgument("inline")
27+
{
28+
Description = "Determines if fields sit side-by-side."
29+
}
30+
];
31+
32+
public override void Execute()
33+
{
34+
ReturnValue = new DEmbedField
35+
{
36+
["name"] = Args.GetText("name"),
37+
["value"] = Args.GetText("value"),
38+
["inline"] = Args.GetBool("inline")
39+
};
40+
}
41+
}
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
using JetBrains.Annotations;
2+
using Newtonsoft.Json.Linq;
23
using SER.Code.ArgumentSystem.Arguments;
34
using SER.Code.ArgumentSystem.BaseArguments;
45
using SER.Code.MethodSystem.BaseMethods.Synchronous;
5-
using SER.Code.MethodSystem.Methods.DiscordMethods.Structures;
66

77
namespace SER.Code.MethodSystem.Methods.DiscordMethods;
88

99
[UsedImplicitly]
10-
public class CreateDiscordEmbedFooterMethod : ReferenceReturningMethod<DEmbedFooter>
10+
public class CreateDiscordEmbedFooterMethod : ReferenceReturningMethod<CreateDiscordEmbedFooterMethod.DEmbedFooter>
1111
{
12+
public class DEmbedFooter : JObject;
13+
1214
public override string Description => "Creates a footer that can be used in discord embeds.";
1315

1416
public override Argument[] ExpectedArguments { get; } =
1517
[
16-
new TextArgument("content"),
18+
new TextArgument("content")
19+
{
20+
Description = "Small text at the bottom of the embed."
21+
},
1722
new TextArgument("icon url")
23+
{
24+
Description = "Small square image next to the footer text."
25+
}
1826
];
1927

2028
public override void Execute()
2129
{
22-
ReturnValue = new(new()
30+
ReturnValue = new()
2331
{
2432
["text"] = Args.GetText("content"),
2533
["icon_url"] = Args.GetText("icon url")
26-
});
34+
};
2735
}
2836
}

Code/MethodSystem/Methods/DiscordMethods/CreateDiscordEmbedMethod.cs

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,107 @@
22
using Newtonsoft.Json.Linq;
33
using SER.Code.ArgumentSystem.Arguments;
44
using SER.Code.ArgumentSystem.BaseArguments;
5+
using SER.Code.Helpers;
56
using SER.Code.MethodSystem.BaseMethods.Synchronous;
6-
using SER.Code.MethodSystem.Methods.DiscordMethods.Structures;
7+
using SER.Code.MethodSystem.MethodDescriptors;
78
using UnityEngine;
89

910
namespace SER.Code.MethodSystem.Methods.DiscordMethods;
1011

1112
[UsedImplicitly]
12-
public class CreateDiscordEmbedMethod : ReferenceReturningMethod<DEmbed>
13+
public class CreateDiscordEmbedMethod : ReferenceReturningMethod<CreateDiscordEmbedMethod.DEmbed>, IAdditionalDescription
1314
{
15+
public class DEmbed : JObject;
16+
1417
public override string Description => "Creates an embed which can later be sent to discord via webhook.";
1518

19+
public string AdditionalDescription =>
20+
$"This method does NOT send the embed. Use {GetFriendlyName(typeof(SendDiscordMessageMethod))} for that.";
21+
1622
public override Argument[] ExpectedArguments { get; } =
1723
[
1824
new TextArgument("title")
1925
{
26+
Description = "The bold header text at the top.",
2027
DefaultValue = new(null, "none")
2128
},
2229
new TextArgument("description")
2330
{
24-
DefaultValue = new(null, "none")
31+
DefaultValue = new(null, "none"),
32+
Description = "The main body text of the embed."
2533
},
2634
new ColorArgument("color")
2735
{
28-
DefaultValue = new(new Color(0, 0, 0, 1), "default")
36+
DefaultValue = new(new Color(0, 0, 0, 0), "none"),
37+
Description = "The vertical sidebar color."
2938
},
30-
new ReferenceArgument<DEmbedFooter>("footer")
39+
new ReferenceArgument<CreateDiscordEmbedAuthorMethod.DEmbedAuthor>("author")
3140
{
32-
DefaultValue = new(null, "none")
41+
DefaultValue = new(null, "none"),
42+
Description = $"Created using {GetFriendlyName(typeof(CreateDiscordEmbedAuthorMethod))}"
43+
},
44+
new ReferenceArgument<CreateDiscordEmbedFooterMethod.DEmbedFooter>("footer")
45+
{
46+
DefaultValue = new(null, "none"),
47+
Description = $"Created using {GetFriendlyName(typeof(CreateDiscordEmbedFooterMethod))}"
48+
},
49+
new TextArgument("thumbnail url")
50+
{
51+
DefaultValue = new(null, "none"),
52+
Description = "The source link for the thumbnail."
53+
},
54+
new TextArgument("image url")
55+
{
56+
DefaultValue = new(null, "none"),
57+
Description = "The source link for the image."
58+
},
59+
new TextArgument("clickable url")
60+
{
61+
DefaultValue = new(null, "none"),
62+
Description = "Makes the title a clickable hyperlink."
63+
},
64+
new ReferenceArgument<CreateDiscordEmbedFieldMethod.DEmbedField>("fields")
65+
{
66+
DefaultValue = new(Array.Empty<CreateDiscordEmbedFieldMethod.DEmbedField>(), "none"),
67+
ConsumesRemainingValues = true,
68+
Description = $"List of fields, created using {GetFriendlyName(typeof(CreateDiscordEmbedFieldMethod))}"
3369
}
3470
];
35-
71+
3672
public override void Execute()
3773
{
38-
var embed = new JObject();
74+
var embed = new DEmbed();
3975

4076
if (Args.GetText("title") is { } title) embed["title"] = title;
77+
4178
if (Args.GetText("description") is { } description) embed["description"] = description;
42-
if (Args.GetColor("color") is { a: < 1 } color)
79+
80+
if (Args.GetColor("color") is { a: > 0 } color)
4381
{
4482
int r = Mathf.RoundToInt(color.r * 255);
4583
int g = Mathf.RoundToInt(color.g * 255);
4684
int b = Mathf.RoundToInt(color.b * 255);
4785

4886
embed["color"] = (r << 16) | (g << 8) | b;
4987
}
50-
if (Args.GetReference<DEmbedFooter>("footer") is { } footer) embed["footer"] = footer.Footer;
88+
89+
if (Args.GetReference<CreateDiscordEmbedAuthorMethod.DEmbedAuthor>("author") is { } author) embed["author"] = author;
90+
91+
if (Args.GetReference<CreateDiscordEmbedFooterMethod.DEmbedFooter>("footer") is { } footer) embed["footer"] = footer;
5192

52-
ReturnValue = new(embed);
93+
if (Args.GetText("thumbnail url") is { } thumbnailUrl) embed["thumbnail"] = new JObject {{"url", thumbnailUrl}};
94+
95+
if (Args.GetText("image url") is { } imageUrl) embed["image"] = new JObject {{"url", imageUrl}};
96+
97+
if (Args.GetText("clickable url") is { } url) embed["url"] = url;
98+
99+
var fields = Args.GetRemainingArguments<CreateDiscordEmbedFieldMethod.DEmbedField, ReferenceArgument<CreateDiscordEmbedFieldMethod.DEmbedField>>("fields");
100+
Log.Signal(fields.Length);
101+
if (fields is { Length: > 0 })
102+
{
103+
embed["fields"] = JArray.FromObject(fields);
104+
}
105+
106+
ReturnValue = embed;
53107
}
54108
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using JetBrains.Annotations;
2+
using Newtonsoft.Json.Linq;
3+
using SER.Code.ArgumentSystem.Arguments;
4+
using SER.Code.ArgumentSystem.BaseArguments;
5+
using SER.Code.Helpers.Exceptions;
6+
using SER.Code.Helpers.Extensions;
7+
using SER.Code.MethodSystem.BaseMethods.Synchronous;
8+
using SER.Code.MethodSystem.MethodDescriptors;
9+
10+
namespace SER.Code.MethodSystem.Methods.DiscordMethods;
11+
12+
[UsedImplicitly]
13+
public class CreateDiscordMessageMethod : ReferenceReturningMethod<CreateDiscordMessageMethod.DMessage>, IAdditionalDescription, ICanError
14+
{
15+
public class DMessage : JObject;
16+
17+
public override string Description => "Creates a discord message object.";
18+
19+
public string AdditionalDescription =>
20+
$"This method does NOT send the message. Use {GetFriendlyName(typeof(SendDiscordMessageMethod))} for that.";
21+
22+
public string[] ErrorReasons { get; } =
23+
[
24+
"Provided more than 10 embeds. Discord only allows 10 embeds per message."
25+
];
26+
27+
public override Argument[] ExpectedArguments { get; } =
28+
[
29+
new TextArgument("message content")
30+
{
31+
Description = "The main message text (up to 2000 characters)",
32+
DefaultValue = new(null, "empty")
33+
},
34+
new TextArgument("sender name")
35+
{
36+
Description = "Overrides the webhook's default display name.",
37+
DefaultValue = new(null, "default")
38+
},
39+
new TextArgument("sender avatar url")
40+
{
41+
Description = "Overrides the webhook's default profile picture.",
42+
DefaultValue = new(null, "default")
43+
},
44+
new ReferenceArgument<CreateDiscordEmbedMethod.DEmbed>("embeds")
45+
{
46+
Description = "An list containing up to 10 rich embed objects.",
47+
// i dont know if we can use both at the same time lmao - andrzej
48+
DefaultValue = new(Array.Empty<CreateDiscordEmbedMethod.DEmbed>(), "none"),
49+
ConsumesRemainingValues = true
50+
}
51+
];
52+
53+
public override void Execute()
54+
{
55+
var messageContent = Args.GetText("message content").MaybeNull();
56+
var webhookName = Args.GetText("sender name").MaybeNull();
57+
var avatarUrl = Args.GetText("sender avatar url").MaybeNull();
58+
var embeds = Args.GetRemainingArguments<CreateDiscordEmbedMethod.DEmbed, ReferenceArgument<CreateDiscordEmbedMethod.DEmbed>>("embeds");
59+
60+
DMessage json = new();
61+
62+
if (messageContent != null) json["content"] = messageContent;
63+
if (webhookName != null) json["username"] = webhookName;
64+
if (avatarUrl != null) json["avatar_url"] = avatarUrl;
65+
if (embeds.Any())
66+
{
67+
if (embeds.Length > 10)
68+
{
69+
throw new ScriptRuntimeError(this, ErrorReasons[0]);
70+
}
71+
72+
json["embeds"] = JArray.FromObject(embeds);
73+
}
74+
75+
ReturnValue = json;
76+
}
77+
}

Code/MethodSystem/Methods/DiscordMethods/SendDiscordEmbedsMethod.cs

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

0 commit comments

Comments
 (0)