Skip to content

Commit 9dd2288

Browse files
committed
pipe all Starscript compilation through one helper that logs if the resulting script is big (>150bytes) (and an additional message if it's very big (>500bytes))
1 parent 993ef4d commit 9dd2288

File tree

9 files changed

+43
-29
lines changed

9 files changed

+43
-29
lines changed

src/Bot/Systems/Commands/Interaction/Modules/Utility/MathCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ public Task<RuntimeResult> MathAsync(
1616
{
1717
try
1818
{
19-
var expr = expression.Replace("{", string.Empty).Replace("}", string.Empty);
19+
expression = expression.Replace("{", string.Empty).Replace("}", string.Empty);
2020

21-
var result = VolteStarscript.RunExpression(expr);
21+
var result = VolteStarscript.RunExpression(expression, Context.Guild);
2222

2323
return Ok(Context.CreateEmbedBuilder()
24-
.AddField("Input", Format.Code(expr))
24+
.AddField("Input", Format.Code(expression))
2525
.AddField("Output", Format.Code(result.ToString())),
2626
ephemeral: !publicResult
2727
);

src/Bot/Systems/Commands/Text/Modules/UserFilterModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ await Context.CreateEmbed("Are you sure you want to run the user filter on every
160160
{
161161
try
162162
{
163-
compiledEntries.Add((entry, entry.Starscript.Compile()));
163+
compiledEntries.Add((entry, entry.Starscript.Compile(Context.Guild)));
164164
}
165165
catch (ParseException)
166166
{

src/Bot/Systems/Commands/Text/Modules/Utility/MathCommand.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Starscript;
2-
using Volte.Systems.Starscript;
32

43
namespace Volte.Systems.Commands.Text.Modules;
54

@@ -11,12 +10,12 @@ public Task<ActionResult> MathAsync([Remainder, Description("The expression.")]
1110
{
1211
try
1312
{
14-
var expr = expression.Replace("{", string.Empty).Replace("}", string.Empty);
15-
16-
var result = VolteStarscript.RunExpression(expr);
13+
expression = expression.Replace("{", string.Empty).Replace("}", string.Empty);
14+
15+
var result = Context.RunStarscriptMath(expression);
1716

1817
return Ok(Context.CreateEmbedBuilder()
19-
.AddField("Input", Format.Code(expr))
18+
.AddField("Input", Format.Code(expression))
2019
.AddField("Output", Format.Code(result.ToString())));
2120
}
2221
catch (ParseException pe)

src/Bot/Systems/Commands/Text/VolteContext.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,12 @@ public void Modify(DataEditor modifier)
127127
db.Save(GuildData);
128128
}
129129

130-
public StringSegment RunStarscript(string source) => VolteStarscript.Run(source, this);
130+
public StringSegment RunStarscript(string source) => Guild.Run(source, this);
131131
public StringSegment RunStarscriptExpression(string expression)
132132
=> RunStarscript($"{{{expression.Replace("{", string.Empty).Replace("}", string.Empty)}}}");
133+
134+
public StringSegment RunStarscriptMath(string expression)
135+
=> VolteStarscript.CompileExpression(expression, Guild).Execute(VolteStarscript.MathHypervisor);
133136

134137
public ValueMap ToStarscript() => new ValueMap()
135138
.Set("message", StarscriptHelper.Wrap(Message))

src/Bot/Systems/Database/Entities/StarscriptTables/StarscriptSrc.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ public class StarscriptSrc
99

1010
public string CodeInput { get; set; }
1111

12-
public Script Compile() =>
12+
public Script Compile(IGuild guild) =>
1313
Type is StarscriptType.SingleExpression
14-
? VolteStarscript.CompileExpression(CodeInput)
15-
: VolteStarscript.Compile(CodeInput);
14+
? VolteStarscript.CompileExpression(CodeInput, guild)
15+
: VolteStarscript.Compile(CodeInput, guild);
1616

1717
public override string ToString()
1818
=> $"{{ Type: {Type}, Source: {CodeInput} }}";

src/Bot/Systems/Database/EntitiesV2/StarscriptTables/StarscriptSrc.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ public class StarscriptSrc
99

1010
public string CodeInput { get; set; }
1111

12-
public Script Compile() =>
12+
public Script Compile(IGuild guild) =>
1313
Type is StarscriptType.SingleExpression
14-
? VolteStarscript.CompileExpression(CodeInput)
15-
: VolteStarscript.Compile(CodeInput);
14+
? VolteStarscript.CompileExpression(CodeInput, guild)
15+
: VolteStarscript.Compile(CodeInput, guild);
1616

1717
public override string ToString()
1818
=> $"{{ Type: {Type}, Source: {CodeInput} }}";

src/Bot/Systems/Database/EntitiesV2/WelcomeSettings.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public async ValueTask<string> FormatJoinMessageAsync(SocketGuildUser user)
3838

3939
try
4040
{
41-
return VolteStarscript.Run(msg, await StarscriptHelper.WrapAsync(user.Guild, user)).ToString();
41+
return user.Guild.Run(msg, await StarscriptHelper.WrapAsync(user.Guild, user)).ToString();
4242
}
4343
catch
4444
{
@@ -59,7 +59,7 @@ public async ValueTask<string> FormatLeftMessageAsync(SocketGuild guild, SocketU
5959

6060
try
6161
{
62-
return VolteStarscript.Run(msg, await StarscriptHelper.WrapAsync(guild, user)).ToString();
62+
return guild.Run(msg, await StarscriptHelper.WrapAsync(guild, user)).ToString();
6363
}
6464
catch
6565
{
@@ -80,7 +80,7 @@ public async ValueTask<string> FormatJoinDmMessageAsync(SocketGuildUser user)
8080

8181
try
8282
{
83-
return VolteStarscript.Run(msg, await StarscriptHelper.WrapAsync(user.Guild, user)).ToString();
83+
return user.Guild.Run(msg, await StarscriptHelper.WrapAsync(user.Guild, user)).ToString();
8484
}
8585
catch
8686
{

src/Bot/Systems/Starscript/VolteStarscript.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,40 @@ public static bool GetBooleanValue(this StringSegment segment)
1414
public static readonly StarscriptHypervisor MathHypervisor = StarscriptHypervisor.Create().WithStandardLibraryMath();
1515

1616

17-
public static StringSegment Run(string source, IStarscriptObject environment)
18-
=> Run(source, environment.ToStarscript());
17+
public static StringSegment Run(this IGuild currentGuild, string source, IStarscriptObject environment)
18+
=> currentGuild.Run(source, environment.ToStarscript());
1919

2020
public static StringSegment Run(Script script, IStarscriptObject environment)
2121
=> Run(script, environment.ToStarscript());
2222

23-
public static StringSegment Run(string source, ValueMap environment)
24-
=> Run(Compile(source), environment);
23+
public static StringSegment Run(this IGuild currentGuild, string source, ValueMap environment)
24+
=> Run(Compile(source, currentGuild), environment);
2525

2626
public static StringSegment Run(Script script, ValueMap environment)
2727
=> Hypervisor.ReplaceLocals(environment).Run(script, environment);
2828

29-
public static StringSegment RunExpression(string expression)
30-
=> CompileExpression(expression).Execute(MathHypervisor);
29+
public static StringSegment RunExpression(string expression, IGuild guild)
30+
=> CompileExpression(expression, guild).Execute(MathHypervisor);
3131

32-
public static Script Compile(string source)
32+
public static Script Compile(string source, IGuild guild)
3333
{
3434
if (!Parser.TryParse(source, out var result))
3535
throw new ParseException(result.Errors.First());
3636

37-
return Compiler.SingleCompile(result);
37+
var script = Compiler.SingleCompile(result);
38+
39+
if (script.Code.Length > 150)
40+
{
41+
Warn(LogSource.Service,
42+
$"Large compiled Starscript (>150 bytes): {{ Guild: \"{guild.Name}\" ({guild.Id})," +
43+
$"CodeLength: {script.Code.Length}, ConstantsCount: {script.Constants.Length} }}");
44+
45+
if (script.Code.Length > 500)
46+
Warn(LogSource.Service, "It's over 500 bytes, should probably take a look!");
47+
}
48+
49+
return script;
3850
}
3951

40-
public static Script CompileExpression(string source) => Compile($"{{{source}}}");
52+
public static Script CompileExpression(string source, IGuild guild) => Compile($"{{{source}}}", guild);
4153
}

src/Bot/Systems/UserFilter/UserFilterTable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public async Task<bool> HandleAsync(IGuild guild, IGuildUser user)
5656

5757
try
5858
{
59-
using (var script = Starscript.Compile())
59+
using (var script = Starscript.Compile(guild))
6060
{
6161
returnVal = VolteStarscript.Hypervisor.Run(script, StarscriptHelper.Wrap(user));
6262
}

0 commit comments

Comments
 (0)