From a00ec907b285578dd0b2b6852dfcb627c4c754e3 Mon Sep 17 00:00:00 2001 From: stepech <29132060+stepech@users.noreply.github.com> Date: Sun, 18 Sep 2022 16:59:38 +0200 Subject: [PATCH 1/2] Hotfixes: - remove some easter eggs - remove token logging - re enable standup ping - fully remove mute role overrides - better error catching in moderation list command --- .../Commands/EmoteCommands.cs | 2 +- .../Commands/MessageCommands.cs | 7 ++- .../Commands/ModerationCommands.cs | 20 +++++-- .../Commands/NewsManagementCommands.cs | 39 +++++++++++--- .../EventHandlers/NewChannelHandler.cs | 54 ------------------- .../Jobs/StandUpJobProvider.cs | 2 +- .../Options/CommonCommandOptions.cs | 1 - src/HonzaBotner.Discord/DiscordBot.cs | 1 - src/HonzaBotner.Discord/DiscordWrapper.cs | 2 +- src/HonzaBotner/Startup.cs | 1 - src/HonzaBotner/appsettings.BotDev.json | 1 - src/HonzaBotner/appsettings.CvutFit.json | 1 - src/HonzaBotner/appsettings.Development.json | 1 - 13 files changed, 58 insertions(+), 74 deletions(-) delete mode 100644 src/HonzaBotner.Discord.Services/EventHandlers/NewChannelHandler.cs diff --git a/src/HonzaBotner.Discord.Services/Commands/EmoteCommands.cs b/src/HonzaBotner.Discord.Services/Commands/EmoteCommands.cs index eeeb91cc..111a77a9 100644 --- a/src/HonzaBotner.Discord.Services/Commands/EmoteCommands.cs +++ b/src/HonzaBotner.Discord.Services/Commands/EmoteCommands.cs @@ -38,7 +38,7 @@ public EmoteCommands(IEmojiCounterService emojiCounterService) [SlashCommandPermissions(Permissions.ManageEmojis)] public async Task EmoteStatsCommandAsync( InteractionContext ctx, - [Option("display", "Display as total instead of perDay?")] bool total = true, + [Option("display", "Display as total instead of perDay?")] bool total = false, [Option("type", "What type of emojis to show? Defaults all")] DisplayTypes type = DisplayTypes.All) { IEnumerable results = await _emojiCounterService.ListAsync(); diff --git a/src/HonzaBotner.Discord.Services/Commands/MessageCommands.cs b/src/HonzaBotner.Discord.Services/Commands/MessageCommands.cs index 26a7e10a..8c524d7c 100644 --- a/src/HonzaBotner.Discord.Services/Commands/MessageCommands.cs +++ b/src/HonzaBotner.Discord.Services/Commands/MessageCommands.cs @@ -108,7 +108,7 @@ public async Task ReactToMessageCommandAsync( DiscordGuild guild = ctx.Guild; DiscordMessage? oldMessage = await DiscordHelper.FindMessageFromLink(guild, url); - if (oldMessage == null) + if (oldMessage is null) { await ctx.CreateResponseAsync("Could not find message to react to."); return; @@ -135,6 +135,11 @@ await ctx.EditResponseAsync( new DiscordWebhookBuilder() .WithContent("Bot cannot react with provided emoji. Is it universal/from this server?")); } + catch (UnauthorizedException) + { + await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent("Too many reactions")); + return; + } response = await interactivity.WaitForReactionAsync(reactionCatch, ctx.User, TimeSpan.FromMinutes(2)); } diff --git a/src/HonzaBotner.Discord.Services/Commands/ModerationCommands.cs b/src/HonzaBotner.Discord.Services/Commands/ModerationCommands.cs index 65170698..ae380720 100644 --- a/src/HonzaBotner.Discord.Services/Commands/ModerationCommands.cs +++ b/src/HonzaBotner.Discord.Services/Commands/ModerationCommands.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using DSharpPlus; using DSharpPlus.Entities; +using DSharpPlus.Exceptions; using DSharpPlus.Interactivity; using DSharpPlus.Interactivity.Extensions; using DSharpPlus.SlashCommands; @@ -130,10 +131,21 @@ public async Task ListCommandAsync( List<(string, string)> embedFields = allWarnings.ConvertAll(warning => { - string target = ctx.Guild.GetMemberAsync(warning.UserId).Result?.DisplayName ?? warning.UserId.ToString(); - string issuer = ctx.Guild.GetMemberAsync(warning.IssuerId).Result?.DisplayName ?? - warning.IssuerId.ToString(); - return ($"#{warning.Id}\t{target}\t{warning.IssuedAt}\t{issuer}", warning.Reason); + DiscordMember? target = null; + DiscordMember? issuer = null; + try + { + issuer = ctx.Guild.GetMemberAsync(warning.IssuerId).Result; + target = ctx.Guild.GetMemberAsync(warning.UserId).Result; + } + catch (NotFoundException) + { + } + + return ($"#{warning.Id}\t" + + $"{target?.DisplayName ?? warning.UserId.ToString()}\t" + + $"{warning.IssuedAt}\t" + + $"{issuer?.DisplayName ?? warning.IssuerId.ToString()}", warning.Reason); }); if (!embedFields.Any()) diff --git a/src/HonzaBotner.Discord.Services/Commands/NewsManagementCommands.cs b/src/HonzaBotner.Discord.Services/Commands/NewsManagementCommands.cs index ed971ab6..64c36e6e 100644 --- a/src/HonzaBotner.Discord.Services/Commands/NewsManagementCommands.cs +++ b/src/HonzaBotner.Discord.Services/Commands/NewsManagementCommands.cs @@ -2,10 +2,13 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Text; using System.Threading.Tasks; using Chronic.Core; using DSharpPlus; using DSharpPlus.Entities; +using DSharpPlus.Interactivity.Enums; +using DSharpPlus.Interactivity.Extensions; using DSharpPlus.SlashCommands; using HonzaBotner.Discord.Services.Jobs; using HonzaBotner.Services.Contract; @@ -34,14 +37,29 @@ public async Task ListConfigCommandAsync(InteractionContext ctx) DiscordEmbedBuilder builder = new() { Title = "News List" }; builder.WithTimestamp(DateTime.Now); + + StringBuilder stringBuilder = new("\n"); + foreach (NewsConfig config in configs) { string active = GetActiveEmoji(config); - builder.AddField($"{active} {config.Name} [{config.Id}]", - $"Last fetched: {config.LastFetched}"); + stringBuilder.Append($"{active} {config.Name} [{config.Id}]"); + stringBuilder.Append($"Last fetched: {config.LastFetched}"); + stringBuilder.AppendLine(); } - await ctx.CreateResponseAsync(builder.Build()); + + var interaction = ctx.Client.GetInteractivity(); + var pages = interaction.GeneratePagesInEmbed(stringBuilder.ToString(), SplitType.Line, builder).ToList(); + + if (!pages.Any()) + { + await ctx.CreateResponseAsync("No configs set", true); + return; + } + + + await interaction.SendPaginatedResponseAsync(ctx.Interaction, false, ctx.User, pages); } @@ -83,9 +101,18 @@ public async Task ToggleConfigCommandAsync(InteractionContext context, public async Task AddConfigCommandAsync(InteractionContext ctx, [Option("name", "Name of the news config")] string name, [Option("source", "Source identification")] string source, - [Option("channels", "Channels where news will be published")] string channels) + [Option("channels", "Channels where news will be published")] string channels, + [Option("since", "Date and time of oldest news fetched")] string rawDateTime) { - NewsConfig config = new(default, name, source, DateTime.MinValue, NewsProviderType.Courses, PublisherType.DiscordEmbed, + DateTime? parsedDateTime = ParseDateTime(rawDateTime); + + if (parsedDateTime is null) + { + await ctx.CreateResponseAsync("Invalid datetime format", true); + return; + } + + NewsConfig config = new(default, name, source, parsedDateTime.Value, NewsProviderType.Courses, PublisherType.DiscordEmbed, true, ctx.ResolvedChannelMentions.Select(ch => ch.Id).ToArray()); await _configService.AddOrUpdate(config); @@ -112,7 +139,7 @@ public async Task EditLastRunConfigCommandAsync(InteractionContext ctx, { DateTime? lastRun = ParseDateTime(rawDateTime); - if (lastRun is null || lastRun <= DateTime.UtcNow) + if (lastRun is null) { await ctx.CreateResponseAsync("Invalid last-run format"); return; diff --git a/src/HonzaBotner.Discord.Services/EventHandlers/NewChannelHandler.cs b/src/HonzaBotner.Discord.Services/EventHandlers/NewChannelHandler.cs deleted file mode 100644 index 267ab6ce..00000000 --- a/src/HonzaBotner.Discord.Services/EventHandlers/NewChannelHandler.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Threading.Tasks; -using DSharpPlus; -using DSharpPlus.Entities; -using DSharpPlus.EventArgs; -using HonzaBotner.Discord.EventHandler; -using HonzaBotner.Discord.Services.Options; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; - -namespace HonzaBotner.Discord.Services.EventHandlers; - -public class NewChannelHandler : IEventHandler -{ - private readonly ILogger _logger; - private readonly CommonCommandOptions _commonOptions; - - public NewChannelHandler(ILogger logger, IOptions commonOptions) - { - _logger = logger; - _commonOptions = commonOptions.Value; - } - - public async Task Handle(ChannelCreateEventArgs eventArgs) - { - try - { - DiscordRole muteRole = eventArgs.Guild.GetRole(_commonOptions.MuteRoleId); - await eventArgs.Channel.AddOverwriteAsync( - muteRole, - deny: Permissions.SendMessages | Permissions.AddReactions | Permissions.SendTtsMessages - ); - } - catch (Exception e) - { - _logger.LogWarning(e, "Couldn't add mute role override"); - } - - try - { - DiscordRole botRole = eventArgs.Guild.GetRole(_commonOptions.BotRoleId); - await eventArgs.Channel.AddOverwriteAsync( - botRole, - Permissions.AccessChannels | Permissions.SendMessages | Permissions.UseVoice - ); - } - catch (Exception e) - { - _logger.LogWarning(e, "Couldn't add mod role override"); - } - - return EventHandlerResult.Continue; - } -} diff --git a/src/HonzaBotner.Discord.Services/Jobs/StandUpJobProvider.cs b/src/HonzaBotner.Discord.Services/Jobs/StandUpJobProvider.cs index 11094950..9b65f47b 100644 --- a/src/HonzaBotner.Discord.Services/Jobs/StandUpJobProvider.cs +++ b/src/HonzaBotner.Discord.Services/Jobs/StandUpJobProvider.cs @@ -98,7 +98,7 @@ await channel.GetMessagesBeforeAsync(messageList.Last().Id) } await channel.SendMessageAsync($@" -Stand-up time, ||o prázdninách se nepinguje||! +Stand-up time, <@&{_commonOptions.StandUpRoleId}>! Results from : ``` diff --git a/src/HonzaBotner.Discord.Services/Options/CommonCommandOptions.cs b/src/HonzaBotner.Discord.Services/Options/CommonCommandOptions.cs index 81b24267..4e2b9d84 100644 --- a/src/HonzaBotner.Discord.Services/Options/CommonCommandOptions.cs +++ b/src/HonzaBotner.Discord.Services/Options/CommonCommandOptions.cs @@ -7,7 +7,6 @@ public class CommonCommandOptions public ulong ModRoleId { get; set; } public ulong AuthenticatedRoleId { get; set; } - public ulong MuteRoleId { get; set; } public ulong BotRoleId { get; set; } public string? HugEmoteName { get; set; } diff --git a/src/HonzaBotner.Discord/DiscordBot.cs b/src/HonzaBotner.Discord/DiscordBot.cs index 79702cb8..8e095079 100644 --- a/src/HonzaBotner.Discord/DiscordBot.cs +++ b/src/HonzaBotner.Discord/DiscordBot.cs @@ -158,7 +158,6 @@ private Task Client_ChannelCreated(DiscordClient client, ChannelCreateEventArgs private Task Client_ThreadCreated(DiscordClient client, ThreadCreateEventArgs args) { - Task.Run(() => args.Thread.SendMessageAsync("Ping <@132599706747535360>")); return _eventHandler.Handle(args); } diff --git a/src/HonzaBotner.Discord/DiscordWrapper.cs b/src/HonzaBotner.Discord/DiscordWrapper.cs index 3c83b988..cad88a5e 100644 --- a/src/HonzaBotner.Discord/DiscordWrapper.cs +++ b/src/HonzaBotner.Discord/DiscordWrapper.cs @@ -40,6 +40,6 @@ public DiscordWrapper(IOptions options, IServiceProvider services }; Commands = Client.UseSlashCommands(sConfig); - Client.Logger.LogInformation("Starting with secret: {Token}", options.Value.Token); + Client.Logger.LogInformation("Starting Bot"); } } diff --git a/src/HonzaBotner/Startup.cs b/src/HonzaBotner/Startup.cs index 835ba3cc..8dac05b0 100644 --- a/src/HonzaBotner/Startup.cs +++ b/src/HonzaBotner/Startup.cs @@ -61,7 +61,6 @@ public void ConfigureServices(IServiceCollection services) .AddEventHandler() .AddEventHandler() .AddEventHandler() - .AddEventHandler() .AddEventHandler() .AddEventHandler() .AddEventHandler(EventHandlerPriority.High) diff --git a/src/HonzaBotner/appsettings.BotDev.json b/src/HonzaBotner/appsettings.BotDev.json index f4a921d3..2fd9df19 100644 --- a/src/HonzaBotner/appsettings.BotDev.json +++ b/src/HonzaBotner/appsettings.BotDev.json @@ -16,7 +16,6 @@ "ModRoleId": 811385023705645086, "BotRoleId": 493947307427889152, "AuthenticatedRoleId": 809436691470614539, - "MuteRoleId": 0, "HugEmoteName": "", "BoosterRoleId": 0, "GentlemenChannelId": 0, diff --git a/src/HonzaBotner/appsettings.CvutFit.json b/src/HonzaBotner/appsettings.CvutFit.json index e94569ed..3bf8f586 100644 --- a/src/HonzaBotner/appsettings.CvutFit.json +++ b/src/HonzaBotner/appsettings.CvutFit.json @@ -17,7 +17,6 @@ "ModRoleId": 366970860550225950, "BotRoleId": 493947307427889152, "AuthenticatedRoleId": 681559148546359432, - "MuteRoleId": 750276752663904306, "HugEmoteName": "peepoHugger", "BoosterRoleId": 648686713845055489, "GentlemenChannelId": 756616473283133551, diff --git a/src/HonzaBotner/appsettings.Development.json b/src/HonzaBotner/appsettings.Development.json index 0e70b62c..00df2317 100644 --- a/src/HonzaBotner/appsettings.Development.json +++ b/src/HonzaBotner/appsettings.Development.json @@ -18,7 +18,6 @@ "ModRoleId": 887978558759264296, "BotRoleId": 887978852930953246, "AuthenticatedRoleId": 887977978712174622, - "MuteRoleId": 0, "HugEmoteName": "", "BoosterRoleId": 820565262095876158, "GentlemenChannelId": 820299208120336415, From be2620b13f61691d35e193488e6a2f01cfa24d37 Mon Sep 17 00:00:00 2001 From: stepech <29132060+stepech@users.noreply.github.com> Date: Sun, 18 Sep 2022 17:03:11 +0200 Subject: [PATCH 2/2] Version: Bump version to 2022.4.1 --- Directory.Build.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Directory.Build.props b/Directory.Build.props index 8d0dd53c..2e1dff08 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,6 @@  - 2022.4 + 2022.4.1 net6.0 preview enable