From 4cc00e01daa0b534d0a19e9761f4f3b6e761a2bc Mon Sep 17 00:00:00 2001 From: Octol1ttle Date: Tue, 14 Feb 2023 23:17:20 +0500 Subject: [PATCH] Bugfixes: - Do not call RemoveUnbanAsync in guild tick loop if the user is not banned - Fix !clear message logs being reversed - Do not process MessageReceivedEvents by webhooks Signed-off-by: Octol1ttle --- Boyfriend.cs | 11 ++++------- CommandProcessor.cs | 6 +++--- Commands/ClearCommand.cs | 2 +- EventHandler.cs | 8 +++----- 4 files changed, 11 insertions(+), 16 deletions(-) diff --git a/Boyfriend.cs b/Boyfriend.cs index fb462c4..a10b1c5 100644 --- a/Boyfriend.cs +++ b/Boyfriend.cs @@ -31,18 +31,14 @@ public static class Boyfriend { (new Game("UNDEAD CORPORATION - Everything will freeze", ActivityType.Listening), new TimeSpan(0, 3, 18)), (new Game("Splatoon 3 - Candy-Coated Rocks", ActivityType.Listening), new TimeSpan(0, 2, 39)), (new Game("RetroSpecter - Overtime", ActivityType.Listening), new TimeSpan(0, 4, 33)), - (new Game("beatMARIO - Night of Knights", ActivityType.Listening), new TimeSpan(0, 4, 10)) + (new Game("SOOOO - Happppy song", ActivityType.Listening), new TimeSpan(0, 5, 24)) }; public static readonly DiscordSocketClient Client = new(Config); private static readonly List GuildTickTasks = new(); - public static void Main() { - InitAsync().GetAwaiter().GetResult(); - } - - private static async Task InitAsync() { + private static async Task Main() { var token = (await File.ReadAllTextAsync("token.txt")).Trim(); Client.Log += Log; @@ -149,7 +145,8 @@ public static class Boyfriend { foreach (var mData in data.MemberData.Values) { var user = guild.GetUser(mData.Id); - if (now >= mData.BannedUntil) _ = guild.RemoveBanAsync(mData.Id); + if (now >= mData.BannedUntil && await guild.GetBanAsync(mData.Id) is not null) + _ = guild.RemoveBanAsync(mData.Id); if (!mData.IsInGuild) continue; if (mData.MutedUntil is null && ulong.TryParse(config["StarterRole"], out var starterRoleId) diff --git a/CommandProcessor.cs b/CommandProcessor.cs index 3ee91b6..2492d38 100644 --- a/CommandProcessor.cs +++ b/CommandProcessor.cs @@ -8,7 +8,6 @@ using Discord.WebSocket; namespace Boyfriend; public sealed class CommandProcessor { - private static readonly string Mention = $"<@{Boyfriend.Client.CurrentUser.Id}>"; private static readonly TimeSpan Infinity = TimeSpan.FromMilliseconds(-1); public static readonly ICommand[] Commands = { @@ -58,9 +57,10 @@ public sealed class CommandProcessor { private async Task RunCommandOnLine(string line, string cleanLine, string prefix) { var prefixed = line.StartsWith(prefix); - if (!prefixed && !line.StartsWith(Mention)) return; + var mention = Boyfriend.Client.CurrentUser.Mention; + if (!prefixed && !line.StartsWith(mention)) return; foreach (var command in Commands) { - var lineNoMention = line.Remove(0, prefixed ? prefix.Length : Mention.Length); + var lineNoMention = line.Remove(0, prefixed ? prefix.Length : mention.Length); if (!command.Aliases.Contains(lineNoMention.Trim().Split()[0])) continue; var args = lineNoMention.Trim().Split().Skip(1).ToArray(); diff --git a/Commands/ClearCommand.cs b/Commands/ClearCommand.cs index dc0e4aa..065c08a 100644 --- a/Commands/ClearCommand.cs +++ b/Commands/ClearCommand.cs @@ -17,7 +17,7 @@ public sealed class ClearCommand : ICommand { var messages = await channel.GetMessagesAsync((int)(toDelete + 1)).FlattenAsync(); var user = (SocketGuildUser)cmd.Context.User; - var msgArray = messages.ToArray(); + var msgArray = messages.Reverse().ToArray(); await channel.DeleteMessagesAsync(msgArray, Utils.GetRequestOptions(user.ToString()!)); foreach (var msg in msgArray.Where(m => !m.Author.IsBot)) diff --git a/EventHandler.cs b/EventHandler.cs index 2e9690f..64bba20 100644 --- a/EventHandler.cs +++ b/EventHandler.cs @@ -83,16 +83,14 @@ public static class EventHandler { } private static Task MessageReceivedEvent(IDeletable messageParam) { - if (messageParam is not SocketUserMessage message) return Task.CompletedTask; + if (messageParam is not SocketUserMessage message || message.Author.IsWebhook) return Task.CompletedTask; _ = message.CleanContent.ToLower() switch { "whoami" => message.ReplyAsync("`nobody`"), "сука !!" => message.ReplyAsync("`root`"), "воооо" => message.ReplyAsync("`removing /...`"), - "op ??" => message.ReplyAsync( - "некоторые пасхальные цитаты которые вы могли найти были легально взяты у <@573772175572729876>"), - "++++" => message.ReplyAsync("#"), - _ => new CommandProcessor(message).HandleCommandAsync() + "++++" => message.ReplyAsync("#"), + _ => new CommandProcessor(message).HandleCommandAsync() }; return Task.CompletedTask; }