From 5b451cee74308f35f9c1b34072d436e866352b23 Mon Sep 17 00:00:00 2001 From: Toastie Date: Sat, 14 Sep 2024 15:11:14 +1200 Subject: [PATCH] Fixed some countries, replacements, updated bot.yml --- .../Administration/GreetBye/GreetCommands.cs | 198 +++++++++--------- .../Administration/GreetBye/GreetService.cs | 67 +++--- .../Modules/Searches/Search/SearchCommands.cs | 2 +- src/EllieBot/_common/Configs/BotConfig.cs | 2 +- .../Impl/ReplacementRegistrator.default.cs | 12 ++ .../_common/Settings/BotConfigService.cs | 6 + src/EllieBot/data/bot.yml | 12 +- src/EllieBot/data/hangman/countries.yml | 20 +- 8 files changed, 159 insertions(+), 160 deletions(-) diff --git a/src/EllieBot/Modules/Administration/GreetBye/GreetCommands.cs b/src/EllieBot/Modules/Administration/GreetBye/GreetCommands.cs index b473326..c7ee52a 100644 --- a/src/EllieBot/Modules/Administration/GreetBye/GreetCommands.cs +++ b/src/EllieBot/Modules/Administration/GreetBye/GreetCommands.cs @@ -5,6 +5,99 @@ public partial class Administration [Group] public partial class GreetCommands : EllieModule { + [Cmd] + [RequireContext(ContextType.Guild)] + [UserPerm(GuildPerm.ManageGuild)] + public Task Boost() + => Toggle(GreetType.Boost); + + [Cmd] + [RequireContext(ContextType.Guild)] + [UserPerm(GuildPerm.ManageGuild)] + public Task BoostDel(int timer = 30) + => SetDel(GreetType.Boost, timer); + + [Cmd] + [RequireContext(ContextType.Guild)] + [UserPerm(GuildPerm.ManageGuild)] + public Task BoostMsg([Leftover] string? text = null) + => SetMsg(GreetType.Boost, text); + + [Cmd] + [RequireContext(ContextType.Guild)] + [UserPerm(GuildPerm.ManageGuild)] + public Task Greet() + => Toggle(GreetType.Greet); + + [Cmd] + [RequireContext(ContextType.Guild)] + [UserPerm(GuildPerm.ManageGuild)] + public Task GreetDel(int timer = 30) + => SetDel(GreetType.Greet, timer); + + [Cmd] + [RequireContext(ContextType.Guild)] + [UserPerm(GuildPerm.ManageGuild)] + public Task GreetMsg([Leftover] string? text = null) + => SetMsg(GreetType.Greet, text); + + [Cmd] + [RequireContext(ContextType.Guild)] + [UserPerm(GuildPerm.ManageGuild)] + public Task GreetDm() + => Toggle(GreetType.GreetDm); + + [Cmd] + [RequireContext(ContextType.Guild)] + [UserPerm(GuildPerm.ManageGuild)] + public Task GreetDmMsg([Leftover] string? text = null) + => SetMsg(GreetType.GreetDm, text); + + [Cmd] + [RequireContext(ContextType.Guild)] + [UserPerm(GuildPerm.ManageGuild)] + public Task Bye() + => Toggle(GreetType.Bye); + + [Cmd] + [RequireContext(ContextType.Guild)] + [UserPerm(GuildPerm.ManageGuild)] + public Task ByeDel(int timer = 30) + => SetDel(GreetType.Bye, timer); + + [Cmd] + [RequireContext(ContextType.Guild)] + [UserPerm(GuildPerm.ManageGuild)] + public Task ByeMsg([Leftover] string? text = null) + => SetMsg(GreetType.Bye, text); + + [Cmd] + [RequireContext(ContextType.Guild)] + [UserPerm(GuildPerm.ManageGuild)] + public Task GreetTest([Leftover] IGuildUser? user = null) + => Test(GreetType.Greet, user); + + [Cmd] + [RequireContext(ContextType.Guild)] + [UserPerm(GuildPerm.ManageGuild)] + public Task GreetDmTest([Leftover] IGuildUser? user = null) + => Test(GreetType.GreetDm, user); + + [Cmd] + [RequireContext(ContextType.Guild)] + [UserPerm(GuildPerm.ManageGuild)] + [Ratelimit(5)] + public Task ByeTest([Leftover] IGuildUser? user = null) + => Test(GreetType.Bye, user); + + [Cmd] + [RequireContext(ContextType.Guild)] + [UserPerm(GuildPerm.ManageGuild)] + [Ratelimit(5)] + public Task BoostTest([Leftover] IGuildUser? user = null) + => Test(GreetType.Boost, user); + + public async Task Toggle(GreetType type) { var enabled = await _service.SetGreet(ctx.Guild.Id, ctx.Channel.Id, type); @@ -75,17 +168,16 @@ public partial class Administration { if (string.IsNullOrWhiteSpace(text)) { - await _service.SetMessage(ctx.Guild.Id, type, null); var conf = await _service.GetGreetSettingsAsync(ctx.Guild.Id, type); - var msg = conf?.MessageText ?? "No message set."; + var msg = conf?.MessageText ?? GreetService.GetDefaultGreet(type); await Response() .Confirm( type switch { - GreetType.Boost => strs.boostmsg_cur(msg.SanitizeMentions()), - GreetType.Greet => strs.greetmsg_cur(msg.SanitizeMentions()), - GreetType.Bye => strs.byemsg_cur(msg.SanitizeMentions()), - GreetType.GreetDm => strs.greetdmmsg_cur(msg.SanitizeMentions()), + GreetType.Boost => strs.boostmsg_cur(msg), + GreetType.Greet => strs.greetmsg_cur(msg), + GreetType.Bye => strs.byemsg_cur(msg), + GreetType.GreetDm => strs.greetdmmsg_cur(msg), _ => strs.error }) .SendAsync(); @@ -121,100 +213,6 @@ public partial class Administration } } - [Cmd] - [RequireContext(ContextType.Guild)] - [UserPerm(GuildPerm.ManageGuild)] - public Task Boost() - => Toggle(GreetType.Boost); - - [Cmd] - [RequireContext(ContextType.Guild)] - [UserPerm(GuildPerm.ManageGuild)] - public Task BoostDel(int timer = 30) - => SetDel(GreetType.Boost, timer); - - [Cmd] - [RequireContext(ContextType.Guild)] - [UserPerm(GuildPerm.ManageGuild)] - public Task BoostMsg([Leftover] string? text = null) - => SetMsg(GreetType.Boost, text); - - [Cmd] - [RequireContext(ContextType.Guild)] - [UserPerm(GuildPerm.ManageGuild)] - public Task Greet() - => Toggle(GreetType.Greet); - - [Cmd] - [RequireContext(ContextType.Guild)] - [UserPerm(GuildPerm.ManageGuild)] - public Task GreetDel(int timer = 30) - => SetDel(GreetType.Greet, timer); - - [Cmd] - [RequireContext(ContextType.Guild)] - [UserPerm(GuildPerm.ManageGuild)] - public Task GreetMsg([Leftover] string? text = null) - => SetMsg(GreetType.Greet, text); - - [Cmd] - [RequireContext(ContextType.Guild)] - [UserPerm(GuildPerm.ManageGuild)] - public Task GreetDm() - => Toggle(GreetType.GreetDm); - - [Cmd] - [RequireContext(ContextType.Guild)] - [UserPerm(GuildPerm.ManageGuild)] - public Task GreetDmMsg([Leftover] string? text = null) - => SetMsg(GreetType.GreetDm, text); - - [Cmd] - [RequireContext(ContextType.Guild)] - [UserPerm(GuildPerm.ManageGuild)] - public Task Bye() - => Toggle(GreetType.Bye); - - [Cmd] - [RequireContext(ContextType.Guild)] - [UserPerm(GuildPerm.ManageGuild)] - public Task ByeDel(int timer = 30) - => SetDel(GreetType.Bye, timer); - - [Cmd] - [RequireContext(ContextType.Guild)] - [UserPerm(GuildPerm.ManageGuild)] - public Task ByeMsg([Leftover] string? text = null) - => SetMsg(GreetType.Bye, text); - - [Cmd] - [RequireContext(ContextType.Guild)] - [UserPerm(GuildPerm.ManageGuild)] - [Ratelimit(5)] - public Task GreetTest([Leftover] IGuildUser? user = null) - => Test(GreetType.Greet, user); - - [Cmd] - [RequireContext(ContextType.Guild)] - [UserPerm(GuildPerm.ManageGuild)] - [Ratelimit(5)] - public Task GreetDmTest([Leftover] IGuildUser? user = null) - => Test(GreetType.GreetDm, user); - - [Cmd] - [RequireContext(ContextType.Guild)] - [UserPerm(GuildPerm.ManageGuild)] - [Ratelimit(5)] - public Task ByeTest([Leftover] IGuildUser? user = null) - => Test(GreetType.Bye, user); - - [Cmd] - [RequireContext(ContextType.Guild)] - [UserPerm(GuildPerm.ManageGuild)] - [Ratelimit(5)] - public Task BoostTest([Leftover] IGuildUser? user = null) - => Test(GreetType.Boost, user); - public async Task Test(GreetType type, IGuildUser? user = null) { user ??= (IGuildUser)ctx.User; diff --git a/src/EllieBot/Modules/Administration/GreetBye/GreetService.cs b/src/EllieBot/Modules/Administration/GreetBye/GreetService.cs index e1fd0db..e47f743 100644 --- a/src/EllieBot/Modules/Administration/GreetBye/GreetService.cs +++ b/src/EllieBot/Modules/Administration/GreetBye/GreetService.cs @@ -14,7 +14,6 @@ public class GreetService : IEService, IReadyExecutor private readonly DiscordSocketClient _client; - private readonly BotConfigService _bss; private readonly IReplacementService _repSvc; private readonly IBotCache _cache; private readonly IMessageSenderService _sender; @@ -29,7 +28,6 @@ public class GreetService : IEService, IReadyExecutor public GreetService( DiscordSocketClient client, DbService db, - BotConfigService bss, IMessageSenderService sender, IReplacementService repSvc, IBotCache cache @@ -37,10 +35,15 @@ public class GreetService : IEService, IReadyExecutor { _db = db; _client = client; - _bss = bss; _repSvc = repSvc; _cache = cache; _sender = sender; + + + foreach (var type in Enum.GetValues()) + { + _enabled[type] = new(); + } } public async Task OnReadyAsync() @@ -52,11 +55,17 @@ public class GreetService : IEService, IReadyExecutor var enabled = await uow.GetTable() .Where(x => x.GuildId.In(guilds)) .Where(x => x.IsEnabled) + .Select(x => new + { + x.GuildId, + x.GreetType + }) .ToListAsync(); - _enabled = enabled.GroupBy(x => x.GreetType, v => v.GuildId) - .ToDictionary(x => x.Key, x => x.ToHashSet().ToConcurrentSet()) - .ToConcurrent(); + foreach (var e in enabled) + { + _enabled[e.GreetType].Add(e.GuildId); + } } _client.UserJoined += OnUserJoined; @@ -146,7 +155,7 @@ public class GreetService : IEService, IReadyExecutor return Task.CompletedTask; } - private readonly TypedKey _greetSettingsKey = new(); + private readonly TypedKey _greetSettingsKey = new("greet_settings"); public async Task GetGreetSettingsAsync(ulong gid, GreetType type) => await _cache.GetOrAddAsync(_greetSettingsKey, @@ -160,6 +169,9 @@ public class GreetService : IEService, IReadyExecutor .Where(x => x.GuildId == gid && x.GreetType == type) .FirstOrDefaultAsync(); + if (res is not null) + res.MessageText ??= GetDefaultGreet(type); + return res; } @@ -324,12 +336,12 @@ public class GreetService : IEService, IReadyExecutor } - private static string GetDefaultGreet(GreetType greetType) + public static string GetDefaultGreet(GreetType greetType) => greetType switch { - GreetType.Boost => "%user.name% has boosted the server!", - GreetType.Greet => "%user.name% has joined the server!", - GreetType.Bye => "%user.name has left the server!", + GreetType.Boost => "%user.mention% has boosted the server!", + GreetType.Greet => "%user.mention% has joined the server!", + GreetType.Bye => "%user.name% has left the server!", GreetType.GreetDm => "Welcome to the server %user.name%", _ => "%user.name% did something new!" }; @@ -343,16 +355,16 @@ public class GreetService : IEService, IReadyExecutor await using var uow = _db.GetDbContext(); var q = uow.GetTable(); + if (value is null) + value = !_enabled[greetType].Contains(guildId); + if (value is { } v) { - var defaultGreet = GetDefaultGreet(greetType); - await q .InsertOrUpdateAsync(() => new() { GuildId = guildId, GreetType = greetType, - MessageText = defaultGreet, IsEnabled = v, ChannelId = channelId, }, @@ -367,40 +379,20 @@ public class GreetService : IEService, IReadyExecutor GreetType = greetType, }); } - else - { - var result = await q - .Where(x => x.GuildId == guildId && x.GreetType == greetType) - .UpdateWithOutputAsync((old) => new() - { - IsEnabled = !old.IsEnabled - }, - (o, n) => n.IsEnabled); - - if (result.Length > 0) - value = result[0]; - } if (value is true) { _enabled[greetType].Add(guildId); - } - else - { - _enabled[greetType].TryRemove(guildId); + return true; } - return true; + _enabled[greetType].TryRemove(guildId); + return false; } public async Task SetMessage(ulong guildId, GreetType greetType, string? message) { - message = message?.SanitizeMentions(); - - if (string.IsNullOrWhiteSpace(message)) - message = GetDefaultGreet(greetType); - await using (var uow = _db.GetDbContext()) { await uow.GetTable() @@ -475,6 +467,7 @@ public class GreetService : IEService, IReadyExecutor { if (conf.GreetType == GreetType.GreetDm) { + await _greetQueue.Writer.WriteAsync((conf, user, channel as ITextChannel)); return await GreetDmUser(conf, user); } diff --git a/src/EllieBot/Modules/Searches/Search/SearchCommands.cs b/src/EllieBot/Modules/Searches/Search/SearchCommands.cs index 6a98eac..411600c 100644 --- a/src/EllieBot/Modules/Searches/Search/SearchCommands.cs +++ b/src/EllieBot/Modules/Searches/Search/SearchCommands.cs @@ -72,7 +72,7 @@ public partial class Searches } [Cmd] - public async Task Image([Leftover] string? query = null) + public async Task Image([Leftover] string? query) { query = query?.Trim(); diff --git a/src/EllieBot/_common/Configs/BotConfig.cs b/src/EllieBot/_common/Configs/BotConfig.cs index c686ed7..df416ee 100644 --- a/src/EllieBot/_common/Configs/BotConfig.cs +++ b/src/EllieBot/_common/Configs/BotConfig.cs @@ -12,7 +12,7 @@ namespace EllieBot.Common.Configs; public sealed partial class BotConfig : ICloneable { [Comment("""DO NOT CHANGE""")] - public int Version { get; set; } = 7; + public int Version { get; set; } = 8; [Comment(""" Most commands, when executed, have a small colored line diff --git a/src/EllieBot/_common/Replacements/Impl/ReplacementRegistrator.default.cs b/src/EllieBot/_common/Replacements/Impl/ReplacementRegistrator.default.cs index d42bba6..0dc42de 100644 --- a/src/EllieBot/_common/Replacements/Impl/ReplacementRegistrator.default.cs +++ b/src/EllieBot/_common/Replacements/Impl/ReplacementRegistrator.default.cs @@ -61,6 +61,18 @@ public sealed partial class ReplacementPatternStore private void WithUsers() { + Register("%user%", static (IUser user) => user.Mention); + Register("%user.mention%", static (IUser user) => user.Mention); + Register("%user.fullname%", static (IUser user) => user.ToString()!); + Register("%user.name%", static (IUser user) => user.Username); + Register("%user.discrim%", static (IUser user) => user.Discriminator); + Register("%user.avatar%", static (IUser user) => user.RealAvatarUrl().ToString()); + Register("%user.id%", static (IUser user) => user.Id.ToString()); + Register("%user.created_time%", static (IUser user) => user.CreatedAt.ToString("HH:mm")); + Register("%user.created_date%", static (IUser user) => user.CreatedAt.ToString("dd.MM.yyyy")); + Register("%user.joined_time%", static (IGuildUser user) => user.JoinedAt?.ToString("HH:mm")); + Register("%user.joined_date%", static (IGuildUser user) => user.JoinedAt?.ToString("dd.MM.yyyy")); + Register("%user%", static (IUser[] users) => string.Join(" ", users.Select(user => user.Mention))); Register("%user.mention%", diff --git a/src/EllieBot/_common/Settings/BotConfigService.cs b/src/EllieBot/_common/Settings/BotConfigService.cs index 477f080..0deca4d 100644 --- a/src/EllieBot/_common/Settings/BotConfigService.cs +++ b/src/EllieBot/_common/Settings/BotConfigService.cs @@ -69,5 +69,11 @@ public sealed class BotConfigService : ConfigServiceBase c.Version = 7; c.IgnoreOtherBots = true; }); + + if (data.Version < 8) + ModifyConfig(c => + { + c.Version = 8; + }); } } \ No newline at end of file diff --git a/src/EllieBot/data/bot.yml b/src/EllieBot/data/bot.yml index 55a1d51..22b08ed 100644 --- a/src/EllieBot/data/bot.yml +++ b/src/EllieBot/data/bot.yml @@ -1,5 +1,5 @@ # DO NOT CHANGE -version: 7 +version: 8 # Most commands, when executed, have a small colored line # next to the response. The color depends whether the command # is completed, errored or in progress (pending) @@ -80,16 +80,6 @@ blocked: modules: [] # Which string will be used to recognize the commands prefix: . -# Toggles whether your bot will group greet/bye messages into a single message every 5 seconds. -# 1st user who joins will get greeted immediately -# If more users join within the next 5 seconds, they will be greeted in groups of 5. -# This will cause %user.mention% and other placeholders to be replaced with multiple users. -# Keep in mind this might break some of your embeds - for example if you have %user.avatar% in the thumbnail, -# it will become invalid, as it will resolve to a list of avatars of grouped users. -# note: This setting is primarily used if you're afraid of raids, or you're running medium/large bots where some -# servers might get hundreds of people join at once. This is used to prevent the bot from getting ratelimited, -# and (slightly) reduce the greet spam in those servers. -groupGreets: false # Whether the bot will rotate through all specified statuses. # This setting can be changed via .ropl command. # See RotatingStatuses submodule in Administration. diff --git a/src/EllieBot/data/hangman/countries.yml b/src/EllieBot/data/hangman/countries.yml index 5899c5d..249408d 100644 --- a/src/EllieBot/data/hangman/countries.yml +++ b/src/EllieBot/data/hangman/countries.yml @@ -140,13 +140,13 @@ imageUrl: https://cdn.nadeko.bot/flags/gt-flag.gif - word: Guinea imageUrl: https://cdn.nadeko.bot/flags/gv-flag.gif -- word: Guinea-Bissau +- word: Guinea Bissau imageUrl: https://cdn.nadeko.bot/flags/pu-flag.gif - word: Guyana imageUrl: https://cdn.nadeko.bot/flags/gy-flag.gif - word: Haiti imageUrl: https://cdn.nadeko.bot/flags/ha-flag.gif -- word: Holy See +- word: Vatican imageUrl: https://cdn.nadeko.bot/flags/vt-flag.gif - word: Honduras imageUrl: https://cdn.nadeko.bot/flags/ho-flag.gif @@ -184,7 +184,7 @@ imageUrl: https://cdn.nadeko.bot/flags/ku-flag.gif - word: Kyrgyzstan imageUrl: https://cdn.nadeko.bot/flags/kg-flag.gif -- word: Laos +- word: Lao People's Democratic Republic imageUrl: https://cdn.nadeko.bot/flags/la-flag.gif - word: Latvia imageUrl: https://cdn.nadeko.bot/flags/lg-flag.gif @@ -282,7 +282,7 @@ imageUrl: https://cdn.nadeko.bot/flags/qa-flag.gif - word: Romania imageUrl: https://cdn.nadeko.bot/flags/ro-flag.gif -- word: Russia +- word: Russian Federation imageUrl: https://cdn.nadeko.bot/flags/rs-flag.gif - word: Rwanda imageUrl: https://cdn.nadeko.bot/flags/rw-flag.gif @@ -318,7 +318,7 @@ imageUrl: https://cdn.nadeko.bot/flags/so-flag.gif - word: South Africa imageUrl: https://cdn.nadeko.bot/flags/sf-flag.gif -- word: South Korea +- word: Republic of Korea imageUrl: https://cdn.nadeko.bot/flags/ks-flag.gif - word: South Sudan imageUrl: https://cdn.nadeko.bot/flags/od-flag.gif @@ -326,9 +326,9 @@ imageUrl: https://cdn.nadeko.bot/flags/sp-flag.gif - word: Sri Lanka imageUrl: https://cdn.nadeko.bot/flags/ce-flag.gif -- word: St. Vincent Grenadines +- word: Saint Vincent and the Grenadines imageUrl: https://cdn.nadeko.bot/flags/vc-flag.gif -- word: State of Palestine +- word: Palestine imageUrl: https://cdn.nadeko.bot/flags/palestine-flag.gif - word: Sudan imageUrl: https://cdn.nadeko.bot/flags/su-flag.gif @@ -338,11 +338,11 @@ imageUrl: https://cdn.nadeko.bot/flags/sw-flag.gif - word: Switzerland imageUrl: https://cdn.nadeko.bot/flags/sz-flag.gif -- word: Syria +- word: Syrian Arab Republic imageUrl: https://cdn.nadeko.bot/flags/sy-flag.gif - word: Tajikistan imageUrl: https://cdn.nadeko.bot/flags/ti-flag.gif -- word: Tanzania +- word: United Republic of Tanzania imageUrl: https://cdn.nadeko.bot/flags/tz-flag.gif - word: Thailand imageUrl: https://cdn.nadeko.bot/flags/th-flag.gif @@ -380,7 +380,7 @@ imageUrl: https://cdn.nadeko.bot/flags/nh-flag.gif - word: Venezuela imageUrl: https://cdn.nadeko.bot/flags/ve-flag.gif -- word: Vietnam +- word: Viet Nam imageUrl: https://cdn.nadeko.bot/flags/vm-flag.gif - word: Yemen imageUrl: https://cdn.nadeko.bot/flags/ym-flag.gif