Finished the work on 1.4, need to test everything now

This commit is contained in:
Master Kwoth
2017-06-01 05:12:00 +02:00
parent 3890816fa7
commit 1fa0aa3450
18 changed files with 1001 additions and 930 deletions

View File

@@ -1,19 +1,15 @@
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using NadekoBot.Attributes;
using NadekoBot.Extensions;
using NadekoBot.Modules.Permissions;
using NadekoBot.Services;
using NadekoBot.Services.Administration;
using NadekoBot.Services.Database.Models;
using NLog;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using static NadekoBot.Services.Administration.LogCommandService;
namespace NadekoBot.Modules.Administration
{
@@ -22,878 +18,13 @@ namespace NadekoBot.Modules.Administration
[Group]
public class LogCommands : NadekoSubmodule
{
private static DiscordShardedClient Client { get; }
private new static Logger _log { get; }
private readonly LogCommandService _lc;
private readonly DbService _db;
private static string PrettyCurrentTime => $"【{DateTime.Now:HH:mm:ss}】";
private static string CurrentTime => $"{DateTime.Now:HH:mm:ss}";
public static ConcurrentDictionary<ulong, LogSetting> GuildLogSettings { get; }
private static ConcurrentDictionary<ITextChannel, List<string>> PresenceUpdates { get; } = new ConcurrentDictionary<ITextChannel, List<string>>();
private static readonly Timer _timerReference;
static LogCommands()
public LogCommands(LogCommandService lc, DbService db)
{
Client = _client;
_log = LogManager.GetCurrentClassLogger();
var sw = Stopwatch.StartNew();
GuildLogSettings = new ConcurrentDictionary<ulong, LogSetting>(gcs
.ToDictionary(g => g.GuildId, g => g.LogSetting));
_timerReference = new Timer(async (state) =>
{
try
{
var keys = PresenceUpdates.Keys.ToList();
await Task.WhenAll(keys.Select(async key =>
{
if (PresenceUpdates.TryRemove(key, out List<string> messages))
try { await key.SendConfirmAsync(key.Guild.GetLogText("presence_updates"), string.Join(Environment.NewLine, messages)); }
catch
{
// ignored
}
}));
}
catch (Exception ex)
{
_log.Warn(ex);
}
}, null, TimeSpan.FromSeconds(15), TimeSpan.FromSeconds(15));
sw.Stop();
_log.Debug($"Loaded in {sw.Elapsed.TotalSeconds:F2}s");
//_client.MessageReceived += _client_MessageReceived;
Client.MessageUpdated += _client_MessageUpdated;
Client.MessageDeleted += _client_MessageDeleted;
Client.UserBanned += _client_UserBanned;
Client.UserUnbanned += _client_UserUnbanned;
Client.UserJoined += _client_UserJoined;
Client.UserLeft += _client_UserLeft;
Client.UserPresenceUpdated += _client_UserPresenceUpdated;
Client.UserVoiceStateUpdated += _client_UserVoiceStateUpdated;
Client.UserVoiceStateUpdated += _client_UserVoiceStateUpdated_TTS;
Client.GuildMemberUpdated += _client_GuildUserUpdated;
#if !GLOBAL_NADEKO
Client.UserUpdated += _client_UserUpdated;
#endif
Client.ChannelCreated += _client_ChannelCreated;
Client.ChannelDestroyed += _client_ChannelDestroyed;
Client.ChannelUpdated += _client_ChannelUpdated;
MuteCommands.UserMuted += MuteCommands_UserMuted;
MuteCommands.UserUnmuted += MuteCommands_UserUnmuted;
}
private static async Task _client_UserUpdated(SocketUser before, SocketUser uAfter)
{
try
{
var after = uAfter as SocketGuildUser;
if (after == null)
return;
var g = after.Guild;
if (!GuildLogSettings.TryGetValue(g.Id, out LogSetting 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)
{
embed.WithTitle("👥 " + g.GetLogText("username_changed"))
.WithDescription($"{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.AvatarId != after.AvatarId)
{
embed.WithTitle("👥" + g.GetLogText("avatar_changed"))
.WithDescription($"{before.Username}#{before.Discriminator} | {before.Id}")
.WithThumbnailUrl(before.GetAvatarUrl())
.WithImageUrl(after.GetAvatarUrl())
.WithFooter(fb => fb.WithText(CurrentTime))
.WithOkColor();
}
else
{
return;
}
await logChannel.EmbedAsync(embed).ConfigureAwait(false);
//var guildsMemberOf = _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
{
// ignored
}
}
private static async Task _client_UserVoiceStateUpdated_TTS(SocketUser iusr, SocketVoiceState before, SocketVoiceState after)
{
try
{
var usr = iusr as IGuildUser;
if (usr == null)
return;
var beforeVch = before.VoiceChannel;
var afterVch = after.VoiceChannel;
if (beforeVch == afterVch)
return;
if (!GuildLogSettings.TryGetValue(usr.Guild.Id, out LogSetting logSetting)
|| (logSetting.LogVoicePresenceTTSId == null))
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(usr.Guild, logSetting, LogType.VoicePresenceTTS)) == null)
return;
var str = "";
if (beforeVch?.Guild == afterVch?.Guild)
{
str = logChannel.Guild.GetLogText("moved", usr.Username, beforeVch?.Name, afterVch?.Name);
}
else if (beforeVch == null)
{
str = logChannel.Guild.GetLogText("joined", usr.Username, afterVch.Name);
}
else if (afterVch == null)
{
str = logChannel.Guild.GetLogText("left", usr.Username, beforeVch.Name);
}
var toDelete = await logChannel.SendMessageAsync(str, true).ConfigureAwait(false);
toDelete.DeleteAfter(5);
}
catch
{
// ignored
}
}
private static async void MuteCommands_UserMuted(IGuildUser usr, MuteCommands.MuteType muteType)
{
try
{
if (!GuildLogSettings.TryGetValue(usr.Guild.Id, out LogSetting logSetting)
|| (logSetting.UserMutedId == null))
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(usr.Guild, logSetting, LogType.UserMuted)) == null)
return;
var mutes = "";
var mutedLocalized = logChannel.Guild.GetLogText("muted_sn");
switch (muteType)
{
case MuteCommands.MuteType.Voice:
mutes = "🔇 " + logChannel.Guild.GetLogText("xmuted_voice", mutedLocalized);
break;
case MuteCommands.MuteType.Chat:
mutes = "🔇 " + logChannel.Guild.GetLogText("xmuted_text", mutedLocalized);
break;
case MuteCommands.MuteType.All:
mutes = "🔇 " + logChannel.Guild.GetLogText("xmuted_text_and_voice", mutedLocalized);
break;
}
var embed = new EmbedBuilder().WithAuthor(eab => eab.WithName(mutes))
.WithTitle($"{usr.Username}#{usr.Discriminator} | {usr.Id}")
.WithFooter(fb => fb.WithText(CurrentTime))
.WithOkColor();
await logChannel.EmbedAsync(embed).ConfigureAwait(false);
}
catch
{
// ignored
}
}
private static async void MuteCommands_UserUnmuted(IGuildUser usr, MuteCommands.MuteType muteType)
{
try
{
if (!GuildLogSettings.TryGetValue(usr.Guild.Id, out LogSetting logSetting)
|| (logSetting.UserMutedId == null))
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(usr.Guild, logSetting, LogType.UserMuted)) == null)
return;
var mutes = "";
var unmutedLocalized = logChannel.Guild.GetLogText("unmuted_sn");
switch (muteType)
{
case MuteCommands.MuteType.Voice:
mutes = "🔊 " + logChannel.Guild.GetLogText("xmuted_voice", unmutedLocalized);
break;
case MuteCommands.MuteType.Chat:
mutes = "🔊 " + logChannel.Guild.GetLogText("xmuted_text", unmutedLocalized);
break;
case MuteCommands.MuteType.All:
mutes = "🔊 " + logChannel.Guild.GetLogText("xmuted_text_and_voice", unmutedLocalized);
break;
}
var embed = new EmbedBuilder().WithAuthor(eab => eab.WithName(mutes))
.WithTitle($"{usr.Username}#{usr.Discriminator} | {usr.Id}")
.WithFooter(fb => fb.WithText($"{CurrentTime}"))
.WithOkColor();
await logChannel.EmbedAsync(embed).ConfigureAwait(false);
}
catch
{
// ignored
}
}
public static async Task TriggeredAntiProtection(IGuildUser[] users, PunishmentAction action, ProtectionType protection)
{
try
{
if (users.Length == 0)
return;
if (!GuildLogSettings.TryGetValue(users.First().Guild.Id, out LogSetting logSetting)
|| (logSetting.LogOtherId == null))
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(users.First().Guild, logSetting, LogType.Other)) == null)
return;
var punishment = "";
switch (action)
{
case PunishmentAction.Mute:
punishment = "🔇 " + logChannel.Guild.GetLogText("muted_pl").ToUpperInvariant();
break;
case PunishmentAction.Kick:
punishment = "👢 " + logChannel.Guild.GetLogText("kicked_pl").ToUpperInvariant();
break;
case PunishmentAction.Softban:
punishment = "☣ " + logChannel.Guild.GetLogText("soft_banned_pl").ToUpperInvariant();
break;
case PunishmentAction.Ban:
punishment = "⛔️ " + logChannel.Guild.GetLogText("banned_pl").ToUpperInvariant();
break;
}
var embed = new EmbedBuilder().WithAuthor(eab => eab.WithName($"🛡 Anti-{protection}"))
.WithTitle(logChannel.Guild.GetLogText("users") + " " + punishment)
.WithDescription(string.Join("\n", users.Select(u => u.ToString())))
.WithFooter(fb => fb.WithText($"{CurrentTime}"))
.WithOkColor();
await logChannel.EmbedAsync(embed).ConfigureAwait(false);
}
catch
{
// ignored
}
}
private static async Task _client_GuildUserUpdated(SocketGuildUser before, SocketGuildUser after)
{
try
{
if (!GuildLogSettings.TryGetValue(before.Guild.Id, out LogSetting logSetting)
|| (logSetting.UserUpdatedId == null))
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(before.Guild, logSetting, LogType.UserUpdated)) == null)
return;
var embed = new EmbedBuilder().WithOkColor().WithFooter(efb => efb.WithText(CurrentTime))
.WithTitle($"{before.Username}#{before.Discriminator} | {before.Id}");
if (before.Nickname != after.Nickname)
{
embed.WithAuthor(eab => eab.WithName("👥 " + logChannel.Guild.GetLogText("nick_change")))
.AddField(efb => efb.WithName(logChannel.Guild.GetLogText("old_nick")).WithValue($"{before.Nickname}#{before.Discriminator}"))
.AddField(efb => efb.WithName(logChannel.Guild.GetLogText("new_nick")).WithValue($"{after.Nickname}#{after.Discriminator}"));
}
else if (!before.Roles.SequenceEqual(after.Roles))
{
if (before.Roles.Count < after.Roles.Count)
{
var diffRoles = after.Roles.Where(r => !before.Roles.Contains(r)).Select(r => r.Name);
embed.WithAuthor(eab => eab.WithName("⚔ " + logChannel.Guild.GetLogText("user_role_add")))
.WithDescription(string.Join(", ", diffRoles).SanitizeMentions());
}
else if (before.Roles.Count > after.Roles.Count)
{
var diffRoles = before.Roles.Where(r => !after.Roles.Contains(r)).Select(r => r.Name);
embed.WithAuthor(eab => eab.WithName("⚔ " + logChannel.Guild.GetLogText("user_role_rem")))
.WithDescription(string.Join(", ", diffRoles).SanitizeMentions());
}
}
else
return;
await logChannel.EmbedAsync(embed).ConfigureAwait(false);
}
catch
{
// ignored
}
}
private static async Task _client_ChannelUpdated(IChannel cbefore, IChannel cafter)
{
try
{
var before = cbefore as IGuildChannel;
if (before == null)
return;
var after = (IGuildChannel)cafter;
if (!GuildLogSettings.TryGetValue(before.Guild.Id, out LogSetting logSetting)
|| (logSetting.ChannelUpdatedId == null)
|| logSetting.IgnoredChannels.Any(ilc => ilc.ChannelId == after.Id))
return;
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)
{
embed.WithTitle(" " + logChannel.Guild.GetLogText("ch_name_change"))
.WithDescription($"{after} | {after.Id}")
.AddField(efb => efb.WithName(logChannel.Guild.GetLogText("ch_old_name")).WithValue(before.Name));
}
else if (beforeTextChannel?.Topic != afterTextChannel?.Topic)
{
embed.WithTitle(" " + logChannel.Guild.GetLogText("ch_topic_change"))
.WithDescription($"{after} | {after.Id}")
.AddField(efb => efb.WithName(logChannel.Guild.GetLogText("old_topic")).WithValue(beforeTextChannel?.Topic ?? "-"))
.AddField(efb => efb.WithName(logChannel.Guild.GetLogText("new_topic")).WithValue(afterTextChannel?.Topic ?? "-"));
}
else
return;
await logChannel.EmbedAsync(embed).ConfigureAwait(false);
}
catch
{
// ignored
}
}
private static async Task _client_ChannelDestroyed(IChannel ich)
{
try
{
var ch = ich as IGuildChannel;
if (ch == null)
return;
if (!GuildLogSettings.TryGetValue(ch.Guild.Id, out LogSetting logSetting)
|| (logSetting.ChannelDestroyedId == null)
|| logSetting.IgnoredChannels.Any(ilc => ilc.ChannelId == ch.Id))
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(ch.Guild, logSetting, LogType.ChannelDestroyed)) == null)
return;
string title;
if (ch is IVoiceChannel)
{
title = logChannel.Guild.GetLogText("voice_chan_destroyed");
}
else
title = logChannel.Guild.GetLogText("text_chan_destroyed");
await logChannel.EmbedAsync(new EmbedBuilder()
.WithOkColor()
.WithTitle("🆕 " + title)
.WithDescription($"{ch.Name} | {ch.Id}")
.WithFooter(efb => efb.WithText(CurrentTime))).ConfigureAwait(false);
}
catch
{
// ignored
}
}
private static async Task _client_ChannelCreated(IChannel ich)
{
try
{
var ch = ich as IGuildChannel;
if (ch == null)
return;
if (!GuildLogSettings.TryGetValue(ch.Guild.Id, out LogSetting logSetting)
|| (logSetting.ChannelCreatedId == null))
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(ch.Guild, logSetting, LogType.ChannelCreated)) == null)
return;
string title;
if (ch is IVoiceChannel)
{
title = logChannel.Guild.GetLogText("voice_chan_created");
}
else
title = logChannel.Guild.GetLogText("text_chan_created");
await logChannel.EmbedAsync(new EmbedBuilder()
.WithOkColor()
.WithTitle("🆕 " + title)
.WithDescription($"{ch.Name} | {ch.Id}")
.WithFooter(efb => efb.WithText(CurrentTime))).ConfigureAwait(false);
}
catch (Exception ex) { _log.Warn(ex); }
}
private static async Task _client_UserVoiceStateUpdated(SocketUser iusr, SocketVoiceState before, SocketVoiceState after)
{
try
{
var usr = iusr as IGuildUser;
if (usr == null)
return;
var beforeVch = before.VoiceChannel;
var afterVch = after.VoiceChannel;
if (beforeVch == afterVch)
return;
if (!GuildLogSettings.TryGetValue(usr.Guild.Id, out LogSetting logSetting)
|| (logSetting.LogVoicePresenceId == null))
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(usr.Guild, logSetting, LogType.VoicePresence)) == null)
return;
string str = null;
if (beforeVch?.Guild == afterVch?.Guild)
{
str = "🎙" + Format.Code(PrettyCurrentTime) + logChannel.Guild.GetLogText("user_vmoved",
"👤" + Format.Bold(usr.Username + "#" + usr.Discriminator),
Format.Bold(beforeVch?.Name ?? ""), Format.Bold(afterVch?.Name ?? ""));
}
else if (beforeVch == null)
{
str = "🎙" + Format.Code(PrettyCurrentTime) + logChannel.Guild.GetLogText("user_vjoined",
"👤" + Format.Bold(usr.Username + "#" + usr.Discriminator),
Format.Bold(afterVch.Name ?? ""));
}
else if (afterVch == null)
{
str = "🎙" + Format.Code(PrettyCurrentTime) + logChannel.Guild.GetLogText("user_vleft",
"👤" + Format.Bold(usr.Username + "#" + usr.Discriminator),
Format.Bold(beforeVch.Name ?? ""));
}
if (str != null)
PresenceUpdates.AddOrUpdate(logChannel, new List<string>() { str }, (id, list) => { list.Add(str); return list; });
}
catch
{
// ignored
}
}
private static async Task _client_UserPresenceUpdated(Optional<SocketGuild> optGuild, SocketUser usr, SocketPresence before, SocketPresence after)
{
try
{
var guild = optGuild.GetValueOrDefault() ?? (usr as SocketGuildUser)?.Guild;
if (guild == null)
return;
if (!GuildLogSettings.TryGetValue(guild.Id, out LogSetting logSetting)
|| (logSetting.LogUserPresenceId == null)
|| before.Status == after.Status)
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(guild, logSetting, LogType.UserPresence)) == null)
return;
string str = "";
if (before.Status != after.Status)
str = "🎭" + Format.Code(PrettyCurrentTime) +
logChannel.Guild.GetLogText("user_status_change",
"👤" + Format.Bold(usr.Username),
Format.Bold(after.Status.ToString()));
//if (before.Game?.Name != after.Game?.Name)
//{
// if (str != "")
// str += "\n";
// str += $"👾`{prettyCurrentTime}`👤__**{usr.Username}**__ is now playing **{after.Game?.Name}**.";
//}
PresenceUpdates.AddOrUpdate(logChannel, new List<string>() { str }, (id, list) => { list.Add(str); return list; });
}
catch
{
// ignored
}
}
private static async Task _client_UserLeft(IGuildUser usr)
{
try
{
if (!GuildLogSettings.TryGetValue(usr.Guild.Id, out LogSetting logSetting)
|| (logSetting.UserLeftId == null))
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(usr.Guild, logSetting, LogType.UserLeft)) == null)
return;
await logChannel.EmbedAsync(new EmbedBuilder()
.WithOkColor()
.WithTitle("❌ " + logChannel.Guild.GetLogText("user_left"))
.WithThumbnailUrl(usr.GetAvatarUrl())
.WithDescription(usr.ToString())
.AddField(efb => efb.WithName("Id").WithValue(usr.Id.ToString()))
.WithFooter(efb => efb.WithText(CurrentTime))).ConfigureAwait(false);
}
catch
{
// ignored
}
}
private static async Task _client_UserJoined(IGuildUser usr)
{
try
{
if (!GuildLogSettings.TryGetValue(usr.Guild.Id, out LogSetting logSetting)
|| (logSetting.UserJoinedId == null))
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(usr.Guild, logSetting, LogType.UserJoined)) == null)
return;
await logChannel.EmbedAsync(new EmbedBuilder()
.WithOkColor()
.WithTitle("✅ " + logChannel.Guild.GetLogText("user_joined"))
.WithThumbnailUrl(usr.GetAvatarUrl())
.WithDescription($"{usr}")
.AddField(efb => efb.WithName("Id").WithValue(usr.Id.ToString()))
.WithFooter(efb => efb.WithText(CurrentTime))).ConfigureAwait(false);
}
catch (Exception ex) { _log.Warn(ex); }
}
private static async Task _client_UserUnbanned(IUser usr, IGuild guild)
{
try
{
if (!GuildLogSettings.TryGetValue(guild.Id, out LogSetting logSetting)
|| (logSetting.UserUnbannedId == null))
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(guild, logSetting, LogType.UserUnbanned)) == null)
return;
await logChannel.EmbedAsync(new EmbedBuilder()
.WithOkColor()
.WithTitle("♻️ " + logChannel.Guild.GetLogText("user_unbanned"))
.WithThumbnailUrl(usr.GetAvatarUrl())
.WithDescription(usr.ToString())
.AddField(efb => efb.WithName("Id").WithValue(usr.Id.ToString()))
.WithFooter(efb => efb.WithText(CurrentTime))).ConfigureAwait(false);
}
catch (Exception ex) { _log.Warn(ex); }
}
private static async Task _client_UserBanned(IUser usr, IGuild guild)
{
try
{
if (!GuildLogSettings.TryGetValue(guild.Id, out LogSetting logSetting)
|| (logSetting.UserBannedId == null))
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(guild, logSetting, LogType.UserBanned)) == null)
return;
await logChannel.EmbedAsync(new EmbedBuilder()
.WithOkColor()
.WithTitle("🚫 " + logChannel.Guild.GetLogText("user_banned"))
.WithThumbnailUrl(usr.GetAvatarUrl())
.WithDescription(usr.ToString())
.AddField(efb => efb.WithName("Id").WithValue(usr.Id.ToString()))
.WithFooter(efb => efb.WithText(CurrentTime))).ConfigureAwait(false);
}
catch (Exception ex) { _log.Warn(ex); }
}
private static async Task _client_MessageDeleted(Cacheable<IMessage, ulong> optMsg, ISocketMessageChannel ch)
{
try
{
var msg = (optMsg.HasValue ? optMsg.Value : null) as IUserMessage;
if (msg == null || msg.IsAuthor())
return;
var channel = ch as ITextChannel;
if (channel == null)
return;
if (!GuildLogSettings.TryGetValue(channel.Guild.Id, out LogSetting logSetting)
|| (logSetting.MessageDeletedId == null)
|| logSetting.IgnoredChannels.Any(ilc => ilc.ChannelId == channel.Id))
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(channel.Guild, logSetting, LogType.MessageDeleted)) == null || logChannel.Id == msg.Id)
return;
var embed = new EmbedBuilder()
.WithOkColor()
.WithTitle("🗑 " + logChannel.Guild.GetLogText("msg_del", ((ITextChannel)msg.Channel).Name))
.WithDescription(msg.Author.ToString())
.AddField(efb => efb.WithName(logChannel.Guild.GetLogText("content")).WithValue(string.IsNullOrWhiteSpace(msg.Content) ? "-" : msg.Resolve(userHandling: TagHandling.FullName)).WithIsInline(false))
.AddField(efb => efb.WithName("Id").WithValue(msg.Id.ToString()).WithIsInline(false))
.WithFooter(efb => efb.WithText(CurrentTime));
if (msg.Attachments.Any())
embed.AddField(efb => efb.WithName(logChannel.Guild.GetLogText("attachments")).WithValue(string.Join(", ", msg.Attachments.Select(a => a.Url))).WithIsInline(false));
await logChannel.EmbedAsync(embed).ConfigureAwait(false);
}
catch (Exception ex)
{
_log.Warn(ex);
// ignored
}
}
private static async Task _client_MessageUpdated(Cacheable<IMessage, ulong> optmsg, SocketMessage imsg2, ISocketMessageChannel ch)
{
try
{
var after = imsg2 as IUserMessage;
if (after == null || after.IsAuthor())
return;
var before = (optmsg.HasValue ? optmsg.Value : null) as IUserMessage;
if (before == null)
return;
var channel = ch as ITextChannel;
if (channel == null)
return;
if (before.Content == after.Content)
return;
if (!GuildLogSettings.TryGetValue(channel.Guild.Id, out LogSetting logSetting)
|| (logSetting.MessageUpdatedId == null)
|| logSetting.IgnoredChannels.Any(ilc => ilc.ChannelId == channel.Id))
return;
ITextChannel logChannel;
if ((logChannel = await TryGetLogChannel(channel.Guild, logSetting, LogType.MessageUpdated)) == null || logChannel.Id == after.Channel.Id)
return;
var embed = new EmbedBuilder()
.WithOkColor()
.WithTitle("📝 " + logChannel.Guild.GetLogText("msg_update", ((ITextChannel)after.Channel).Name))
.WithDescription(after.Author.ToString())
.AddField(efb => efb.WithName(logChannel.Guild.GetLogText("old_msg")).WithValue(string.IsNullOrWhiteSpace(before.Content) ? "-" : before.Resolve(userHandling: TagHandling.FullName)).WithIsInline(false))
.AddField(efb => efb.WithName(logChannel.Guild.GetLogText("new_msg")).WithValue(string.IsNullOrWhiteSpace(after.Content) ? "-" : after.Resolve(userHandling: TagHandling.FullName)).WithIsInline(false))
.AddField(efb => efb.WithName("Id").WithValue(after.Id.ToString()).WithIsInline(false))
.WithFooter(efb => efb.WithText(CurrentTime));
await logChannel.EmbedAsync(embed).ConfigureAwait(false);
}
catch
{
// ignored
}
}
public enum LogType
{
Other,
MessageUpdated,
MessageDeleted,
UserJoined,
UserLeft,
UserBanned,
UserUnbanned,
UserUpdated,
ChannelCreated,
ChannelDestroyed,
ChannelUpdated,
UserPresence,
VoicePresence,
VoicePresenceTTS,
UserMuted
};
private static async Task<ITextChannel> TryGetLogChannel(IGuild guild, LogSetting logSetting, LogType logChannelType)
{
ulong? id = null;
switch (logChannelType)
{
case LogType.Other:
id = logSetting.LogOtherId;
break;
case LogType.MessageUpdated:
id = logSetting.MessageUpdatedId;
break;
case LogType.MessageDeleted:
id = logSetting.MessageDeletedId;
break;
case LogType.UserJoined:
id = logSetting.UserJoinedId;
break;
case LogType.UserLeft:
id = logSetting.UserLeftId;
break;
case LogType.UserBanned:
id = logSetting.UserBannedId;
break;
case LogType.UserUnbanned:
id = logSetting.UserUnbannedId;
break;
case LogType.UserUpdated:
id = logSetting.UserUpdatedId;
break;
case LogType.ChannelCreated:
id = logSetting.ChannelCreatedId;
break;
case LogType.ChannelDestroyed:
id = logSetting.ChannelDestroyedId;
break;
case LogType.ChannelUpdated:
id = logSetting.ChannelUpdatedId;
break;
case LogType.UserPresence:
id = logSetting.LogUserPresenceId;
break;
case LogType.VoicePresence:
id = logSetting.LogVoicePresenceId;
break;
case LogType.VoicePresenceTTS:
id = logSetting.LogVoicePresenceTTSId;
break;
case LogType.UserMuted:
id = logSetting.UserMutedId;
break;
}
if (!id.HasValue)
{
UnsetLogSetting(guild.Id, logChannelType);
return null;
}
var channel = await guild.GetTextChannelAsync(id.Value).ConfigureAwait(false);
if (channel == null)
{
UnsetLogSetting(guild.Id, logChannelType);
return null;
}
else
return channel;
}
private static void UnsetLogSetting(ulong guildId, LogType logChannelType)
{
using (var uow = _db.UnitOfWork)
{
var newLogSetting = uow.GuildConfigs.LogSettingsFor(guildId).LogSetting;
switch (logChannelType)
{
case LogType.Other:
newLogSetting.LogOtherId = null;
break;
case LogType.MessageUpdated:
newLogSetting.MessageUpdatedId = null;
break;
case LogType.MessageDeleted:
newLogSetting.MessageDeletedId = null;
break;
case LogType.UserJoined:
newLogSetting.UserJoinedId = null;
break;
case LogType.UserLeft:
newLogSetting.UserLeftId = null;
break;
case LogType.UserBanned:
newLogSetting.UserBannedId = null;
break;
case LogType.UserUnbanned:
newLogSetting.UserUnbannedId = null;
break;
case LogType.UserUpdated:
newLogSetting.UserUpdatedId = null;
break;
case LogType.UserMuted:
newLogSetting.UserMutedId = null;
break;
case LogType.ChannelCreated:
newLogSetting.ChannelCreatedId = null;
break;
case LogType.ChannelDestroyed:
newLogSetting.ChannelDestroyedId = null;
break;
case LogType.ChannelUpdated:
newLogSetting.ChannelUpdatedId = null;
break;
case LogType.UserPresence:
newLogSetting.LogUserPresenceId = null;
break;
case LogType.VoicePresence:
newLogSetting.LogVoicePresenceId = null;
break;
case LogType.VoicePresenceTTS:
newLogSetting.LogVoicePresenceTTSId = null;
break;
}
GuildLogSettings.AddOrUpdate(guildId, newLogSetting, (gid, old) => newLogSetting);
uow.Complete();
}
_lc = lc;
_db = db;
}
public enum EnableDisable
@@ -913,7 +44,7 @@ namespace NadekoBot.Modules.Administration
using (var uow = _db.UnitOfWork)
{
logSetting = uow.GuildConfigs.LogSettingsFor(channel.Guild.Id).LogSetting;
GuildLogSettings.AddOrUpdate(channel.Guild.Id, (id) => logSetting, (id, old) => logSetting);
_lc.GuildLogSettings.AddOrUpdate(channel.Guild.Id, (id) => logSetting, (id, old) => logSetting);
logSetting.LogOtherId =
logSetting.MessageUpdatedId =
logSetting.MessageDeletedId =
@@ -949,7 +80,7 @@ namespace NadekoBot.Modules.Administration
using (var uow = _db.UnitOfWork)
{
var config = uow.GuildConfigs.LogSettingsFor(channel.Guild.Id);
LogSetting logSetting = GuildLogSettings.GetOrAdd(channel.Guild.Id, (id) => config.LogSetting);
LogSetting logSetting = _lc.GuildLogSettings.GetOrAdd(channel.Guild.Id, (id) => config.LogSetting);
removed = logSetting.IgnoredChannels.RemoveWhere(ilc => ilc.ChannelId == channel.Id);
config.LogSetting.IgnoredChannels.RemoveWhere(ilc => ilc.ChannelId == channel.Id);
if (removed == 0)
@@ -989,7 +120,7 @@ namespace NadekoBot.Modules.Administration
using (var uow = _db.UnitOfWork)
{
var logSetting = uow.GuildConfigs.LogSettingsFor(channel.Guild.Id).LogSetting;
GuildLogSettings.AddOrUpdate(channel.Guild.Id, (id) => logSetting, (id, old) => logSetting);
_lc.GuildLogSettings.AddOrUpdate(channel.Guild.Id, (id) => logSetting, (id, old) => logSetting);
switch (type)
{
case LogType.Other:
@@ -1049,13 +180,4 @@ namespace NadekoBot.Modules.Administration
}
}
}
public static class GuildExtensions
{
public static string GetLogText(this IGuild guild, string key, params object[] replacements)
=> NadekoTopLevelModule.GetTextStatic(key,
NadekoBot.Localization.GetCultureInfo(guild),
typeof(Administration).Name.ToLowerInvariant(),
replacements);
}
}

View File

@@ -101,8 +101,8 @@ namespace NadekoBot.Modules.Help
if (alias != null)
str += string.Format(" **/ `{0}`**", Prefix + alias);
var embed = new EmbedBuilder()
.AddField(fb => fb.WithName(str).WithValue($"{com.RealSummary()} {GetCommandRequirements(com)}").WithIsInline(true))
.AddField(fb => fb.WithName(GetText("usage")).WithValue(com.RealRemarks()).WithIsInline(false))
.AddField(fb => fb.WithName(str).WithValue($"{com.RealSummary(Prefix)} {GetCommandRequirements(com)}").WithIsInline(true))
.AddField(fb => fb.WithName(GetText("usage")).WithValue(com.RealRemarks(Prefix)).WithIsInline(false))
.WithColor(NadekoBot.OkColor);
await channel.EmbedAsync(embed).ConfigureAwait(false);
}

View File

@@ -94,7 +94,7 @@ namespace NadekoBot.Modules.Permissions
.Select(p =>
{
var str =
$"`{p.Index + 1}.` {Format.Bold(p.GetCommand((SocketGuild) Context.Guild))}";
$"`{p.Index + 1}.` {Format.Bold(p.GetCommand(Prefix, (SocketGuild) Context.Guild))}";
if (p.Index == 0)
str += $" [{GetText("uneditable")}]";
return str;
@@ -125,7 +125,7 @@ namespace NadekoBot.Modules.Permissions
}
await ReplyConfirmLocalized("removed",
index + 1,
Format.Code(p.GetCommand((SocketGuild) Context.Guild))).ConfigureAwait(false);
Format.Code(p.GetCommand(Prefix, (SocketGuild) Context.Guild))).ConfigureAwait(false);
}
catch (IndexOutOfRangeException)
{
@@ -171,7 +171,7 @@ namespace NadekoBot.Modules.Permissions
_service.UpdateCache(config);
}
await ReplyConfirmLocalized("moved_permission",
Format.Code(fromPerm.GetCommand((SocketGuild) Context.Guild)),
Format.Code(fromPerm.GetCommand(Prefix, (SocketGuild) Context.Guild)),
++from,
++to)
.ConfigureAwait(false);

View File

@@ -18,13 +18,11 @@ namespace NadekoBot.Modules.Utility
{
private readonly DiscordShardedClient _client;
private readonly IStatsService _stats;
private readonly CommandHandler _cmdHandler;
public InfoCommands(DiscordShardedClient client, IStatsService stats, CommandHandler ch)
{
_client = client;
_stats = stats;
_cmdHandler = ch;
}
[NadekoCommand, Usage, Description, Aliases]