From 6251eea44f603b6ccee00bf677456108769c3833 Mon Sep 17 00:00:00 2001 From: Ryan de Jonge Date: Tue, 14 Nov 2023 20:19:18 +0100 Subject: [PATCH] Better bot legitimacy controls --- ModCore/Components/BotManagerComponents.cs | 36 ++++++++++++++++++++++ ModCore/Listeners/MessageSnipe.cs | 8 +++-- ModCore/Listeners/NoBotFarm.cs | 7 ++++- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/ModCore/Components/BotManagerComponents.cs b/ModCore/Components/BotManagerComponents.cs index cf5b502c..38e173c4 100644 --- a/ModCore/Components/BotManagerComponents.cs +++ b/ModCore/Components/BotManagerComponents.cs @@ -1,5 +1,7 @@ using DSharpPlus; +using DSharpPlus.Entities; using DSharpPlus.EventArgs; +using ModCore.Database; using ModCore.Extensions; using ModCore.Extensions.Abstractions; using ModCore.Extensions.Attributes; @@ -12,6 +14,15 @@ namespace ModCore.Components { public class BotManagerComponents : BaseComponentModule { + private DatabaseContextBuilder database; + private DiscordClient client; + + public BotManagerComponents(DatabaseContextBuilder database, DiscordClient client) + { + this.database = database; + this.client = client; + } + [Component("fb", ComponentType.Button)] public async Task RespondFeedbackAsync(ComponentInteractionCreateEventArgs e, IDictionary context) { @@ -28,5 +39,30 @@ await Client.GetInteractionExtension().RespondWithModalAsync context) + { + if (Client.CurrentApplication.Owners.All(x => x.Id != e.User.Id)) + return; + + if (ulong.TryParse(context["id"], out ulong id)) + { + var naughtyGuild = await client.GetGuildAsync(id); + if (naughtyGuild is not null) + { + await naughtyGuild.LeaveAsync(); + var response = new DiscordInteractionResponseBuilder() + .WithContent("") + .AsEphemeral() + .AddEmbed(new DiscordEmbedBuilder().WithTitle($"Left naughty guild.") + .WithDescription($"{id}: {naughtyGuild.Name}") + .WithThumbnail(naughtyGuild.GetIconUrl(ImageFormat.Png)) + .WithColor(DiscordColor.Red)); + + await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, response); + } + } + } } } diff --git a/ModCore/Listeners/MessageSnipe.cs b/ModCore/Listeners/MessageSnipe.cs index bcd735a9..361d67ce 100644 --- a/ModCore/Listeners/MessageSnipe.cs +++ b/ModCore/Listeners/MessageSnipe.cs @@ -19,7 +19,7 @@ public static async Task MessageSniped(MessageDeleteEventArgs eventargs, SharedD { await Task.Yield(); - if (eventargs.Message == null) + if (eventargs.Message is null) return; if (eventargs.Message.WebhookMessage) return; @@ -54,10 +54,12 @@ public static async Task MessageSniped(MessageDeleteEventArgs eventargs, SharedD [AsyncListener(EventType.MessageUpdated)] public static async Task MessageEdited(MessageUpdateEventArgs eventargs, SharedData sharedData, DatabaseContextBuilder database, IMemoryCache cache) { - if (eventargs.Message == null) + if (eventargs.Message is null) return; if (eventargs.Message.WebhookMessage) return; + if (eventargs.MessageBefore is null) + return; await Task.Yield(); @@ -71,7 +73,7 @@ public static async Task MessageEdited(MessageUpdateEventArgs eventargs, SharedD if(cfg != null && cfg.Logging.EditLog_Enable) { var channel = eventargs.Guild.GetChannel(cfg.Logging.ChannelId); - if (channel == null) + if (channel is null) return; if (eventargs.Message.Content != eventargs.MessageBefore.Content) diff --git a/ModCore/Listeners/NoBotFarm.cs b/ModCore/Listeners/NoBotFarm.cs index 05a5b938..1966aaaa 100644 --- a/ModCore/Listeners/NoBotFarm.cs +++ b/ModCore/Listeners/NoBotFarm.cs @@ -31,7 +31,7 @@ public static async Task NoBotFarmsPleaseAsync(GuildCreateEventArgs e, DiscordCl var embed = new DiscordEmbedBuilder() .WithTitle("Joined new guild!") - .WithDescription($"Guild Name: {e.Guild.Name}") + .WithDescription($"Guild Name: {e.Guild.Name}\nID: {e.Guild.Id}") .AddField("Members", e.Guild.MemberCount.ToString(), true) .AddField("Bots", botCount >= 0 ? botCount.ToString() : "LargeGuild", true) .AddField("Bot Ratio", botCount >= 0 ? $"{botRatio}%" : "LargeGuild", true); @@ -54,6 +54,11 @@ public static async Task NoBotFarmsPleaseAsync(GuildCreateEventArgs e, DiscordCl messageBuilder.AddEmbed(embed); + messageBuilder.AddComponents(new DiscordComponent[] + { + new DiscordButtonComponent(ButtonStyle.Danger, "lguild", "Leave Guild", emoji: new DiscordComponentEmoji("❌")) + }); + await channel.SendMessageAsync(messageBuilder); } }