diff --git a/src/Mewdeko/Common/Autocompleters/SuggestionAutocomplete.cs b/src/Mewdeko/Common/Autocompleters/SuggestionAutocomplete.cs index 6eb5f01ab..8a645e8ac 100644 --- a/src/Mewdeko/Common/Autocompleters/SuggestionAutocomplete.cs +++ b/src/Mewdeko/Common/Autocompleters/SuggestionAutocomplete.cs @@ -5,21 +5,27 @@ namespace Mewdeko.Common.Autocompleters; public class SuggestionAutocompleter : AutocompleteHandler { + private readonly SuggestionsService suggest; + public SuggestionAutocompleter(SuggestionsService suggest) => this.suggest = suggest; - private readonly SuggestionsService suggest; - - public override Task GenerateSuggestionsAsync(IInteractionContext context, IAutocompleteInteraction interaction, IParameterInfo parameter, - IServiceProvider services) + public override Task GenerateSuggestionsAsync(IInteractionContext context, + IAutocompleteInteraction interaction, IParameterInfo parameter, IServiceProvider services) { var content = (string)interaction.Data.Current.Value; + var suggestions = suggest.Suggestions(context.Guild?.Id ?? 0); - return Task.FromResult(AutocompletionResult.FromSuccess(suggest.Suggestions(context.Guild?.Id ?? 0) - .Where(x => $"{x.SuggestionId}{x.Suggestion}".Contains(content)) + return Task.FromResult(AutocompletionResult.FromSuccess(suggestions + .Where(x => x.Suggestion.Contains(content) || x.SuggestionId.ToString().Contains(content)) .OrderByDescending(x => x.Suggestion.StartsWith(content)) .ThenByDescending(x => x.SuggestionId.ToString().StartsWith(content)) - .Select(x => - new AutocompleteResult($"{x.SuggestionId} | {x.Suggestion}".TrimTo(100), x.SuggestionId)))); + .Select(CreateAutocompleteResult))); + } + + private static AutocompleteResult CreateAutocompleteResult(SuggestionsModel x) + { + var formattedResult = $"{x.SuggestionId} | {x.Suggestion}".TrimTo(100); + return new AutocompleteResult(formattedResult, x.SuggestionId); } } \ No newline at end of file diff --git a/src/Mewdeko/Modules/Administration/Services/NewLogCommandService.cs b/src/Mewdeko/Modules/Administration/Services/NewLogCommandService.cs index 509c0cf5e..94f518620 100644 --- a/src/Mewdeko/Modules/Administration/Services/NewLogCommandService.cs +++ b/src/Mewdeko/Modules/Administration/Services/NewLogCommandService.cs @@ -1,4 +1,5 @@ -using Mewdeko.Modules.Moderation.Services; +using Discord.Rest; +using Mewdeko.Modules.Moderation.Services; using Microsoft.EntityFrameworkCore; namespace Mewdeko.Modules.Administration.Services; @@ -76,14 +77,13 @@ public NewLogCommandService(DbService db, IDataCache cache, DiscordSocketClient handler.MessageDeleted += OnMessageDeleted; handler.UserJoined += OnUserJoined; handler.UserLeft += OnUserLeft; - handler.UserBanned += OnUserBanned; - handler.UserUnbanned += OnUserUnbanned; handler.UserUpdated += OnUserUpdated; handler.ChannelCreated += OnChannelCreated; handler.ChannelDestroyed += OnChannelDestroyed; handler.ChannelUpdated += OnChannelUpdated; handler.UserVoiceStateUpdated += OnVoicePresence; handler.UserVoiceStateUpdated += OnVoicePresenceTts; + handler.AuditLogCreated += OnAuditLogCreated; muteService.UserMuted += OnUserMuted; muteService.UserUnmuted += OnUserUnmuted; @@ -101,6 +101,21 @@ public NewLogCommandService(DbService db, IDataCache cache, DiscordSocketClient .ToConcurrent(); } + private async Task OnAuditLogCreated(SocketAuditLogEntry args, SocketGuild arsg2) + { + if (args.Action == ActionType.Ban) + { + var data = args.Data as BanAuditLogData; + await OnUserBanned(data.Target, arsg2, args.User); + } + + if (args.Action == ActionType.Unban) + { + var data = args.Data as UnbanAuditLogData; + await OnUserUnbanned(data.Target, arsg2, args.User); + } + } + private async Task OnRoleCreated(SocketRole args) { @@ -843,7 +858,7 @@ private async Task OnUserLeft(IGuild guild, IUser arsg2) } } - private async Task OnUserBanned(SocketUser args, SocketGuild arsg2) + private async Task OnUserBanned(IUser args, SocketGuild arsg2, SocketUser bannedBy) { if (args is not SocketGuildUser usr) return; if (GuildLogSettings.TryGetValue(arsg2.Id, out var logSetting)) @@ -856,10 +871,6 @@ private async Task OnUserBanned(SocketUser args, SocketGuild arsg2) if (channel is null) return; - var auditLogs = await arsg2.GetAuditLogsAsync(1, actionType: ActionType.Ban).FlattenAsync(); - - var entry = auditLogs.FirstOrDefault(); - var eb = new EmbedBuilder() .WithOkColor() .WithTitle("User Banned") @@ -870,7 +881,7 @@ private async Task OnUserBanned(SocketUser args, SocketGuild arsg2) $"`Account Created:` {args.CreatedAt:dd/MM/yyyy}\n" + $"`Joined Server:` {usr.JoinedAt:dd/MM/yyyy}\n" + $"`Roles:` {string.Join(", ", usr.Roles.Select(x => x.Mention))}\n" + - $"`Banned By:` {entry.User.Mention} | {entry.User.Id}") + $"`Banned By:` {bannedBy.Mention} | {bannedBy.Id}") .WithThumbnailUrl(args.RealAvatarUrl().ToString()); var component = new ComponentBuilder().WithButton("View User (May not work)", style: ButtonStyle.Link, @@ -880,7 +891,7 @@ private async Task OnUserBanned(SocketUser args, SocketGuild arsg2) } } - private async Task OnUserUnbanned(SocketUser args, SocketGuild arsg2) + private async Task OnUserUnbanned(IUser args, SocketGuild arsg2, SocketUser unbannedBy) { if (GuildLogSettings.TryGetValue(arsg2.Id, out var logSetting)) { @@ -892,9 +903,6 @@ private async Task OnUserUnbanned(SocketUser args, SocketGuild arsg2) if (channel is null) return; - var auditLogs = await arsg2.GetAuditLogsAsync(1, actionType: ActionType.Unban).FlattenAsync(); - - var entry = auditLogs.FirstOrDefault(); var eb = new EmbedBuilder() .WithOkColor() @@ -904,7 +912,7 @@ private async Task OnUserUnbanned(SocketUser args, SocketGuild arsg2) $"`User Id:` {args.Id}" + $"`User Global Name:` {args.GlobalName ?? args.Username}" + $"`Account Created:` {args.CreatedAt:dd/MM/yyyy}\n" + - $"`Unbanned By:` {entry.User.Mention} | {entry.User.Id}") + $"`Unbanned By:` {unbannedBy.Mention} | {unbannedBy.Id}") .WithThumbnailUrl(args.RealAvatarUrl().ToString()); var component = new ComponentBuilder().WithButton("View User (May not work)", style: ButtonStyle.Link, diff --git a/src/Mewdeko/Services/Impl/EventHandler.cs b/src/Mewdeko/Services/Impl/EventHandler.cs index f6aee2a8d..246d561cb 100644 --- a/src/Mewdeko/Services/Impl/EventHandler.cs +++ b/src/Mewdeko/Services/Impl/EventHandler.cs @@ -7,9 +7,11 @@ public class EventHandler public delegate Task AsyncEventHandler(TEventArgs args, TArgs arsg2); - public delegate Task AsyncEventHandler(TEventArgs args, TArgs args2, TEvent args3); + public delegate Task AsyncEventHandler(TEventArgs args, TArgs args2, + TEvent args3); - public delegate Task AsyncEventHandler(TEventArgs args, TArgs args2, TEvent args3, TArgs2 args4); + public delegate Task AsyncEventHandler(TEventArgs args, TArgs args2, + TEvent args3, TArgs2 args4); // Actual events public event AsyncEventHandler? MessageReceived; @@ -22,7 +24,10 @@ public class EventHandler public event AsyncEventHandler, Cacheable>? MessageDeleted; public event AsyncEventHandler, SocketGuildUser>? GuildMemberUpdated; public event AsyncEventHandler, SocketMessage, ISocketMessageChannel>? MessageUpdated; - public event AsyncEventHandler>, Cacheable>? MessagesBulkDeleted; + + public event AsyncEventHandler>, Cacheable>? + MessagesBulkDeleted; + public event AsyncEventHandler? UserBanned; public event AsyncEventHandler? UserUnbanned; public event AsyncEventHandler? UserUpdated; @@ -31,8 +36,13 @@ public class EventHandler public event AsyncEventHandler? ChannelDestroyed; public event AsyncEventHandler? ChannelUpdated; public event AsyncEventHandler? RoleDeleted; - public event AsyncEventHandler, Cacheable, SocketReaction>? ReactionAdded; - public event AsyncEventHandler, Cacheable, SocketReaction>? ReactionRemoved; + + public event AsyncEventHandler, Cacheable, SocketReaction>? + ReactionAdded; + + public event AsyncEventHandler, Cacheable, SocketReaction>? + ReactionRemoved; + public event AsyncEventHandler, Cacheable>? ReactionsCleared; public event AsyncEventHandler? InteractionCreated; public event AsyncEventHandler, Cacheable>? UserIsTyping; @@ -43,6 +53,7 @@ public class EventHandler public event AsyncEventHandler>? ThreadDeleted; public event AsyncEventHandler? ThreadMemberJoined; public event AsyncEventHandler? ThreadMemberLeft; + public event AsyncEventHandler? AuditLogCreated; public event AsyncEventHandler? Ready; private readonly DiscordSocketClient client; @@ -82,9 +93,17 @@ public EventHandler(DiscordSocketClient client) client.ThreadDeleted += ClientOnThreadDeleted; client.ThreadMemberJoined += ClientOnThreadMemberJoined; client.ThreadMemberLeft += ClientOnThreadMemberLeft; + client.AuditLogCreated += ClientOnAuditLogCreated; client.Ready += ClientOnReady; } + private Task ClientOnAuditLogCreated(SocketAuditLogEntry arg1, SocketGuild arg2) + { + if (AuditLogCreated is not null) + _ = AuditLogCreated(arg1, arg2); + return Task.CompletedTask; + } + private Task ClientOnReady() { if (Ready is not null) @@ -130,7 +149,7 @@ private Task ClientOnThreadCreated(SocketThreadChannel arg) private Task ClientOnJoinedGuild(SocketGuild arg) { - if (PresenceUpdated is not null) + if (JoinedGuild is not null) _ = JoinedGuild(arg); return Task.CompletedTask; } @@ -162,14 +181,16 @@ private Task ClientOnReactionsCleared(Cacheable arg1, Cache return Task.CompletedTask; } - private Task ClientOnReactionRemoved(Cacheable arg1, Cacheable arg2, SocketReaction arg3) + private Task ClientOnReactionRemoved(Cacheable arg1, Cacheable arg2, + SocketReaction arg3) { if (ReactionRemoved is not null) _ = ReactionRemoved(arg1, arg2, arg3); return Task.CompletedTask; } - private Task ClientOnReactionAdded(Cacheable arg1, Cacheable arg2, SocketReaction arg3) + private Task ClientOnReactionAdded(Cacheable arg1, Cacheable arg2, + SocketReaction arg3) { if (ReactionAdded is not null) _ = ReactionAdded(arg1, arg2, arg3); @@ -232,7 +253,8 @@ private Task ClientOnUserBanned(SocketUser arg1, SocketGuild arg2) return Task.CompletedTask; } - private Task ClientOnMessagesBulkDeleted(IReadOnlyCollection> arg1, Cacheable arg2) + private Task ClientOnMessagesBulkDeleted(IReadOnlyCollection> arg1, + Cacheable arg2) { if (MessagesBulkDeleted is not null) _ = MessagesBulkDeleted(arg1, arg2);