From 014498bf151981dad724d12ef92c03a8fbb4b79d Mon Sep 17 00:00:00 2001 From: ishauny Date: Sun, 17 Dec 2023 15:36:39 +0000 Subject: [PATCH 1/4] Update dependencies and modules --- .gitignore | 3 ++ .../Discord.Addons.Interactive.csproj | 4 +- .../InteractiveService.cs | 2 +- Floofbot/Floofbot.csproj | 26 +++++-------- Floofbot/Handlers/CommandHandler.cs | 1 - .../20200512215812_modmail.Designer.cs | 2 +- Floofbot/Migrations/20200512215812_modmail.cs | 2 +- Floofbot/Program.cs | 8 ++-- Floofbot/Services/EventHandlerService.cs | 39 +++++++++---------- Floofbot/Services/NicknameAlertService.cs | 25 ++++++------ Floofbot/Services/UserRoleRetentionService.cs | 2 + Floofbot/Services/WelcomeGateService.cs | 6 ++- Floofbot/Services/WordFilterService.cs | 4 +- 13 files changed, 61 insertions(+), 63 deletions(-) diff --git a/.gitignore b/.gitignore index 120e551c..f5a98009 100644 --- a/.gitignore +++ b/.gitignore @@ -269,3 +269,6 @@ __pycache__/ Floofbot/config.yaml /Floofbot/floofData.db +/Floofbot/config.yaml.sample +/Floofbot/config.yaml +/Floofbot/app.config diff --git a/Discord.Addons.Interactive/Discord.Addons.Interactive.csproj b/Discord.Addons.Interactive/Discord.Addons.Interactive.csproj index 6f013579..f0e4b187 100644 --- a/Discord.Addons.Interactive/Discord.Addons.Interactive.csproj +++ b/Discord.Addons.Interactive/Discord.Addons.Interactive.csproj @@ -28,8 +28,8 @@ - - + + \ No newline at end of file diff --git a/Discord.Addons.Interactive/InteractiveService.cs b/Discord.Addons.Interactive/InteractiveService.cs index 197a18c6..c43fd82f 100644 --- a/Discord.Addons.Interactive/InteractiveService.cs +++ b/Discord.Addons.Interactive/InteractiveService.cs @@ -305,7 +305,7 @@ private static async Task HandlerAsync(SocketMessage message, SocketCommandConte /// /// The . /// - private async Task HandleReactionAsync(Cacheable message, ISocketMessageChannel channel, SocketReaction reaction) + private async Task HandleReactionAsync(Cacheable message, Cacheable channel, SocketReaction reaction) { if (reaction.UserId == Discord.CurrentUser.Id) { diff --git a/Floofbot/Floofbot.csproj b/Floofbot/Floofbot.csproj index f0b76321..5365b5d2 100644 --- a/Floofbot/Floofbot.csproj +++ b/Floofbot/Floofbot.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.1 + netcoreapp8.0 @@ -12,24 +12,18 @@ - - - - + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - - - - - PreserveNewest - + + + + + diff --git a/Floofbot/Handlers/CommandHandler.cs b/Floofbot/Handlers/CommandHandler.cs index 8b535f67..734f7844 100644 --- a/Floofbot/Handlers/CommandHandler.cs +++ b/Floofbot/Handlers/CommandHandler.cs @@ -129,7 +129,6 @@ private async Task HandleCommandAsync(SocketMessage s) { prefix = BotConfigFactory.Config.Prefix; } - bool hasValidPrefix = msg.HasMentionPrefix(_client.CurrentUser, ref argPos) || msg.HasStringPrefix(prefix, ref argPos); string strippedCommandName = msg.Content.Substring(argPos).Split()[0]; bool hasValidStart = !string.IsNullOrEmpty(strippedCommandName) && Regex.IsMatch(strippedCommandName, @"^[0-9]?[a-z]+\??$", RegexOptions.IgnoreCase); diff --git a/Floofbot/Migrations/20200512215812_modmail.Designer.cs b/Floofbot/Migrations/20200512215812_modmail.Designer.cs index 97060f94..6da2b7af 100644 --- a/Floofbot/Migrations/20200512215812_modmail.Designer.cs +++ b/Floofbot/Migrations/20200512215812_modmail.Designer.cs @@ -10,7 +10,7 @@ namespace Floofbot.Migrations { [DbContext(typeof(FloofDataContext))] [Migration("20200512215812_modmail")] - partial class modmail + partial class Modmail { protected override void BuildTargetModel(ModelBuilder modelBuilder) { diff --git a/Floofbot/Migrations/20200512215812_modmail.cs b/Floofbot/Migrations/20200512215812_modmail.cs index 57207553..63ed7eef 100644 --- a/Floofbot/Migrations/20200512215812_modmail.cs +++ b/Floofbot/Migrations/20200512215812_modmail.cs @@ -2,7 +2,7 @@ namespace Floofbot.Migrations { - public partial class modmail : Migration + public partial class Modmail : Migration { protected override void Up(MigrationBuilder migrationBuilder) { diff --git a/Floofbot/Program.cs b/Floofbot/Program.cs index ab2d263f..34a2dd30 100644 --- a/Floofbot/Program.cs +++ b/Floofbot/Program.cs @@ -31,7 +31,7 @@ static async Task Main(string[] args) string token = BotConfigFactory.Config.Token; if (string.IsNullOrEmpty(token)) { - Console.WriteLine("Error: the Token field in app.config must contain a valid Discord bot token."); + Console.WriteLine("Error: the Token field in config.yaml must contain a valid Discord bot token."); Environment.Exit(1); } await new Program().MainAsync(token); @@ -62,8 +62,10 @@ public async Task MainAsync(string token) new DiscordSocketConfig() { LogLevel = LogSeverity.Info, - MessageCacheSize = 1000 - }); + MessageCacheSize = 1000, + AlwaysDownloadUsers = true, + GatewayIntents = GatewayIntents.All + }) ; try { diff --git a/Floofbot/Services/EventHandlerService.cs b/Floofbot/Services/EventHandlerService.cs index 1fca4e80..4be7de97 100644 --- a/Floofbot/Services/EventHandlerService.cs +++ b/Floofbot/Services/EventHandlerService.cs @@ -253,8 +253,9 @@ public Task MessageUpdated(Cacheable before, SocketMessage afte }); return Task.CompletedTask; } - public Task MessageDeleted(Cacheable before, ISocketMessageChannel chan) + public Task MessageDeleted(Cacheable before, Cacheable channel) { + IMessageChannel chan = channel.Value; var _ = Task.Run(async () => { try @@ -267,15 +268,17 @@ public Task MessageDeleted(Cacheable before, ISocketMessageChan if (message.Author.IsBot) return; - var channel = chan as ITextChannel; // channel null, dm message? - if (channel == null) + if (chan == null) return; - if ((IsToggled(channel.Guild)) == false) // not toggled on + IGuildChannel messageChannel = message.Channel as IGuildChannel; + IGuild guild = messageChannel.Guild; + + if ((IsToggled(guild)) == false) // not toggled on return; - Discord.ITextChannel logChannel = await GetChannel(channel.Guild, "MessageDeletedChannel"); + Discord.ITextChannel logChannel = await GetChannel(guild, "MessageDeletedChannel"); if (logChannel == null) return; @@ -283,7 +286,7 @@ public Task MessageDeleted(Cacheable before, ISocketMessageChan embed.WithTitle($"⚠️ Message Deleted | {message.Author.Username}#{message.Author.Discriminator}") .WithColor(Color.Gold) - .WithDescription($"{message.Author.Mention} ({message.Author.Id}) has had their message deleted in {channel.Mention}!") + .WithDescription($"{message.Author.Mention} ({message.Author.Id}) has had their message deleted in {MentionUtils.MentionChannel(chan.Id)}!") .WithCurrentTimestamp() .WithFooter($"user_message_deleted user_messagelog {message.Author.Id}"); if (message.Content.Length > 0) @@ -437,8 +440,10 @@ public Task UserUnbanned(IUser user, IGuild guild) return Task.CompletedTask; } - public Task UserJoined(IGuildUser user) + public Task UserJoined(SocketGuildUser sUser) { + SocketGuild guild = sUser.Guild; + IGuildUser user = guild.GetUser(sUser.Id); var _ = Task.Run(async () => { try @@ -482,7 +487,7 @@ public Task UserJoined(IGuildUser user) }); return Task.CompletedTask; } - public Task UserLeft(IGuildUser user) + public Task UserLeft(SocketGuild guild, SocketUser user) { var _ = Task.Run(async () => { @@ -491,12 +496,11 @@ public Task UserLeft(IGuildUser user) if (user.IsBot) return; - await _userRoleRetentionService.LogUserRoles(user); - if ((IsToggled(user.Guild)) == false) + if ((IsToggled(guild)) == false) return; - Discord.ITextChannel channel = await GetChannel(user.Guild, "UserLeftChannel"); + Discord.ITextChannel channel = await GetChannel(guild, "UserLeftChannel"); if (channel == null) return; @@ -504,14 +508,6 @@ public Task UserLeft(IGuildUser user) embed.WithTitle($"❌ User Left | {user.Username}#{user.Discriminator}") .WithColor(Color.Red) .WithDescription($"{user.Mention} | ``{user.Id}``"); - if (user.JoinedAt != null) - { - DateTimeOffset userJoined = ((DateTimeOffset)user.JoinedAt); - TimeSpan interval = DateTime.UtcNow - userJoined.DateTime; - string day_word = interval.Days == 1 ? "day" : "days"; - embed.AddField("Joined Server", userJoined.ToString("ddd, dd MMM yyyy"), true); - embed.AddField("Time at Server", $"{interval.Days} {day_word}", true); - } embed.WithFooter($"user_leave user_joinlog {user.Id}") .WithCurrentTimestamp(); @@ -603,8 +599,9 @@ public Task UserUpdated(SocketUser before, SocketUser userAfter) } - public Task GuildMemberUpdated(SocketGuildUser before, SocketGuildUser after) + public Task GuildMemberUpdated(Cacheable _before, SocketGuildUser after) { + SocketGuildUser before = _before.Value; var _ = Task.Run(async () => { try @@ -656,6 +653,8 @@ public Task GuildMemberUpdated(SocketGuildUser before, SocketGuildUser after) } else if (before.Roles.Count != after.Roles.Count) { + await _userRoleRetentionService.LogUserRoles(before as IGuildUser); + List beforeRoles = new List(before.Roles); List afterRoles = new List(after.Roles); List roleDifference = new List(); diff --git a/Floofbot/Services/NicknameAlertService.cs b/Floofbot/Services/NicknameAlertService.cs index fad63524..b48874b8 100644 --- a/Floofbot/Services/NicknameAlertService.cs +++ b/Floofbot/Services/NicknameAlertService.cs @@ -66,10 +66,10 @@ public async Task HandleBadNickname(SocketGuildUser badUser, IGuild guild, List< } - public async Task OnReactionAdded(Cacheable message, ISocketMessageChannel channel, SocketReaction reaction) + public async Task OnReactionAdded(Cacheable message, Cacheable channel, SocketReaction reaction) { var msg = message.Value as IUserMessage; - var chan = channel as ITextChannel; + var chan = channel.Value as ITextChannel; if (reaction.User.Value.IsBot || msg == null || chan == null) return; @@ -100,11 +100,11 @@ public async Task OnReactionAdded(Cacheable message, ISocke await badUser.SendMessageAsync("", false, builder.Build()); await badUser.Guild.AddBanAsync(badUser, 0, $"{moderator.Username}#{moderator.Discriminator} ({moderator.Id}) -> Inappropriate Name"); - await channel.SendMessageAsync($"Got it! I banned {badUser.Username}#{badUser.Discriminator}!"); + await chan.SendMessageAsync($"Got it! I banned {badUser.Username}#{badUser.Discriminator}!"); } catch (Exception ex) { - await channel.SendMessageAsync("Unable to ban user. Do I have the permissions?"); + await chan.SendMessageAsync("Unable to ban user. Do I have the permissions?"); Log.Error("Unable to ban user for bad name: " + ex); } alertMessageIdsDic.Remove(msg.Id); @@ -132,12 +132,11 @@ public async Task OnReactionAdded(Cacheable message, ISocke builder.AddField("Reason", "Warned by BOT for an inappropriate name"); builder.Color = Color.DarkOrange; await badUser.SendMessageAsync("", false, builder.Build()); - - await channel.SendMessageAsync($"Got it! I warned {badUser.Username}#{badUser.Discriminator}!"); + await chan.SendMessageAsync($"Got it! I warned {badUser.Username}#{badUser.Discriminator}!"); } catch (Exception ex) { - await channel.SendMessageAsync("Unable to warn user. Do I have the permissions?"); + await chan.SendMessageAsync("Unable to warn user. Do I have the permissions?"); Log.Error("Unable to warn user for bad name: " + ex); } alertMessageIdsDic.Remove(msg.Id); @@ -154,14 +153,12 @@ public async Task OnReactionAdded(Cacheable message, ISocke builder.AddField("Reason", "Kicked by BOT for an inappropriate name"); builder.Color = Color.DarkOrange; await badUser.SendMessageAsync("", false, builder.Build()); - await badUser.KickAsync($"{badUser.Username}#{badUser.Discriminator} -> Inappropriate Name"); - - await channel.SendMessageAsync($"Got it! I kicked {badUser.Username}#{badUser.Discriminator}!"); + await chan.SendMessageAsync($"Got it! I kicked {badUser.Username}#{badUser.Discriminator}!"); } catch (Exception ex) { - await channel.SendMessageAsync("Unable to kick user. Do I have the permissions?"); + await chan.SendMessageAsync("Unable to kick user. Do I have the permissions?"); Log.Error("Unable to kick user for bad name: " + ex); } alertMessageIdsDic.Remove(msg.Id); @@ -171,18 +168,18 @@ public async Task OnReactionAdded(Cacheable message, ISocke try { await badUser.Guild.GetUser(badUser.Id).ModifyAsync(user => user.Nickname = ""); - await channel.SendMessageAsync($"Got it! I removed {badUser.Username}#{badUser.Discriminator}'s nickname!"); + await chan.SendMessageAsync($"Got it! I removed {badUser.Username}#{badUser.Discriminator}'s nickname!"); } catch (Exception ex) { - await channel.SendMessageAsync("Unable to remove their nickname. Do I have the permissions?"); + await chan.SendMessageAsync("Unable to remove their nickname. Do I have the permissions?"); Log.Error("Unable to remove nickname for bad name: " + ex); } alertMessageIdsDic.Remove(msg.Id); } else if (reaction.Emote.Name.Equals(NO_ACTION_EMOJI.Name)) { - await channel.SendMessageAsync($"Got it! I took no action against {badUser.Username}#{badUser.Discriminator}!"); + await chan.SendMessageAsync($"Got it! I took no action against {badUser.Username}#{badUser.Discriminator}!"); alertMessageIdsDic.Remove(msg.Id); } return; diff --git a/Floofbot/Services/UserRoleRetentionService.cs b/Floofbot/Services/UserRoleRetentionService.cs index 87304edd..8adb4ea6 100644 --- a/Floofbot/Services/UserRoleRetentionService.cs +++ b/Floofbot/Services/UserRoleRetentionService.cs @@ -7,6 +7,7 @@ using System; using System.Collections.Generic; using System.Data.OleDb; +using System.Diagnostics.Eventing.Reader; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -57,6 +58,7 @@ public string GetUserRoles(IGuildUser user) } public async Task RestoreUserRoles(IGuildUser user) { + var oldUserRoles = GetUserRoles(user); if (oldUserRoles != null) // user actually had old roles diff --git a/Floofbot/Services/WelcomeGateService.cs b/Floofbot/Services/WelcomeGateService.cs index b5049789..6646b66f 100644 --- a/Floofbot/Services/WelcomeGateService.cs +++ b/Floofbot/Services/WelcomeGateService.cs @@ -1,4 +1,5 @@ -using Discord.WebSocket; +using Discord; +using Discord.WebSocket; using Floofbot.Services.Repository; using Floofbot.Services.Repository.Models; using Serilog; @@ -14,8 +15,9 @@ public WelcomeGateService(FloofDataContext floofDb) { _floofDb = floofDb; } - public async Task HandleWelcomeGate(SocketGuildUser before, SocketGuildUser after) + public async Task HandleWelcomeGate(Cacheable _before, SocketGuildUser after) { + SocketGuildUser before = _before.Value; if (before.IsPending == after.IsPending) // no welcome gate change return; FloofDataContext floofDb = new FloofDataContext(); diff --git a/Floofbot/Services/WordFilterService.cs b/Floofbot/Services/WordFilterService.cs index 94406605..46a1def6 100644 --- a/Floofbot/Services/WordFilterService.cs +++ b/Floofbot/Services/WordFilterService.cs @@ -23,7 +23,7 @@ public List filteredWordsInName(FloofDataContext floofDb, string message } DateTime currentTime = DateTime.Now; - if (_lastRefreshedTime == null || currentTime.Subtract(_lastRefreshedTime).TotalMinutes >= 30) + if (currentTime.Subtract(_lastRefreshedTime).TotalMinutes >= 30) { _filteredWords = floofDb.FilteredWords.AsQueryable() .Where(x => x.ServerId == serverId).ToList(); @@ -61,7 +61,7 @@ public bool hasFilteredWord(FloofDataContext floofDb, string messageContent, ulo } DateTime currentTime = DateTime.Now; - if (_lastRefreshedTime == null || currentTime.Subtract(_lastRefreshedTime).TotalMinutes >= 30) + if (currentTime.Subtract(_lastRefreshedTime).TotalMinutes >= 30) { _filteredWords = floofDb.FilteredWords.AsQueryable() .Where(x => x.ServerId == serverId).ToList(); From 036e1507b666cd9ce9ddadb953409db4d9793381 Mon Sep 17 00:00:00 2001 From: ishauny Date: Sun, 17 Dec 2023 15:54:01 +0000 Subject: [PATCH 2/4] Revert gitignore update --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index f5a98009..1be20899 100644 --- a/.gitignore +++ b/.gitignore @@ -269,6 +269,5 @@ __pycache__/ Floofbot/config.yaml /Floofbot/floofData.db -/Floofbot/config.yaml.sample /Floofbot/config.yaml /Floofbot/app.config From 15ff1390241645ad488b2435249248c38818f1f6 Mon Sep 17 00:00:00 2001 From: jsh Date: Sun, 17 Dec 2023 23:42:18 -0800 Subject: [PATCH 3/4] Update .setup-dotnet action and .NET version --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0e48554d..eef41e88 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,9 +15,9 @@ jobs: - uses: actions/checkout@v2 - name: Setup .NET Core - uses: actions/setup-dotnet@v1 + uses: actions/setup-dotnet@v3 with: - dotnet-version: 3.1.301 + dotnet-version: 8.0.x - name: Build Windows binary run: dotnet publish --runtime win-x64 --configuration Release --output artifacts/win-x64 /p:PublishSingleFile=true Floofbot/Floofbot.csproj From e08a031378c5970087db40df31d82dda4770395c Mon Sep 17 00:00:00 2001 From: jsh Date: Thu, 21 Dec 2023 08:35:33 -0800 Subject: [PATCH 4/4] Add workflow dispatch trigger This enables running actions manually --- .github/workflows/main.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index eef41e88..14fd9839 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -5,6 +5,7 @@ on: branches: [ master ] pull_request: branches: [ master ] + workflow_dispatch: {} jobs: