added crown emoji next to commands if -v 1 or -v2 option is specified

cleanup hangman a little
This commit is contained in:
Toastie 2025-03-15 11:13:52 +13:00
parent b9b53ef3e8
commit 73db4e59c2
Signed by: toastie_t0ast
GPG key ID: 0861BE54AD481DC7
2 changed files with 167 additions and 154 deletions
src/EllieBot/Modules
Games/Hangman
Help

View file

@ -52,11 +52,8 @@ public sealed class HangmanService : IHangmanService, IExecNoCommand
public ValueTask<bool> StopHangman(ulong channelId) public ValueTask<bool> StopHangman(ulong channelId)
{ {
lock (_locker) if (_hangmanGames.TryRemove(channelId, out _))
{ return new(true);
if (_hangmanGames.TryRemove(channelId, out _))
return new(true);
}
return new(false); return new(false);
} }
@ -66,48 +63,50 @@ public sealed class HangmanService : IHangmanService, IExecNoCommand
public async Task ExecOnNoCommandAsync(IGuild guild, IUserMessage msg) public async Task ExecOnNoCommandAsync(IGuild guild, IUserMessage msg)
{ {
if (_hangmanGames.ContainsKey(msg.Channel.Id)) if (!_hangmanGames.ContainsKey(msg.Channel.Id))
{ {
if (string.IsNullOrWhiteSpace(msg.Content)) return;
}
if (string.IsNullOrWhiteSpace(msg.Content))
return;
if (_cdCache.TryGetValue(msg.Author.Id, out _))
return;
HangmanGame.State state;
long rew = 0;
lock (_locker)
{
if (!_hangmanGames.TryGetValue(msg.Channel.Id, out var game))
return; return;
if (_cdCache.TryGetValue(msg.Author.Id, out _)) state = game.Guess(msg.Content.ToLowerInvariant());
if (state.GuessResult == HangmanGame.GuessResult.NoAction)
return; return;
HangmanGame.State state; if (state.GuessResult is HangmanGame.GuessResult.Incorrect or HangmanGame.GuessResult.AlreadyTried)
long rew = 0;
lock (_locker)
{ {
if (!_hangmanGames.TryGetValue(msg.Channel.Id, out var game)) _cdCache.Set(msg.Author.Id,
return; string.Empty,
new MemoryCacheEntryOptions
state = game.Guess(msg.Content.ToLowerInvariant()); {
AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(3)
if (state.GuessResult == HangmanGame.GuessResult.NoAction) });
return;
if (state.GuessResult is HangmanGame.GuessResult.Incorrect or HangmanGame.GuessResult.AlreadyTried)
{
_cdCache.Set(msg.Author.Id,
string.Empty,
new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(3)
});
}
if (state.Phase == HangmanGame.Phase.Ended)
{
if (_hangmanGames.TryRemove(msg.Channel.Id, out _))
rew = _gcs.Data.Hangman.CurrencyReward;
}
} }
if (rew > 0) if (state.Phase == HangmanGame.Phase.Ended)
await _cs.AddAsync(msg.Author, rew, new("hangman", "win")); {
if (_hangmanGames.TryRemove(msg.Channel.Id, out _))
await SendState((ITextChannel)msg.Channel, msg.Author, msg.Content, state); rew = _gcs.Data.Hangman.CurrencyReward;
}
} }
if (rew > 0)
await _cs.AddAsync(msg.Author, rew, new("hangman", "win"));
await SendState((ITextChannel)msg.Channel, msg.Author, msg.Content, state);
} }
private Task<IUserMessage> SendState( private Task<IUserMessage> SendState(

View file

@ -53,10 +53,10 @@ public sealed partial class Help : EllieModule<HelpService>
var clientId = await _lazyClientId.Value; var clientId = await _lazyClientId.Value;
var repCtx = new ReplacementContext(Context) var repCtx = new ReplacementContext(Context)
.WithOverride("{0}", () => clientId.ToString()) .WithOverride("{0}", () => clientId.ToString())
.WithOverride("{1}", () => prefix) .WithOverride("{1}", () => prefix)
.WithOverride("%prefix%", () => prefix) .WithOverride("%prefix%", () => prefix)
.WithOverride("%bot.prefix%", () => prefix); .WithOverride("%bot.prefix%", () => prefix);
var text = SmartText.CreateFrom(botSettings.HelpText); var text = SmartText.CreateFrom(botSettings.HelpText);
return await repSvc.ReplaceAsync(text, repCtx); return await repSvc.ReplaceAsync(text, repCtx);
@ -87,8 +87,8 @@ public sealed partial class Help : EllieModule<HelpService>
} }
var menu = new SelectMenuBuilder() var menu = new SelectMenuBuilder()
.WithPlaceholder("Select a module to see its commands") .WithPlaceholder("Select a module to see its commands")
.WithCustomId("cmds:modules_select"); .WithCustomId("cmds:modules_select");
foreach (var m in topLevelModules) foreach (var m in topLevelModules)
menu.AddOption(m.Name, m.Name, GetModuleEmoji(m.Name)); menu.AddOption(m.Name, m.Name, GetModuleEmoji(m.Name));
@ -106,33 +106,33 @@ public sealed partial class Help : EllieModule<HelpService>
}); });
await Response() await Response()
.Paginated() .Paginated()
.Items(topLevelModules) .Items(topLevelModules)
.PageSize(12) .PageSize(12)
.CurrentPage(page) .CurrentPage(page)
.Interaction(inter) .Interaction(inter)
.AddFooter(false) .AddFooter(false)
.Page((items, _) => .Page((items, _) =>
{ {
var embed = CreateEmbed().WithOkColor().WithTitle(GetText(strs.list_of_modules)); var embed = CreateEmbed().WithOkColor().WithTitle(GetText(strs.list_of_modules));
if (!items.Any()) if (!items.Any())
{ {
embed = embed.WithOkColor().WithDescription(GetText(strs.module_page_empty)); embed = embed.WithOkColor().WithDescription(GetText(strs.module_page_empty));
return embed; return embed;
} }
items items
.ToList() .ToList()
.ForEach(module => embed.AddField($"{GetModuleEmoji(module.Name)} {module.Name}", .ForEach(module => embed.AddField($"{GetModuleEmoji(module.Name)} {module.Name}",
GetModuleDescription(module.Name) GetModuleDescription(module.Name)
+ "\n" + "\n"
+ Format.Code(GetText(strs.module_footer(prefix, module.Name.ToLowerInvariant()))), + Format.Code(GetText(strs.module_footer(prefix, module.Name.ToLowerInvariant()))),
true)); true));
return embed; return embed;
}) })
.SendAsync(); .SendAsync();
} }
private string GetModuleDescription(string moduleName) private string GetModuleDescription(string moduleName)
@ -142,11 +142,11 @@ public sealed partial class Help : EllieModule<HelpService>
if (key.Key == strs.module_description_missing.Key) if (key.Key == strs.module_description_missing.Key)
{ {
var desc = _marmalades var desc = _marmalades
.GetLoadedMarmalades(Culture) .GetLoadedMarmalades(Culture)
.FirstOrDefault(m => m.Canaries .FirstOrDefault(m => m.Canaries
.Any(x => x.Name.Equals(moduleName, .Any(x => x.Name.Equals(moduleName,
StringComparison.InvariantCultureIgnoreCase))) StringComparison.InvariantCultureIgnoreCase)))
?.Description; ?.Description;
if (desc is not null) if (desc is not null)
return desc; return desc;
@ -238,18 +238,18 @@ public sealed partial class Help : EllieModule<HelpService>
var allowed = new List<CommandInfo>(); var allowed = new List<CommandInfo>();
var mdls = _cmds.Commands var mdls = _cmds.Commands
.Where(c => c.Module.GetTopLevelModule() .Where(c => c.Module.GetTopLevelModule()
.Name .Name
.StartsWith(module, StringComparison.InvariantCultureIgnoreCase)) .StartsWith(module, StringComparison.InvariantCultureIgnoreCase))
.ToArray(); .ToArray();
if (mdls.Length == 0) if (mdls.Length == 0)
{ {
var group = _cmds.Modules var group = _cmds.Modules
.Where(x => x.Parent is not null) .Where(x => x.Parent is not null)
.FirstOrDefault(x => string.Equals(x.Name.Replace("Commands", ""), .FirstOrDefault(x => string.Equals(x.Name.Replace("Commands", ""),
module, module,
StringComparison.InvariantCultureIgnoreCase)); StringComparison.InvariantCultureIgnoreCase));
if (group is not null) if (group is not null)
{ {
@ -272,8 +272,8 @@ public sealed partial class Help : EllieModule<HelpService>
var cmds = allowed.OrderBy(c => c.Aliases[0]) var cmds = allowed.OrderBy(c => c.Aliases[0])
.DistinctBy(x => x.Aliases[0]) .DistinctBy(x => x.Aliases[0])
.ToList(); .ToList();
// check preconditions for all commands, but only if it's not 'all' // check preconditions for all commands, but only if it's not 'all'
@ -284,12 +284,12 @@ public sealed partial class Help : EllieModule<HelpService>
succ = succ =
[ [
..(await cmds.Select(async x => ..(await cmds.Select(async x =>
{ {
var pre = await x.CheckPreconditionsAsync(Context, _services); var pre = await x.CheckPreconditionsAsync(Context, _services);
return (Cmd: x, Succ: pre.IsSuccess); return (Cmd: x, Succ: pre.IsSuccess);
}) })
.WhenAll()).Where(x => x.Succ) .WhenAll()).Where(x => x.Succ)
.Select(x => x.Cmd) .Select(x => x.Cmd)
]; ];
if (opts.View == CommandsOptions.ViewType.Hide) if (opts.View == CommandsOptions.ViewType.Hide)
@ -298,8 +298,8 @@ public sealed partial class Help : EllieModule<HelpService>
} }
var cmdsWithGroup = cmds.GroupBy(c => c.Module.GetGroupName()) var cmdsWithGroup = cmds.GroupBy(c => c.Module.GetGroupName())
.OrderBy(x => x.Key == x.First().Module.Name ? int.MaxValue : x.Count()) .OrderBy(x => x.Key == x.First().Module.Name ? int.MaxValue : x.Count())
.ToList(); .ToList();
if (cmdsWithGroup.Count == 0) if (cmdsWithGroup.Count == 0)
{ {
@ -311,8 +311,8 @@ public sealed partial class Help : EllieModule<HelpService>
} }
var sb = new SelectMenuBuilder() var sb = new SelectMenuBuilder()
.WithCustomId("cmds:submodule_select") .WithCustomId("cmds:submodule_select")
.WithPlaceholder("Select a submodule to see detailed commands"); .WithPlaceholder("Select a submodule to see detailed commands");
var groups = cmdsWithGroup.ToArray(); var groups = cmdsWithGroup.ToArray();
var embed = CreateEmbed().WithOkColor(); var embed = CreateEmbed().WithOkColor();
@ -322,17 +322,29 @@ public sealed partial class Help : EllieModule<HelpService>
var transformed = g var transformed = g
.Select(x => .Select(x =>
{ {
var prepend = string.Empty;
var isOwnerOnly = x.Preconditions.Any(x => x is OwnerOnlyAttribute);
if (isOwnerOnly)
prepend = " \\👑";
//if cross is specified, and the command doesn't satisfy the requirements, cross it out //if cross is specified, and the command doesn't satisfy the requirements, cross it out
if (opts.View == CommandsOptions.ViewType.Cross) if (opts.View == CommandsOptions.ViewType.Cross)
{ {
return $"{(succ.Contains(x) ? "" : "")} {prefix + x.Aliases[0]}"; var outp = $"{(succ.Contains(x) ? "" : "")} {prefix + x.Aliases[0]}";
return prepend + outp;
} }
var output = prefix + x.Aliases[0];
if (x.Aliases.Count == 1) if (x.Aliases.Count > 1)
return prefix + x.Aliases[0]; output += " | " + prefix + x.Aliases[1];
return prefix + x.Aliases[0] + " | " + prefix + x.Aliases[1]; if (opts.View == CommandsOptions.ViewType.All)
return prepend + output;
return output;
}); });
embed.AddField(g.Key, "" + string.Join("\n", transformed) + "", true); embed.AddField(g.Key, "" + string.Join("\n", transformed) + "", true);
@ -347,7 +359,9 @@ public sealed partial class Help : EllieModule<HelpService>
{ {
var groupName = smc.Data.Values.FirstOrDefault(); var groupName = smc.Data.Values.FirstOrDefault();
var mdl = _cmds.Modules.FirstOrDefault(x var mdl = _cmds.Modules.FirstOrDefault(x
=> string.Equals(x.Name.Replace("Commands", ""), groupName, StringComparison.InvariantCultureIgnoreCase)); => string.Equals(x.Name.Replace("Commands", ""),
groupName,
StringComparison.InvariantCultureIgnoreCase));
await smc.DeferAsync(); await smc.DeferAsync();
await Group(mdl); await Group(mdl);
} }
@ -359,8 +373,8 @@ public sealed partial class Help : EllieModule<HelpService>
private async Task Group(ModuleInfo group) private async Task Group(ModuleInfo group)
{ {
var menu = new SelectMenuBuilder() var menu = new SelectMenuBuilder()
.WithCustomId("cmds:group_select") .WithCustomId("cmds:group_select")
.WithPlaceholder("Select a command to see its details"); .WithPlaceholder("Select a command to see its details");
foreach (var cmd in group.Commands.DistinctBy(x => x.Aliases[0])) foreach (var cmd in group.Commands.DistinctBy(x => x.Aliases[0]))
{ {
@ -377,30 +391,30 @@ public sealed partial class Help : EllieModule<HelpService>
}); });
await Response() await Response()
.Paginated() .Paginated()
.Items(group.Commands.DistinctBy(x => x.Aliases[0]).ToArray()) .Items(group.Commands.DistinctBy(x => x.Aliases[0]).ToArray())
.PageSize(25) .PageSize(25)
.Interaction(inter) .Interaction(inter)
.Page((items, _) => .Page((items, _) =>
{ {
var eb = CreateEmbed() var eb = CreateEmbed()
.WithTitle(GetText(strs.cmd_group_commands(group.Name))) .WithTitle(GetText(strs.cmd_group_commands(group.Name)))
.WithOkColor(); .WithOkColor();
foreach (var cmd in items) foreach (var cmd in items)
{ {
string cmdName; string cmdName;
if (cmd.Aliases.Count > 1) if (cmd.Aliases.Count > 1)
cmdName = Format.Code(prefix + cmd.Aliases[0]) + " | " + Format.Code(prefix + cmd.Aliases[1]); cmdName = Format.Code(prefix + cmd.Aliases[0]) + " | " + Format.Code(prefix + cmd.Aliases[1]);
else else
cmdName = Format.Code(prefix + cmd.Aliases.First()); cmdName = Format.Code(prefix + cmd.Aliases.First());
eb.AddField(cmdName, cmd.RealSummary(_strings, _marmalades, Culture, prefix)); eb.AddField(cmdName, cmd.RealSummary(_strings, _marmalades, Culture, prefix));
} }
return eb; return eb;
}) })
.SendAsync(); .SendAsync();
} }
[Cmd] [Cmd]
@ -419,10 +433,10 @@ public sealed partial class Help : EllieModule<HelpService>
fail = fail.Substring(prefix.Length); fail = fail.Substring(prefix.Length);
var group = _cmds.Modules var group = _cmds.Modules
.SelectMany(x => x.Submodules) .SelectMany(x => x.Submodules)
.FirstOrDefault(x => string.Equals(x.Group, .FirstOrDefault(x => string.Equals(x.Group,
fail, fail,
StringComparison.InvariantCultureIgnoreCase)); StringComparison.InvariantCultureIgnoreCase));
if (group is not null) if (group is not null)
{ {
@ -477,29 +491,29 @@ public sealed partial class Help : EllieModule<HelpService>
// order commands by top level module name // order commands by top level module name
// and make a dictionary of <ModuleName, Array<JsonCommandData>> // and make a dictionary of <ModuleName, Array<JsonCommandData>>
var cmdData = _cmds.Commands.GroupBy(x => x.Module.GetTopLevelModule().Name) var cmdData = _cmds.Commands.GroupBy(x => x.Module.GetTopLevelModule().Name)
.OrderBy(x => x.Key) .OrderBy(x => x.Key)
.ToDictionary(x => x.Key, .ToDictionary(x => x.Key,
x => x.DistinctBy(c => c.Aliases.First()) x => x.DistinctBy(c => c.Aliases.First())
.Select(com => .Select(com =>
{ {
List<string> optHelpStr = null; List<string> optHelpStr = null;
var opt = CommandsUtilityService.GetEllieOptionType(com.Attributes); var opt = CommandsUtilityService.GetEllieOptionType(com.Attributes);
if (opt is not null) if (opt is not null)
optHelpStr = CommandsUtilityService.GetCommandOptionHelpList(opt); optHelpStr = CommandsUtilityService.GetCommandOptionHelpList(opt);
return new CommandJsonObject return new CommandJsonObject
{ {
Aliases = com.Aliases.Select(alias => prefix + alias).ToArray(), Aliases = com.Aliases.Select(alias => prefix + alias).ToArray(),
Description = com.RealSummary(_strings, _marmalades, Culture, prefix), Description = com.RealSummary(_strings, _marmalades, Culture, prefix),
Usage = com.RealRemarksArr(_strings, _marmalades, Culture, prefix), Usage = com.RealRemarksArr(_strings, _marmalades, Culture, prefix),
Submodule = com.Module.Name, Submodule = com.Module.Name,
Module = com.Module.GetTopLevelModule().Name, Module = com.Module.GetTopLevelModule().Name,
Options = optHelpStr, Options = optHelpStr,
Requirements = CommandsUtilityService.GetCommandRequirements(com) Requirements = CommandsUtilityService.GetCommandRequirements(com)
}; };
}) })
.ToList()); .ToList());
var readableData = JsonConvert.SerializeObject(cmdData, Formatting.Indented); var readableData = JsonConvert.SerializeObject(cmdData, Formatting.Indented);
@ -512,17 +526,17 @@ public sealed partial class Help : EllieModule<HelpService>
[Cmd] [Cmd]
public async Task Guide() public async Task Guide()
=> await Response() => await Response()
.Confirm(strs.guide("https://commands.elliebot.net", .Confirm(strs.guide("https://commands.elliebot.net",
"https://docs.elliebot.net")) "https://docs.elliebot.net"))
.SendAsync(); .SendAsync();
[Cmd] [Cmd]
[OnlyPublicBot] [OnlyPublicBot]
public async Task Donate() public async Task Donate()
{ {
var eb = CreateEmbed() var eb = CreateEmbed()
.WithOkColor() .WithOkColor()
.WithTitle("Thank you for considering to donate to the EllieBot project!"); .WithTitle("Thank you for considering to donate to the EllieBot project!");
eb eb
.WithDescription(""" .WithDescription("""
@ -554,9 +568,9 @@ public sealed partial class Help : EllieModule<HelpService>
try try
{ {
await Response() await Response()
.Channel(await ctx.User.CreateDMChannelAsync()) .Channel(await ctx.User.CreateDMChannelAsync())
.Embed(eb) .Embed(eb)
.SendAsync(); .SendAsync();
_ = ctx.OkAsync(); _ = ctx.OkAsync();
} }