timely now has an option in gambling whether to use no protection, captcha, or button.

grpc api fix
This commit is contained in:
Toastie (DCS Team) 2024-11-05 20:38:37 +13:00
parent c5aeb43046
commit e7cfd3a752
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
5 changed files with 89 additions and 16 deletions

View file

@ -14,6 +14,13 @@ using System.Text;
using EllieBot.Modules.Gambling.Rps; using EllieBot.Modules.Gambling.Rps;
using EllieBot.Common.TypeReaders; using EllieBot.Common.TypeReaders;
using EllieBot.Modules.Patronage; using EllieBot.Modules.Patronage;
using SixLabors.Fonts;
using SixLabors.Fonts.Unicode;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using Color = SixLabors.ImageSharp.Color;
namespace EllieBot.Modules.Gambling; namespace EllieBot.Modules.Gambling;
@ -211,6 +218,7 @@ public partial class Gambling : GamblingModule<GamblingService>
customId: "timely:" + _rng.Next(123456, 999999)), customId: "timely:" + _rng.Next(123456, 999999)),
async (smc) => async (smc) =>
{ {
await smc.DeferAsync();
await ClaimTimely(); await ClaimTimely();
}); });
@ -226,13 +234,65 @@ public partial class Gambling : GamblingModule<GamblingService>
return; return;
} }
if (Config.Timely.HasButton) if (Config.Timely.ProtType == TimelyProt.Button)
{ {
var interaction = CreateTimelyInteraction(); var interaction = CreateTimelyInteraction();
var msg = await Response().Pending(strs.timely_button).Interaction(interaction).SendAsync(); var msg = await Response().Pending(strs.timely_button).Interaction(interaction).SendAsync();
await msg.DeleteAsync(); await msg.DeleteAsync();
return; return;
} }
else if (Config.Timely.ProtType == TimelyProt.Captcha)
{
var password = _service.GeneratePassword();
var img = new Image<Rgba32>(70, 35);
var font = _fonts.NotoSans.CreateFont(30);
var outlinePen = new SolidPen(Color.Black, 1f);
var strikeoutRun = new RichTextRun
{
Start = 0,
End = password.GetGraphemeCount(),
Font = font,
StrikeoutPen = new SolidPen(Color.White, 3),
TextDecorations = TextDecorations.Strikeout
};
// draw password on the image
img.Mutate(x =>
{
x.DrawText(new RichTextOptions(font)
{
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
FallbackFontFamilies = _fonts.FallBackFonts,
Origin = new(35, 17),
TextRuns = [strikeoutRun]
},
password,
Brushes.Solid(Color.White),
outlinePen);
});
using var stream = await img.ToStreamAsync();
var captcha = await Response()
// .Embed(_sender.CreateEmbed()
// .WithOkColor()
// .WithImageUrl("attachment://timely.png"))
.File(stream, "timely.png")
.SendAsync();
try
{
var userInput = await GetUserInputAsync(ctx.User.Id, ctx.Channel.Id);
if (userInput?.ToLowerInvariant() != password?.ToLowerInvariant())
{
return;
}
}
finally
{
_ = captcha.DeleteAsync();
}
}
await ClaimTimely(); await ClaimTimely();
} }

View file

@ -11,7 +11,7 @@ namespace EllieBot.Modules.Gambling.Common;
public sealed partial class GamblingConfig : ICloneable<GamblingConfig> public sealed partial class GamblingConfig : ICloneable<GamblingConfig>
{ {
[Comment("""DO NOT CHANGE""")] [Comment("""DO NOT CHANGE""")]
public int Version { get; set; } = 10; public int Version { get; set; } = 11;
[Comment("""Currency settings""")] [Comment("""Currency settings""")]
public CurrencyConfig Currency { get; set; } public CurrencyConfig Currency { get; set; }
@ -119,9 +119,17 @@ public partial class TimelyConfig
public int Cooldown { get; set; } = 24; public int Cooldown { get; set; } = 24;
[Comment(""" [Comment("""
Whether the users are required to type a password when they do timely. How will timely be protected?
None, Button (users have to click the button) or Captcha (users have to type the captcha from an image)
""")] """)]
public bool HasButton { get; set; } = true; public TimelyProt ProtType { get; set; } = TimelyProt.Button;
}
public enum TimelyProt
{
None,
Button,
Captcha
} }
[Cloneable] [Cloneable]

View file

@ -144,9 +144,9 @@ public sealed class GamblingConfigService : ConfigServiceBase<GamblingConfig>
ConfigPrinters.ToString, ConfigPrinters.ToString,
val => val >= 0); val => val >= 0);
AddParsedProp("timely.btn", AddParsedProp("timely.prot",
gs => gs.Timely.HasButton, gs => gs.Timely.ProtType,
bool.TryParse, ConfigParsers.InsensitiveEnum,
ConfigPrinters.ToString); ConfigPrinters.ToString);
Migrate(); Migrate();
@ -189,11 +189,11 @@ public sealed class GamblingConfigService : ConfigServiceBase<GamblingConfig>
}); });
} }
if (data.Version < 10) if (data.Version < 11)
{ {
ModifyConfig(c => ModifyConfig(c =>
{ {
c.Version = 10; c.Version = 11;
}); });
} }
} }

View file

@ -17,10 +17,13 @@ public sealed class GreetByeSvc : GrpcGreet.GrpcGreetBase, IGrpcSvc, IEService
public ServerServiceDefinition Bind() public ServerServiceDefinition Bind()
=> GrpcGreet.BindService(this); => GrpcGreet.BindService(this);
private static GrpcGreetSettings ToConf(GreetSettings? conf) private static GrpcGreetSettings ToConf(GreetSettings? conf, GreetType type)
{ {
if (conf is null) if (conf is null)
return new GrpcGreetSettings(); return new GrpcGreetSettings()
{
Type = (GrpcGreetType)type
};
return new GrpcGreetSettings() return new GrpcGreetSettings()
{ {
@ -35,9 +38,10 @@ public sealed class GreetByeSvc : GrpcGreet.GrpcGreetBase, IGrpcSvc, IEService
{ {
var guildId = request.GuildId; var guildId = request.GuildId;
var conf = await _gs.GetGreetSettingsAsync(guildId, (GreetType)request.Type); var type = (GreetType)request.Type;
var conf = await _gs.GetGreetSettingsAsync(guildId, type);
return ToConf(conf); return ToConf(conf, type);
} }
public override async Task<UpdateGreetReply> UpdateGreet(UpdateGreetRequest request, ServerCallContext context) public override async Task<UpdateGreetReply> UpdateGreet(UpdateGreetRequest request, ServerCallContext context)

View file

@ -1,5 +1,5 @@
# DO NOT CHANGE # DO NOT CHANGE
version: 10 version: 11
# Currency settings # Currency settings
currency: currency:
# What is the emoji/character which represents the currency # What is the emoji/character which represents the currency
@ -56,8 +56,9 @@ timely:
# How often (in hours) can users claim currency with .timely command # How often (in hours) can users claim currency with .timely command
# setting to 0 or less will disable this feature # setting to 0 or less will disable this feature
cooldown: 12 cooldown: 12
# Whether the users are required to type a password when they do timely. # How will timely be protected?
hasButton: true # None, Button (users have to click the button) or Captcha (users have to type the captcha from an image)
protType: Button
# How much will each user's owned currency decay over time. # How much will each user's owned currency decay over time.
decay: decay:
# Percentage of user's current currency which will be deducted every 24h. # Percentage of user's current currency which will be deducted every 24h.