From 86a3dd213796fd6aba8b51bc43611196a69adad6 Mon Sep 17 00:00:00 2001 From: Sellara <147769367+Sella-GH@users.noreply.github.com> Date: Fri, 1 Nov 2024 01:05:55 +0100 Subject: [PATCH] Update DSP and readd the GuildCreatedEvent (#209) * Update DSP * Reuse the event since it's fixed --- Directory.Packages.props | 6 ++-- .../DiscordEvents/DiscordGuildsHandler.cs | 31 ++++++++++++------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 2eb9e5c3..35c7ebe1 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -4,9 +4,9 @@ - - - + + + diff --git a/src/AzzyBot.Bot/Services/DiscordEvents/DiscordGuildsHandler.cs b/src/AzzyBot.Bot/Services/DiscordEvents/DiscordGuildsHandler.cs index b5032154..1dbf7367 100644 --- a/src/AzzyBot.Bot/Services/DiscordEvents/DiscordGuildsHandler.cs +++ b/src/AzzyBot.Bot/Services/DiscordEvents/DiscordGuildsHandler.cs @@ -23,14 +23,15 @@ public sealed class DiscordGuildsHandler(ILogger logger, A private const string NewGuildText = "Thank you for adding me to your server **%GUILD%**! Before you can make good use of me, you have to set my settings first.\n\nPlease use the command `config modify-core` for this.\nOnly administrators are able to execute this command right now."; - public Task HandleEventAsync(DiscordClient sender, GuildCreatedEventArgs eventArgs) + public async Task HandleEventAsync(DiscordClient sender, GuildCreatedEventArgs eventArgs) { ArgumentNullException.ThrowIfNull(sender); ArgumentNullException.ThrowIfNull(eventArgs); _logger.GuildCreated(eventArgs.Guild.Name); - return Task.CompletedTask; + await _dbActions.AddGuildAsync(eventArgs.Guild.Id); + await GuildCreatedHelperAsync([eventArgs.Guild]); } public async Task HandleEventAsync(DiscordClient sender, GuildDeletedEventArgs eventArgs) @@ -72,20 +73,13 @@ public async Task HandleEventAsync(DiscordClient sender, GuildDownloadCompletedE return; } - DiscordEmbed embed; - DiscordMember owner; IEnumerable addedGuilds = await _dbActions.AddGuildsAsync(eventArgs.Guilds); if (addedGuilds.Any()) { - foreach (DiscordGuild guild in addedGuilds) - { - owner = await guild.GetGuildOwnerAsync(); - await owner.SendMessageAsync(NewGuildText.Replace("%GUILD%", guild.Name, StringComparison.OrdinalIgnoreCase)); - embed = await EmbedBuilder.BuildGuildAddedEmbedAsync(guild); - await _botService.SendMessageAsync(_settings.NotificationChannelId, embeds: [embed]); - } + await GuildCreatedHelperAsync(addedGuilds); } + DiscordEmbed embed; IEnumerable removedGuilds = await _dbActions.DeleteGuildsAsync(eventArgs.Guilds); if (removedGuilds.Any()) { @@ -99,4 +93,19 @@ public async Task HandleEventAsync(DiscordClient sender, GuildDownloadCompletedE IAsyncEnumerable guilds = _dbActions.GetGuildsAsync(loadEverything: true); await _botService.CheckPermissionsAsync(guilds); } + + private async Task GuildCreatedHelperAsync(IEnumerable guilds) + { + ArgumentNullException.ThrowIfNull(guilds); + + DiscordEmbed embed; + DiscordMember owner; + foreach (DiscordGuild guild in guilds) + { + owner = await guild.GetGuildOwnerAsync(); + await owner.SendMessageAsync(NewGuildText.Replace("%GUILD%", guild.Name, StringComparison.OrdinalIgnoreCase)); + embed = await EmbedBuilder.BuildGuildAddedEmbedAsync(guild); + await _botService.SendMessageAsync(_settings.NotificationChannelId, embeds: [embed]); + } + } }