diff --git a/src/NadekoBot/Modules/Administration/Commands/LogCommand.cs b/src/NadekoBot/Modules/Administration/Commands/LogCommand.cs index 856d729d..a0eda9d0 100644 --- a/src/NadekoBot/Modules/Administration/Commands/LogCommand.cs +++ b/src/NadekoBot/Modules/Administration/Commands/LogCommand.cs @@ -23,14 +23,17 @@ namespace NadekoBot.Modules.Administration [Group] public class LogCommands : ModuleBase { + private const string clockEmojiUrl = "https://cdn.discordapp.com/attachments/155726317222887425/258309524966866945/clock.png"; + private static ShardedDiscordClient _client { get; } private static Logger _log { get; } private static string prettyCurrentTime => $"【{DateTime.Now:HH:mm:ss}】"; + private static string currentTime = $"{DateTime.Now:HH:mm:ss}"; public static ConcurrentDictionary GuildLogSettings { get; } - private static ConcurrentDictionary> UserPresenceUpdates { get; } = new ConcurrentDictionary>(); + private static ConcurrentDictionary> PresenceUpdates { get; } = new ConcurrentDictionary>(); private static Timer timerReference { get; } private IGoogleApiService _google { get; } @@ -50,20 +53,20 @@ namespace NadekoBot.Modules.Administration { try { - var keys = UserPresenceUpdates.Keys.ToList(); + var keys = PresenceUpdates.Keys.ToList(); await Task.WhenAll(keys.Select(async key => { List messages; - if (UserPresenceUpdates.TryRemove(key, out messages)) - try { await key.SendMessageAsync(string.Join(Environment.NewLine, messages)); } catch { } + if (PresenceUpdates.TryRemove(key, out messages)) + try { await key.SendConfirmAsync("Presence Updates", string.Join(Environment.NewLine, messages)); } catch { } })); } catch (Exception ex) { _log.Warn(ex); } - }, null, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10)); + }, null, TimeSpan.FromSeconds(15), TimeSpan.FromSeconds(15)); sw.Stop(); _log.Debug($"Loaded in {sw.Elapsed.TotalSeconds:F2}s"); @@ -91,33 +94,68 @@ namespace NadekoBot.Modules.Administration MuteCommands.UserUnmuted += MuteCommands_UserUnmuted; } - private static async void _client_UserUpdated(SocketUser before, SocketUser after) + private static async void _client_UserUpdated(SocketUser before, SocketUser uAfter) { try { - var str = ""; - if (before.Username != after.Username) - str = $"👤__**{before.Username}#{before.Discriminator}**__ **| Name Changed |** 🆔 `{before.Id}`\n\t\t`New:` **{after.ToString()}**"; - else if (before.AvatarUrl != after.AvatarUrl) - str = $"👤__**{before.Username}#{before.Discriminator}**__ **| Avatar Changed |** 🆔 `{before.Id}`\n\t🖼 {await NadekoBot.Google.ShortenUrl(before.AvatarUrl)} `=>` {await NadekoBot.Google.ShortenUrl(after.AvatarUrl)}"; + var after = uAfter as SocketGuildUser; - if (string.IsNullOrWhiteSpace(str)) + if (after == null) return; - var guildsMemberOf = NadekoBot.Client.GetGuilds().Where(g => g.Users.Select(u => u.Id).Contains(before.Id)).ToList(); - foreach (var g in guildsMemberOf) + var g = after.Guild; + + LogSetting logSetting; + if (!GuildLogSettings.TryGetValue(g.Id, out logSetting) + || (logSetting.UserUpdatedId == null)) + return; + + ITextChannel logChannel; + if ((logChannel = await TryGetLogChannel(g, logSetting, LogType.UserUpdated)) == null) + return; + + var embed = new EmbedBuilder(); + + + if (before.Username != after.Username) { - LogSetting logSetting; - if (!GuildLogSettings.TryGetValue(g.Id, out logSetting) - || (logSetting.UserUpdatedId == null)) - return; - - ITextChannel logChannel; - if ((logChannel = await TryGetLogChannel(g, logSetting, LogType.UserUpdated)) == null) - return; - - try { await logChannel.SendMessageAsync(str).ConfigureAwait(false); } catch { } + embed.WithAuthor(eab => eab.WithName("Username Changed")) + .WithTitle($"{before.Username}#{before.Discriminator} | {before.Id}") + .AddField(fb => fb.WithName("Old Name").WithValue($"{before.Username}").WithIsInline(true)) + .AddField(fb => fb.WithName("New Name").WithValue($"{after.Username}").WithIsInline(true)) + .WithFooter(fb => fb.WithText(currentTime)) + .WithOkColor(); } + else if (before.AvatarUrl != after.AvatarUrl) + { + embed.WithAuthor(eab => eab.WithName("Avatar Changed")) + .WithTitle($"{before.Username}#{before.Discriminator} | {before.Id}") + .WithThumbnailUrl(before.AvatarUrl) + .WithImageUrl(after.AvatarUrl) + .WithFooter(fb => fb.WithText(currentTime)) + .WithOkColor(); + } + else + { + return; + } + + await logChannel.EmbedAsync(embed).ConfigureAwait(false); + + //var guildsMemberOf = NadekoBot.Client.GetGuilds().Where(g => g.Users.Select(u => u.Id).Contains(before.Id)).ToList(); + //foreach (var g in guildsMemberOf) + //{ + // LogSetting logSetting; + // if (!GuildLogSettings.TryGetValue(g.Id, out logSetting) + // || (logSetting.UserUpdatedId == null)) + // return; + + // ITextChannel logChannel; + // if ((logChannel = await TryGetLogChannel(g, logSetting, LogType.UserUpdated)) == null) + // return; + + // try { await logChannel.SendMessageAsync(str).ConfigureAwait(false); } catch { } + //} } catch { } @@ -146,7 +184,7 @@ namespace NadekoBot.Modules.Administration if ((logChannel = await TryGetLogChannel(usr.Guild, logSetting, LogType.VoicePresenceTTS)) == null) return; - string str = null; + var str = ""; if (beforeVch?.Guild == afterVch?.Guild) { str = $"{usr.Username} moved from {beforeVch.Name} to {afterVch.Name}"; @@ -190,9 +228,15 @@ namespace NadekoBot.Modules.Administration mutes = "text and voice chat"; break; } - await logChannel.SendMessageAsync($"‼️🕕`{prettyCurrentTime}`👤__**{usr.Username}#{usr.Discriminator}**__🔇 **| User muted from the {mutes}. |** 🆔 `{usr.Id}`").ConfigureAwait(false); + + var embed = new EmbedBuilder().WithAuthor(eab => eab.WithName("User Muted from " + mutes)) + .WithTitle($"{usr.Username}#{usr.Discriminator} | {usr.Id}") + .WithFooter(fb => fb.WithText(currentTime)) + .WithOkColor(); + + await logChannel.EmbedAsync(embed).ConfigureAwait(false); } - catch (Exception ex) { _log.Warn(ex); } + catch { } } private static async void MuteCommands_UserUnmuted(IGuildUser usr, MuteCommands.MuteType muteType) @@ -221,9 +265,15 @@ namespace NadekoBot.Modules.Administration mutes = "text and voice chat"; break; } - await logChannel.SendMessageAsync($"‼️🕕`{prettyCurrentTime}`👤__**{usr.Username}#{usr.Discriminator}**__🔊 **| User unmuted from the {mutes}. |** 🆔 `{usr.Id}`").ConfigureAwait(false); + + var embed = new EmbedBuilder().WithAuthor(eab => eab.WithName("User Unmuted from " + mutes)) + .WithTitle($"{usr.Username}#{usr.Discriminator} | {usr.Id}") + .WithFooter(fb => fb.WithText($"{currentTime}")) + .WithOkColor(); + + await logChannel.EmbedAsync(embed).ConfigureAwait(false); } - catch (Exception ex) { _log.Warn(ex); } + catch { } } public static async Task TriggeredAntiProtection(IGuildUser[] users, PunishmentAction action, ProtectionType protection) @@ -245,22 +295,25 @@ namespace NadekoBot.Modules.Administration if (action == PunishmentAction.Mute) { punishment = "🔇 MUTED"; - //punishment = "MUTED"; } else if (action == PunishmentAction.Kick) { punishment = "☣ SOFT-BANNED (KICKED)"; - //punishment = "KICKED"; } else if (action == PunishmentAction.Ban) { punishment = "⛔️ BANNED"; - //punishment = "BANNED"; } - await logChannel.SendMessageAsync(String.Join("\n", users.Select(user => $"‼️ {Format.Bold(user.ToString())} got **{punishment}** due to __**{protection}**__ protection on **{user.Guild.Name}** server."))) - .ConfigureAwait(false); + + var embed = new EmbedBuilder().WithAuthor(eab => eab.WithName($"Anti-{protection}")) + .WithTitle($"Users " + punishment) + .WithDescription(String.Join("\n", users.Select(u => u.ToString()))) + .WithFooter(fb => fb.WithText($"{currentTime}")) + .WithOkColor(); + + await logChannel.EmbedAsync(embed).ConfigureAwait(false); } - catch (Exception ex) { _log.Warn(ex); } + catch { } } private static async void _client_GuildUserUpdated(SocketGuildUser before, SocketGuildUser after) @@ -275,27 +328,35 @@ namespace NadekoBot.Modules.Administration ITextChannel logChannel; if ((logChannel = await TryGetLogChannel(before.Guild, logSetting, LogType.UserUpdated)) == null) return; - var str = ""; + var embed = new EmbedBuilder().WithOkColor().WithFooter(efb => efb.WithText(currentTime)) + .WithTitle($"{before.Username}#{before.Discriminator} | {before.Id}"); if (before.Nickname != after.Nickname) - str = $"👤__**{before.Username}#{before.Discriminator}**__ **| Nickname Changed |** 🆔 `{before.Id}`\n\t\t`Old:` **{before.Nickname}**\n\t\t`New:` **{after.Nickname}**"; + { + embed.WithAuthor(eab => eab.WithName("Nickname Changed")) + + .AddField(efb => efb.WithName("Old Nickname").WithValue($"{before.Nickname}#{before.Discriminator}")) + .AddField(efb => efb.WithName("New Nickname").WithValue($"{after.Nickname}#{after.Discriminator}")); + } else if (!before.RoleIds.SequenceEqual(after.RoleIds)) { if (before.RoleIds.Count < after.RoleIds.Count) { - var diffRoles = after.RoleIds.Where(r => !before.RoleIds.Contains(r)).Select(r => "**" + before.Guild.GetRole(r).Name + "**"); - str = $"👤__**{before.ToString()}**__ **| User's Role Added |** 🆔 `{before.Id}`\n\t✅ {string.Join(", ", diffRoles).SanitizeMentions()}\n\t\t⚔ **`{string.Join(", ", after.GetRoles().Select(r => r.Name)).SanitizeMentions()}`** ⚔"; + var diffRoles = after.RoleIds.Where(r => !before.RoleIds.Contains(r)).Select(r => before.Guild.GetRole(r).Name); + embed.WithAuthor(eab => eab.WithName("User's Role Added")) + .WithDescription(string.Join(", ", diffRoles).SanitizeMentions()); } else if (before.RoleIds.Count > after.RoleIds.Count) { - var diffRoles = before.RoleIds.Where(r => !after.RoleIds.Contains(r)).Select(r => "**" + before.Guild.GetRole(r).Name + "**"); - str = $"👤__**{before.ToString()}**__ **| User's Role Removed |** 🆔 `{before.Id}`\n\t🚮 {string.Join(", ", diffRoles).SanitizeMentions()}\n\t\t⚔ **`{string.Join(", ", after.GetRoles().Select(r => r.Name)).SanitizeMentions()}`** ⚔"; + var diffRoles = before.RoleIds.Where(r => !after.RoleIds.Contains(r)).Select(r => before.Guild.GetRole(r).Name); + embed.WithAuthor(eab => eab.WithName("User's Role Removed")) + .WithDescription(string.Join(", ", diffRoles).SanitizeMentions()); } } else return; - try { await logChannel.SendMessageAsync($"🕔`{prettyCurrentTime}` " + str).ConfigureAwait(false); } catch (Exception ex) { _log.Warn(ex); } + await logChannel.EmbedAsync(embed).ConfigureAwait(false); } - catch (Exception ex) { _log.Warn(ex); } + catch { } } private static async void _client_ChannelUpdated(IChannel cbefore, IChannel cafter) @@ -316,16 +377,30 @@ namespace NadekoBot.Modules.Administration ITextChannel logChannel; if ((logChannel = await TryGetLogChannel(before.Guild, logSetting, LogType.ChannelUpdated)) == null) return; + + var embed = new EmbedBuilder().WithOkColor().WithFooter(efb => efb.WithText(currentTime)); + + var beforeTextChannel = cbefore as ITextChannel; + var afterTextChannel = cafter as ITextChannel; + if (before.Name != after.Name) - await logChannel.SendMessageAsync($@"🕓`{prettyCurrentTime}`ℹ️ **| Channel Name Changed |** #⃣ `{after.Name} ({after.Id})` - `Old:` {before.Name} - **`New:`** {after.Name}").ConfigureAwait(false); - else if ((before as ITextChannel).Topic != (after as ITextChannel).Topic) - await logChannel.SendMessageAsync($@"🕘`{prettyCurrentTime}`ℹ️ **| Channel Topic Changed |** #⃣ `{after.Name} ({after.Id})` - `Old:` {((ITextChannel)before).Topic} - **`New:`** {((ITextChannel)after).Topic}").ConfigureAwait(false); + { + embed.WithTitle("Channel Name Changed") + .AddField(efb => efb.WithName("Id").WithValue(before.Id.ToString())) + .AddField(efb => efb.WithName("Old Name").WithValue(before.Name)) + .AddField(efb => efb.WithName("New name").WithValue(after.Name)); + } + else if (beforeTextChannel?.Topic != afterTextChannel?.Topic) + { + embed.WithTitle("Channel Topic Changed") + .AddField(efb => efb.WithName("Id").WithValue(before.Id.ToString())) + .AddField(efb => efb.WithName("Old Topic").WithValue(beforeTextChannel.Topic)) + .AddField(efb => efb.WithName("New Topic").WithValue(afterTextChannel.Topic)); + } + + await logChannel.EmbedAsync(embed).ConfigureAwait(false); } - catch (Exception ex) { _log.Warn(ex); } + catch { } } private static async void _client_ChannelDestroyed(IChannel ich) @@ -346,9 +421,13 @@ namespace NadekoBot.Modules.Administration if ((logChannel = await TryGetLogChannel(ch.Guild, logSetting, LogType.ChannelDestroyed)) == null) return; - await logChannel.SendMessageAsync($"🕕`{prettyCurrentTime}`🗑 **| {(ch is IVoiceChannel ? "Voice" : "Text")} Channel Deleted #⃣ {ch.Name}** `({ch.Id})`").ConfigureAwait(false); + await logChannel.EmbedAsync(new EmbedBuilder() + .WithOkColor() + .WithTitle((ch is IVoiceChannel ? "Voice" : "Text") + " Channel Destroyed") + .WithDescription($"{ch.Name} | {ch.Id}") + .WithFooter(efb => efb.WithText(currentTime))).ConfigureAwait(false); } - catch (Exception ex) { _log.Warn(ex); } + catch { } } private static async void _client_ChannelCreated(IChannel ich) @@ -368,7 +447,11 @@ namespace NadekoBot.Modules.Administration if ((logChannel = await TryGetLogChannel(ch.Guild, logSetting, LogType.ChannelCreated)) == null) return; - await logChannel.SendMessageAsync($"🕓`{prettyCurrentTime}`🆕 **| {(ch is IVoiceChannel ? "Voice" : "Text")} Channel Created: #⃣ {ch.Name}** `({ch.Id})`").ConfigureAwait(false); + await logChannel.EmbedAsync(new EmbedBuilder() + .WithOkColor() + .WithTitle((ch is IVoiceChannel ? "Voice" : "Text") + " Channel Created") + .WithDescription($"{ch.Name} | {ch.Id}") + .WithFooter(efb => efb.WithText(currentTime))).ConfigureAwait(false); } catch (Exception ex) { _log.Warn(ex); } } @@ -410,12 +493,9 @@ namespace NadekoBot.Modules.Administration str = $"🎙`{prettyCurrentTime}`👤__**{usr.Username}#{usr.Discriminator}**__ has left **{beforeVch.Name}** voice channel."; } if (str != null) - UserPresenceUpdates.AddOrUpdate(logChannel, new List() { str }, (id, list) => { list.Add(str); return list; }); - } - catch (Exception ex) - { - _log.Warn(ex); + PresenceUpdates.AddOrUpdate(logChannel, new List() { str }, (id, list) => { list.Add(str); return list; }); } + catch { } } private static async void _client_UserPresenceUpdated(Optional optGuild, SocketUser usr, SocketPresence before, SocketPresence after) @@ -438,11 +518,11 @@ namespace NadekoBot.Modules.Administration return; string str; if (before.Status != after.Status) - str = $"🔵`{prettyCurrentTime}`👤__**{usr.Username}**__ is now **{after.Status}**."; + str = $"🎭`{prettyCurrentTime}`👤__**{usr.Username}**__ is now **{after.Status}**."; else str = $"👾`{prettyCurrentTime}`👤__**{usr.Username}**__ is now playing **{after.Game}**."; - UserPresenceUpdates.AddOrUpdate(logChannel, new List() { str }, (id, list) => { list.Add(str); return list; }); + PresenceUpdates.AddOrUpdate(logChannel, new List() { str }, (id, list) => { list.Add(str); return list; }); } catch { } } @@ -459,7 +539,13 @@ namespace NadekoBot.Modules.Administration ITextChannel logChannel; if ((logChannel = await TryGetLogChannel(usr.Guild, logSetting, LogType.UserLeft)) == null) return; - await logChannel.SendMessageAsync($"❗️🕛`{prettyCurrentTime}`👤__**{usr.Username}#{usr.Discriminator}**__❌ **| USER LEFT |** 🆔 `{usr.Id}`").ConfigureAwait(false); + + await logChannel.EmbedAsync(new EmbedBuilder() + .WithOkColor() + .WithTitle("User Left") + .WithThumbnailUrl(usr.AvatarUrl) + .WithDescription($"{usr} | {usr.Id}") + .WithFooter(efb => efb.WithText(currentTime))).ConfigureAwait(false); } catch { } } @@ -477,7 +563,12 @@ namespace NadekoBot.Modules.Administration if ((logChannel = await TryGetLogChannel(usr.Guild, logSetting, LogType.UserJoined)) == null) return; - await logChannel.SendMessageAsync($"❕🕓`{prettyCurrentTime}`👤__**{usr.Username}#{usr.Discriminator}**__✅ **| USER JOINED |** 🆔 `{usr.Id}`").ConfigureAwait(false); + await logChannel.EmbedAsync(new EmbedBuilder() + .WithOkColor() + .WithTitle("User Joined") + .WithThumbnailUrl(usr.AvatarUrl) + .WithDescription($"{usr} | {usr.Id}") + .WithFooter(efb => efb.WithText(currentTime))).ConfigureAwait(false); } catch (Exception ex) { _log.Warn(ex); } } @@ -495,7 +586,11 @@ namespace NadekoBot.Modules.Administration if ((logChannel = await TryGetLogChannel(guild, logSetting, LogType.UserUnbanned)) == null) return; - await logChannel.SendMessageAsync($"❕🕘`{prettyCurrentTime}`👤__**{usr.Username}#{usr.Discriminator}**__♻️ **| USER UN-BANNED |** 🆔 `{usr.Id}`").ConfigureAwait(false); + await logChannel.EmbedAsync(new EmbedBuilder() + .WithOkColor() + .WithTitle("User Unbanned") + .WithDescription($"{usr} | {usr.Id}") + .WithFooter(efb => efb.WithText(currentTime))).ConfigureAwait(false); } catch (Exception ex) { _log.Warn(ex); } } @@ -512,7 +607,11 @@ namespace NadekoBot.Modules.Administration ITextChannel logChannel; if ((logChannel = await TryGetLogChannel(guild, logSetting, LogType.UserBanned)) == null) return; - await logChannel.SendMessageAsync($"‼️🕕`{prettyCurrentTime}`👤__**{usr.Username}#{usr.Discriminator}**__🚫 **| USER BANNED |** 🆔 `{usr.Id}`").ConfigureAwait(false); + await logChannel.EmbedAsync(new EmbedBuilder() + .WithOkColor() + .WithTitle("User Banned") + .WithDescription($"{usr} | {usr.Id}") + .WithFooter(efb => efb.WithText(currentTime))).ConfigureAwait(false); } catch (Exception ex) { _log.Warn(ex); } } @@ -539,13 +638,19 @@ namespace NadekoBot.Modules.Administration ITextChannel logChannel; if ((logChannel = await TryGetLogChannel(channel.Guild, logSetting, LogType.MessageDeleted)) == null || logChannel.Id == msg.Id) return; - var str = $@"🕔`{prettyCurrentTime}`👤__**{msg.Author.Username}#{msg.Author.Discriminator}**__ **| Deleted Message |** 🆔 `{msg.Author.Id}` #⃣ `{channel.Name}` -🗑 {msg.Resolve(userHandling: TagHandling.FullName)}"; + var embed = new EmbedBuilder() + .WithOkColor() + .WithAuthor(eab => eab.WithName("Message Deleted")) + .WithTitle($"{msg.Author}") + .WithDescription($"{msg.Resolve(userHandling: TagHandling.FullName)}") + .AddField(efb => efb.WithName("Message Id").WithValue(msg.Id.ToString()).WithIsInline(false)) + .WithFooter(efb => efb.WithText(currentTime)); if (msg.Attachments.Any()) - str += $"{Environment.NewLine}📎 {string.Join(", ", msg.Attachments.Select(a => a.ProxyUrl))}"; - await logChannel.SendMessageAsync(str.SanitizeMentions()).ConfigureAwait(false); + embed.AddField(efb => efb.WithName("Attachments").WithValue(string.Join(", ", msg.Attachments.Select(a => a.ProxyUrl))).WithIsInline(false)); + + await logChannel.EmbedAsync(embed).ConfigureAwait(false); } - catch (Exception ex) { _log.Warn(ex); } + catch { } } private static async void _client_MessageUpdated(Optional optmsg, SocketMessage imsg2) @@ -576,11 +681,19 @@ namespace NadekoBot.Modules.Administration ITextChannel logChannel; if ((logChannel = await TryGetLogChannel(channel.Guild, logSetting, LogType.MessageUpdated)) == null || logChannel.Id == after.Channel.Id) return; - await logChannel.SendMessageAsync($@"🕔`{prettyCurrentTime}`👤__**{before.Author.Username}#{before.Author.Discriminator}**__ **| 📝 Edited Message |** 🆔 `{before.Author.Id}` #⃣ `{channel.Name}` - `Old:` {before.Resolve(userHandling: TagHandling.FullName).SanitizeMentions()} - **`New:`** {after.Resolve(userHandling: TagHandling.FullName).SanitizeMentions()}").ConfigureAwait(false); + + var embed = new EmbedBuilder() + .WithOkColor() + .WithAuthor(eab => eab.WithName("Message Updated")) + .WithTitle($"{after.Author}") + .AddField(efb => efb.WithName("Old Message").WithValue(before.Resolve(userHandling: TagHandling.FullName)).WithIsInline(false)) + .AddField(efb => efb.WithName("New Message").WithValue(after.Resolve(userHandling: TagHandling.FullName)).WithIsInline(false)) + .AddField(efb => efb.WithName("Message Id").WithValue(after.Id.ToString()).WithIsInline(false)) + .WithFooter(efb => efb.WithText(currentTime)); + + await logChannel.EmbedAsync(embed).ConfigureAwait(false); } - catch (Exception ex) { _log.Warn(ex); } + catch { } } public enum LogType @@ -763,6 +876,7 @@ namespace NadekoBot.Modules.Administration logSetting.ChannelUpdatedId = logSetting.LogUserPresenceId = logSetting.LogVoicePresenceId = + logSetting.UserMutedId = logSetting.LogVoicePresenceTTSId = (action.Value ? channel.Id : (ulong?)null); await uow.CompleteAsync().ConfigureAwait(false);