Logging around 70% done

This commit is contained in:
Kwoth 2016-09-06 03:59:00 +02:00
parent 6d50336368
commit b78686141c
5 changed files with 676 additions and 497 deletions

View File

@ -1,466 +1,485 @@
//using Discord; //using Discord;
//using Discord.Commands; //using Discord.Commands;
//using Discord.WebSocket; //using NadekoBot.Classes;
//using NadekoBot.Attributes;
//using NadekoBot.Extensions; //using NadekoBot.Extensions;
//using NadekoBot.Services; //using NadekoBot.Modules.Permissions.Classes;
//using NadekoBot.Services.Database;
//using NadekoBot.Services.Database.Models;
//using NLog;
//using System; //using System;
//using System.Collections.Concurrent; //using System.Collections.Concurrent;
//using System.Collections.Generic; //using System.Collections.Generic;
//using System.Linq; //using System.Linq;
//using System.Reflection;
//using System.Threading.Tasks; //using System.Threading.Tasks;
////todo DB
////todo Add flags for every event
//namespace NadekoBot.Modules.Administration //namespace NadekoBot.Modules.Administration
//{ //{
// public partial class Administration // public class LogCommand : DiscordCommand
// { // {
// [Group]
// public class LogCommands
// {
// private DiscordSocketClient _client { get; }
// private Logger _log { get; }
// private string prettyCurrentTime => $"【{DateTime.Now:HH:mm:ss}】"; // private string prettyCurrentTime => $"【{DateTime.Now:HH:mm:ss}】";
// public ConcurrentDictionary<ulong, LogSetting> GuildLogSettings { get; } // private ConcurrentBag<KeyValuePair<Channel, string>> voicePresenceUpdates = new ConcurrentBag<KeyValuePair<Channel, string>>();
// public LogCommands(DiscordSocketClient client) // public LogCommand(DiscordModule module) : base(module)
// { // {
// _client = client; // NadekoBot.Client.MessageReceived += MsgRecivd;
// _log = LogManager.GetCurrentClassLogger(); // NadekoBot.Client.MessageDeleted += MsgDltd;
// NadekoBot.Client.MessageUpdated += MsgUpdtd;
// NadekoBot.Client.UserUpdated += UsrUpdtd;
// NadekoBot.Client.UserBanned += UsrBanned;
// NadekoBot.Client.UserLeft += UsrLeft;
// NadekoBot.Client.UserJoined += UsrJoined;
// NadekoBot.Client.UserUnbanned += UsrUnbanned;
// NadekoBot.Client.ChannelCreated += ChannelCreated;
// NadekoBot.Client.ChannelDestroyed += ChannelDestroyed;
// NadekoBot.Client.ChannelUpdated += ChannelUpdated;
// using (var uow = DbHandler.UnitOfWork())
// NadekoBot.Client.MessageReceived += async (s, e) =>
// { // {
// GuildLogSettings = new ConcurrentDictionary<ulong, LogSetting>(uow.GuildConfigs // if (e.Channel.IsPrivate || umsg.Author.Id == NadekoBot.Client.CurrentUser.Id)
// .GetAll() // return;
// .ToDictionary(g => g.GuildId, g => g.LogSetting)); // if (!SpecificConfigurations.Default.Of(e.Server.Id).SendPrivateMessageOnMention) return;
// try
// {
// var usr = e.Message.MentionedUsers.FirstOrDefault(u => u != umsg.Author);
// if (usr?.Status != UserStatus.Offline)
// return;
// await channel.SendMessageAsync($"User `{usr.Name}` is offline. PM sent.").ConfigureAwait(false);
// await usr.SendMessageAsync(
// $"User `{umsg.Author.Username}` mentioned you on " +
// $"`{e.Server.Name}` server while you were offline.\n" +
// $"`Message:` {e.Message.Text}").ConfigureAwait(false);
// } // }
// catch { }
// };
// _client.MessageReceived += _client_MessageReceived; // // start the userpresence queue
// _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;
// }
// private Task _client_UserVoiceStateUpdated(IUser iusr, IVoiceState before, IVoiceState after) // NadekoBot.OnReady += () => Task.Run(async () =>
// { // {
// var usr = iusr as IGuildUser; // while (true)
// if (usr == null)
// return Task.CompletedTask;
// var beforeVch = before.VoiceChannel;
// var afterVch = after.VoiceChannel;
// if (beforeVch == afterVch)
// return Task.CompletedTask;
// LogSetting logSetting;
// if (!GuildLogSettings.TryGetValue(usr.Guild.Id, out logSetting)
// || !logSetting.LogVoicePresence
// || !logSetting.IgnoredChannels.Any(ic => ic.ChannelId == after.VoiceChannel.Id))
// return Task.CompletedTask;
// ITextChannel logChannel;
// if ((logChannel = usr.Guild.GetTextChannel(logSetting.ChannelId)) == null)
// return Task.CompletedTask;
// var task = Task.Run(async () =>
// { // {
// if (beforeVch?.Guild == afterVch?.Guild) // var toSend = new Dictionary<Channel, string>();
// //take everything from the queue and merge the messages which are going to the same channel
// KeyValuePair<Channel, string> item;
// while (voicePresenceUpdates.TryTake(out item))
// { // {
// await logChannel.SendMessageAsync($"🎼`{prettyCurrentTime}` {usr.Username} moved from **{beforeVch.Name}** to **{afterVch.Name}** voice channel.").ConfigureAwait(false); // if (toSend.ContainsKey(item.Key))
// }
// else if (beforeVch == null)
// { // {
// await logChannel.SendMessageAsync($"🎼`{prettyCurrentTime}` {usr.Username} has joined **{afterVch.Name}** voice channel.").ConfigureAwait(false); // toSend[item.Key] = toSend[item.Key] + Environment.NewLine + item.Value;
// }
// else if (afterVch == null)
// {
// await logChannel.SendMessageAsync($"🎼`{prettyCurrentTime}` {usr.Username} has left **{beforeVch.Name}** voice channel.").ConfigureAwait(false);
// }
// });
// return Task.CompletedTask;
// }
// private Task _client_UserPresenceUpdated(IGuildUser usr, IPresence before, IPresence after)
// {
// LogSetting logSetting;
// if (!GuildLogSettings.TryGetValue(usr.Guild.Id, out logSetting)
// || !logSetting.LogUserPresence
// || before.Status == after.Status)
// return Task.CompletedTask;
// ITextChannel logChannel;
// if ((logChannel = usr.Guild.GetTextChannel(logSetting.ChannelId)) == null)
// return Task.CompletedTask;
// return Task.CompletedTask;
// }
// private Task _client_UserLeft(IGuildUser usr)
// {
// LogSetting logSetting;
// if (!GuildLogSettings.TryGetValue(usr.Guild.Id, out logSetting)
// || !logSetting.IsLogging
// || !logSetting.UserLeft)
// return Task.CompletedTask;
// var task = Task.Run(async () =>
// {
// });
// return Task.CompletedTask;
// }
// private Task _client_UserJoined(IGuildUser usr)
// {
// LogSetting logSetting;
// if (!GuildLogSettings.TryGetValue(usr.Guild.Id, out logSetting)
// || !logSetting.IsLogging
// || !logSetting.UserJoined)
// return Task.CompletedTask;
// var task = Task.Run(async () =>
// {
// });
// return Task.CompletedTask;
// }
// private Task _client_UserUnbanned(IUser usr, IGuild guild)
// {
// LogSetting logSetting;
// if (!GuildLogSettings.TryGetValue(guild.Id, out logSetting)
// || !logSetting.IsLogging
// || !logSetting.UserUnbanned)
// return Task.CompletedTask;
// var task = Task.Run(async () =>
// {
// });
// return Task.CompletedTask;
// }
// private Task _client_UserBanned(IUser usr, IGuild guild)
// {
// LogSetting logSetting;
// if (!GuildLogSettings.TryGetValue(guild.Id, out logSetting)
// || !logSetting.IsLogging
// || !logSetting.UserBanned)
// return Task.CompletedTask;
// var task = Task.Run(async () =>
// {
// });
// return Task.CompletedTask;
// }
// private Task _client_MessageDeleted(ulong arg1, Optional<IMessage> imsg)
// {
// var msg = (imsg.IsSpecified ? imsg.Value : null) as IUserMessage;
// if (msg == null)
// return Task.CompletedTask;
// var channel = msg.Channel as ITextChannel;
// if (channel == null)
// return Task.CompletedTask;
// LogSetting logSetting;
// if (!GuildLogSettings.TryGetValue(channel.Guild.Id, out logSetting)
// || !logSetting.IsLogging
// || !logSetting.MessageDeleted)
// return Task.CompletedTask;
// var task = Task.Run(async () =>
// {
// });
// return Task.CompletedTask;
// }
// private Task _client_MessageUpdated(Optional<IMessage> optmsg, IMessage imsg2)
// {
// var after = imsg2 as IUserMessage;
// if (after == null)
// return Task.CompletedTask;
// var channel = after.Channel as ITextChannel;
// if (channel == null)
// return Task.CompletedTask;
// LogSetting logSetting;
// if (!GuildLogSettings.TryGetValue(channel.Guild.Id, out logSetting)
// || !logSetting.IsLogging
// || !logSetting.MessageUpdated)
// return Task.CompletedTask;
// var task = Task.Run(async () =>
// {
// });
// return Task.CompletedTask;
// }
// private Task _client_MessageReceived(IMessage imsg)
// {
// var msg = imsg as IUserMessage;
// if (msg == null)
// return Task.CompletedTask;
// var channel = msg.Channel as ITextChannel;
// if (channel == null)
// return Task.CompletedTask;
// LogSetting logSetting;
// if (!GuildLogSettings.TryGetValue(channel.Guild.Id, out logSetting)
// || !logSetting.IsLogging
// || !logSetting.MessageReceived)
// return Task.CompletedTask;
// var task = Task.Run(() =>
// {
// });
// return Task.CompletedTask;
// }
// private enum LogChannelType { Text, Voice, UserPresence };
// private ITextChannel TryGetLogChannel(IGuild guild, LogSetting logSetting, LogChannelType logChannelType = LogChannelType.Text)
// {
// ulong id = 0;
// switch (logChannelType)
// {
// case LogChannelType.Text:
// id = logSetting.ChannelId;
// break;
// case LogChannelType.Voice:
// id = logSetting.VoicePresenceChannelId;
// break;
// case LogChannelType.UserPresence:
// id = logSetting.UserPresenceChannelId;
// break;
// }
// var channel = guild.GetTextChannel(id);
// if (channel == null)
// using (var uow = DbHandler.UnitOfWork())
// {
// switch (logChannelType)
// {
// case LogChannelType.Text:
// logSetting.IsLogging = false;
// break;
// case LogChannelType.Voice:
// logSetting.LogVoicePresence = false;
// break;
// case LogChannelType.UserPresence:
// logSetting.LogUserPresence = false;
// break;
// }
// uow.Complete();
// return null;
// } // }
// else // else
// return channel; // {
// toSend.Add(item.Key, item.Value);
// }
// }
// //send merged messages to each channel
// foreach (var k in toSend)
// {
// try { await k.Key.SendMessageAsync(Environment.NewLine + k.Value).ConfigureAwait(false); } catch { }
// } // }
// [LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] // await Task.Delay(5000);
// [RequireContext(ContextType.Guild)] // }
// public async Task LogServer(IUserMessage msg) // });
// {
// var channel = (ITextChannel)msg.Channel;
// GuildConfig config;
// using (var uow = DbHandler.UnitOfWork())
// {
// config = uow.GuildConfigs.For(channel.Guild.Id);
// LogSetting logSetting = GuildLogSettings.GetOrAdd(channel.Guild.Id, (id) => config.LogSetting);
// logSetting.IsLogging = !logSetting.IsLogging;
// config.LogSetting = logSetting;
// await uow.CompleteAsync();
// } // }
// if (config.LogSetting.IsLogging) // private async void ChannelUpdated(object sender, ChannelUpdatedEventArgs e)
// await channel.SendMessageAsync("`Logging enabled.`").ConfigureAwait(false); // {
// try
// {
// var config = SpecificConfigurations.Default.Of(e.Server.Id);
// var chId = config.LogServerChannel;
// if (chId == null || config.LogserverIgnoreChannels.Contains(e.After.Id))
// return;
// Channel ch;
// if ((ch = e.Server.TextChannels.Where(tc => tc.Id == chId).FirstOrDefault()) == null)
// return;
// if (e.Before.Name != e.After.Name)
// await ch.SendMessageAsync($@"`{prettyCurrentTime}` **Channel Name Changed** `#{e.Before.Name}` (*{e.After.Id}*)
// `New:` {e.After.Name}").ConfigureAwait(false);
// else if (e.Before.Topic != e.After.Topic)
// await ch.SendMessageAsync($@"`{prettyCurrentTime}` **Channel Topic Changed** `#{e.After.Name}` (*{e.After.Id}*)
// `Old:` {e.Before.Topic}
// `New:` {e.After.Topic}").ConfigureAwait(false);
// }
// catch { }
// }
// private async void ChannelDestroyed(object sender, ChannelEventArgs e)
// {
// try
// {
// var config = SpecificConfigurations.Default.Of(e.Server.Id);
// var chId = config.LogServerChannel;
// if (chId == null || config.LogserverIgnoreChannels.Contains(e.Channel.Id))
// return;
// Channel ch;
// if ((ch = e.Server.TextChannels.Where(tc => tc.Id == chId).FirstOrDefault()) == null)
// return;
// await ch.SendMessageAsync($"❗`{prettyCurrentTime}`❗`Channel Deleted:` #{e.Channel.Name} (*{e.Channel.Id}*)").ConfigureAwait(false);
// }
// catch { }
// }
// private async void ChannelCreated(object sender, ChannelEventArgs e)
// {
// try
// {
// var config = SpecificConfigurations.Default.Of(e.Server.Id);
// var chId = config.LogServerChannel;
// if (chId == null || config.LogserverIgnoreChannels.Contains(e.Channel.Id))
// return;
// Channel ch;
// if ((ch = e.Server.TextChannels.Where(tc => tc.Id == chId).FirstOrDefault()) == null)
// return;
// await ch.SendMessageAsync($"`{prettyCurrentTime}`🆕`Channel Created:` #{e.Channel.Mention} (*{e.Channel.Id}*)").ConfigureAwait(false);
// }
// catch { }
// }
// private async void UsrUnbanned(object sender, UserEventArgs e)
// {
// try
// {
// var chId = SpecificConfigurations.Default.Of(e.Server.Id).LogServerChannel;
// if (chId == null)
// return;
// Channel ch;
// if ((ch = e.Server.TextChannels.Where(tc => tc.Id == chId).FirstOrDefault()) == null)
// return;
// await ch.SendMessageAsync($"`{prettyCurrentTime}`♻`User was unbanned:` **{umsg.Author.Username}** ({umsg.Author.Id})").ConfigureAwait(false);
// }
// catch { }
// }
// private async void UsrJoined(object sender, UserEventArgs e)
// {
// try
// {
// var chId = SpecificConfigurations.Default.Of(e.Server.Id).LogServerChannel;
// if (chId == null)
// return;
// Channel ch;
// if ((ch = e.Server.TextChannels.Where(tc => tc.Id == chId).FirstOrDefault()) == null)
// return;
// await ch.SendMessageAsync($"`{prettyCurrentTime}`✅`User joined:` **{umsg.Author.Username}** ({umsg.Author.Id})").ConfigureAwait(false);
// }
// catch { }
// }
// private async void UsrLeft(object sender, UserEventArgs e)
// {
// try
// {
// var chId = SpecificConfigurations.Default.Of(e.Server.Id).LogServerChannel;
// if (chId == null)
// return;
// Channel ch;
// if ((ch = e.Server.TextChannels.Where(tc => tc.Id == chId).FirstOrDefault()) == null)
// return;
// await ch.SendMessageAsync($"`{prettyCurrentTime}`❗`User left:` **{umsg.Author.Username}** ({umsg.Author.Id})").ConfigureAwait(false);
// }
// catch { }
// }
// private async void UsrBanned(object sender, UserEventArgs e)
// {
// try
// {
// var chId = SpecificConfigurations.Default.Of(e.Server.Id).LogServerChannel;
// if (chId == null)
// return;
// Channel ch;
// if ((ch = e.Server.TextChannels.Where(tc => tc.Id == chId).FirstOrDefault()) == null)
// return;
// await ch.SendMessageAsync($"❗`{prettyCurrentTime}`❌`User banned:` **{umsg.Author.Username}** ({umsg.Author.Id})").ConfigureAwait(false);
// }
// catch { }
// }
// private async void MsgRecivd(object sender, MessageEventArgs e)
// {
// try
// {
// if (e.Server == null || e.Channel.IsPrivate || umsg.Author.Id == NadekoBot.Client.CurrentUser.Id)
// return;
// var config = SpecificConfigurations.Default.Of(e.Server.Id);
// var chId = config.LogServerChannel;
// if (chId == null || e.Channel.Id == chId || config.LogserverIgnoreChannels.Contains(e.Channel.Id))
// return;
// Channel ch;
// if ((ch = e.Server.TextChannels.Where(tc => tc.Id == chId).FirstOrDefault()) == null)
// return;
// if (!string.IsNullOrWhiteSpace(e.Message.Text))
// {
// await ch.SendMessageAsync(
// $@"🕔`{prettyCurrentTime}` **New Message** `#{e.Channel.Name}`
//👤`{umsg.Author?.ToString() ?? ("NULL")}` {e.Message.Text.Unmention()}").ConfigureAwait(false);
// }
// else // else
// await channel.SendMessageAsync("`Logging disabled.`").ConfigureAwait(false); // {
// await ch.SendMessageAsync(
// $@"🕔`{prettyCurrentTime}` **File Uploaded** `#{e.Channel.Name}`
//👤`{umsg.Author?.ToString() ?? ("NULL")}` {e.Message.Attachments.FirstOrDefault()?.ProxyUrl}").ConfigureAwait(false);
// } // }
// [LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
// [RequireContext(ContextType.Guild)]
// public async Task LogIgnore(IUserMessage imsg)
// {
// var channel = (ITextChannel)imsg.Channel;
// int removed;
// using (var uow = DbHandler.UnitOfWork())
// {
// var config = uow.GuildConfigs.For(channel.Guild.Id);
// LogSetting logSetting = GuildLogSettings.GetOrAdd(channel.Guild.Id, (id) => config.LogSetting);
// removed = logSetting.IgnoredChannels.RemoveWhere(ilc => ilc.ChannelId == channel.Id);
// if (removed == 0)
// logSetting.IgnoredChannels.Add(new IgnoredLogChannel { ChannelId = channel.Id });
// config.LogSetting = logSetting;
// await uow.CompleteAsync().ConfigureAwait(false);
// } // }
// catch { }
// if (removed == 0) // }
// await channel.SendMessageAsync($"`Logging will now ignore {channel.Name} ({channel.Id}) channel.`").ConfigureAwait(false); // private async void MsgDltd(object sender, MessageEventArgs e)
// {
// try
// {
// if (e.Server == null || e.Channel.IsPrivate || umsg.Author?.Id == NadekoBot.Client.CurrentUser.Id)
// return;
// var config = SpecificConfigurations.Default.Of(e.Server.Id);
// var chId = config.LogServerChannel;
// if (chId == null || e.Channel.Id == chId || config.LogserverIgnoreChannels.Contains(e.Channel.Id))
// return;
// Channel ch;
// if ((ch = e.Server.TextChannels.Where(tc => tc.Id == chId).FirstOrDefault()) == null)
// return;
// if (!string.IsNullOrWhiteSpace(e.Message.Text))
// {
// await ch.SendMessageAsync(
// $@"🕔`{prettyCurrentTime}` **Message** 🚮 `#{e.Channel.Name}`
//👤`{umsg.Author?.ToString() ?? ("NULL")}` {e.Message.Text.Unmention()}").ConfigureAwait(false);
// }
// else // else
// await channel.SendMessageAsync($"`Logging will no longer ignore {channel.Name} ({channel.Id}) channel.`").ConfigureAwait(false); // {
// await ch.SendMessageAsync(
// $@"🕔`{prettyCurrentTime}` **File Deleted** `#{e.Channel.Name}`
//👤`{umsg.Author?.ToString() ?? ("NULL")}` {e.Message.Attachments.FirstOrDefault()?.ProxyUrl}").ConfigureAwait(false);
// } // }
// }
// catch { }
// }
// private async void MsgUpdtd(object sender, MessageUpdatedEventArgs e)
// {
// try
// {
// if (e.Server == null || e.Channel.IsPrivate || umsg.Author?.Id == NadekoBot.Client.CurrentUser.Id)
// return;
// var config = SpecificConfigurations.Default.Of(e.Server.Id);
// var chId = config.LogServerChannel;
// if (chId == null || e.Channel.Id == chId || config.LogserverIgnoreChannels.Contains(e.Channel.Id))
// return;
// Channel ch;
// if ((ch = e.Server.TextChannels.Where(tc => tc.Id == chId).FirstOrDefault()) == null)
// return;
// await ch.SendMessageAsync(
// $@"🕔`{prettyCurrentTime}` **Message** 📝 `#{e.Channel.Name}`
//👤`{umsg.Author?.ToString() ?? ("NULL")}`
// `Old:` {e.Before.Text.Unmention()}
// `New:` {e.After.Text.Unmention()}").ConfigureAwait(false);
// }
// catch { }
// }
// private async void UsrUpdtd(object sender, UserUpdatedEventArgs e)
// {
// var config = SpecificConfigurations.Default.Of(e.Server.Id);
// try
// {
// var chId = config.LogPresenceChannel;
// if (chId != null)
// {
// Channel ch;
// if ((ch = e.Server.TextChannels.Where(tc => tc.Id == chId).FirstOrDefault()) != null)
// {
// if (e.Before.Status != e.After.Status)
// {
// voicePresenceUpdates.Add(new KeyValuePair<Channel, string>(ch, $"`{prettyCurrentTime}`**{e.Before.Name}** is now **{e.After.Status}**."));
// }
// }
// }
// }
// catch { }
// [LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] // try
// [RequireContext(ContextType.Guild)]
// public async Task LogAdd(IUserMessage msg, string eventName)
// { // {
// var channel = (ITextChannel)msg.Channel; // ulong notifyChBeforeId;
// eventName = eventName.ToLowerInvariant(); // ulong notifyChAfterId;
// Channel notifyChBefore = null;
// Channel notifyChAfter = null;
// var beforeVch = e.Before.VoiceChannel;
// var afterVch = e.After.VoiceChannel;
// var notifyLeave = false;
// var notifyJoin = false;
// if ((beforeVch != null || afterVch != null) && (beforeVch != afterVch)) // this means we need to notify for sure.
// {
// if (beforeVch != null && config.VoiceChannelLog.TryGetValue(beforeVch.Id, out notifyChBeforeId) && (notifyChBefore = e.Before.Server.TextChannels.FirstOrDefault(tc => tc.Id == notifyChBeforeId)) != null)
// {
// notifyLeave = true;
// }
// if (afterVch != null && config.VoiceChannelLog.TryGetValue(afterVch.Id, out notifyChAfterId) && (notifyChAfter = e.After.Server.TextChannels.FirstOrDefault(tc => tc.Id == notifyChAfterId)) != null)
// {
// notifyJoin = true;
// }
// if ((notifyLeave && notifyJoin) && (notifyChAfter == notifyChBefore))
// {
// await notifyChAfter.SendMessageAsync($"🎼`{prettyCurrentTime}` {e.Before.Name} moved from **{beforeVch.Mention}** to **{afterVch.Mention}** voice channel.").ConfigureAwait(false);
// }
// else if (notifyJoin)
// {
// await notifyChAfter.SendMessageAsync($"🎼`{prettyCurrentTime}` {e.Before.Name} has joined **{afterVch.Mention}** voice channel.").ConfigureAwait(false);
// }
// else if (notifyLeave)
// {
// await notifyChBefore.SendMessageAsync($"🎼`{prettyCurrentTime}` {e.Before.Name} has left **{beforeVch.Mention}** voice channel.").ConfigureAwait(false);
// }
// }
// }
// catch { }
// switch (eventName) // try
// { // {
// case "messagereceived": // var chId = SpecificConfigurations.Default.Of(e.Server.Id).LogServerChannel;
// case "messageupdated": // if (chId == null)
// case "messagedeleted": // return;
// case "userjoined": // Channel ch;
// case "userleft": // if ((ch = e.Server.TextChannels.Where(tc => tc.Id == chId).FirstOrDefault()) == null)
// case "userbanned": // return;
// case "userunbanned": // string str = $"🕔`{prettyCurrentTime}`";
// case "channelcreated": // if (e.Before.Name != e.After.Name)
// case "channeldestroyed": // str += $"**Name Changed**👤`{e.Before?.ToString()}`\n\t\t`New:`{e.After.ToString()}`";
// case "channelupdated": // else if (e.Before.Nickname != e.After.Nickname)
// using (var uow = DbHandler.UnitOfWork()) // str += $"**Nickname Changed**👤`{e.Before?.ToString()}`\n\t\t`Old:` {e.Before.Nickname}#{e.Before.Discriminator}\n\t\t`New:` {e.After.Nickname}#{e.After.Discriminator}";
// else if (e.Before.AvatarUrl != e.After.AvatarUrl)
// str += $"**Avatar Changed**👤`{e.Before?.ToString()}`\n\t {await e.Before.AvatarUrl.ShortenUrl()} `=>` {await e.After.AvatarUrl.ShortenUrl()}";
// else if (!e.Before.Roles.SequenceEqual(e.After.Roles))
// { // {
// var config = uow.GuildConfigs.For(channel.Guild.Id); // if (e.Before.Roles.Count() < e.After.Roles.Count())
// LogSetting logSetting = GuildLogSettings.GetOrAdd(channel.Guild.Id, (id) => config.LogSetting);
// logSetting.GetType().GetProperty(eventName).SetValue(logSetting, true);
// config.LogSetting = logSetting;
// await uow.CompleteAsync().ConfigureAwait(false);
// }
// await channel.SendMessageAsync($"`Now logging {eventName} event.`").ConfigureAwait(false);
// break;
// default:
// await channel.SendMessageAsync($"`Event \"{eventName}\" not found.`").ConfigureAwait(false);
// break;
// }
// }
// [LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
// [RequireContext(ContextType.Guild)]
// public async Task LogRemove(IUserMessage msg, string eventName)
// { // {
// var channel = (ITextChannel)msg.Channel; // var diffRoles = e.After.Roles.Where(r => !e.Before.Roles.Contains(r)).Select(r => "`" + r.Name + "`");
// eventName = eventName.ToLowerInvariant(); // str += $"**User's Roles changed ⚔➕**👤`{e.Before?.ToString()}`\n\tNow has {string.Join(", ", diffRoles)} role.";
// switch (eventName)
// {
// case "messagereceived":
// case "messageupdated":
// case "messagedeleted":
// case "userjoined":
// case "userleft":
// case "userbanned":
// case "userunbanned":
// case "channelcreated":
// case "channeldestroyed":
// case "channelupdated":
// using (var uow = DbHandler.UnitOfWork())
// {
// var config = uow.GuildConfigs.For(channel.Guild.Id);
// LogSetting logSetting = GuildLogSettings.GetOrAdd(channel.Guild.Id, (id) => config.LogSetting);
// logSetting.GetType().GetProperty(eventName).SetValue(logSetting, false);
// config.LogSetting = logSetting;
// await uow.CompleteAsync().ConfigureAwait(false);
// } // }
// await channel.SendMessageAsync($"`No longer logging {eventName} event.`").ConfigureAwait(false); // else if (e.Before.Roles.Count() > e.After.Roles.Count())
// break;
// default:
// await channel.SendMessageAsync($"`Event \"{eventName}\" not found.`").ConfigureAwait(false);
// break;
// }
// }
// [LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
// [RequireContext(ContextType.Guild)]
// public async Task UserPresence(IUserMessage imsg)
// { // {
// var channel = (ITextChannel)imsg.Channel; // var diffRoles = e.Before.Roles.Where(r => !e.After.Roles.Contains(r)).Select(r => "`" + r.Name + "`");
// bool enabled; // str += $"**User's Roles changed ⚔➖**👤`{e.Before?.ToString()}`\n\tNo longer has {string.Join(", ", diffRoles)} role.";
// using (var uow = DbHandler.UnitOfWork())
// {
// var config = uow.GuildConfigs.For(channel.Guild.Id);
// LogSetting logSetting = GuildLogSettings.GetOrAdd(channel.Guild.Id, (id) => config.LogSetting);
// enabled = logSetting.LogUserPresence = !config.LogSetting.LogUserPresence;
// config.LogSetting = logSetting;
// await uow.CompleteAsync().ConfigureAwait(false);
// } // }
// if (enabled)
// await channel.SendMessageAsync($"`Logging user presence updates in {channel.Name} ({channel.Id}) channel.`").ConfigureAwait(false);
// else // else
// await channel.SendMessageAsync($"`Stopped logging user presence updates.`").ConfigureAwait(false); // {
// Console.WriteLine("SEQUENCE NOT EQUAL BUT NO DIFF ROLES - REPORT TO KWOTH on #NADEKOLOG server");
// return;
// } // }
// [LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
// [RequireContext(ContextType.Guild)]
// public async Task VoicePresence(IUserMessage imsg)
// {
// var channel = (ITextChannel)imsg.Channel;
// bool enabled;
// using (var uow = DbHandler.UnitOfWork())
// {
// var config = uow.GuildConfigs.For(channel.Guild.Id);
// LogSetting logSetting = GuildLogSettings.GetOrAdd(channel.Guild.Id, (id) => config.LogSetting);
// enabled = config.LogSetting.LogVoicePresence = !config.LogSetting.LogVoicePresence;
// config.LogSetting = logSetting;
// await uow.CompleteAsync().ConfigureAwait(false);
// } // }
// if (enabled)
// await channel.SendMessageAsync($"`Logging voice presence updates in {channel.Name} ({channel.Id}) channel.`").ConfigureAwait(false);
// else // else
// await channel.SendMessageAsync($"`Stopped logging voice presence updates.`").ConfigureAwait(false); // return;
// await ch.SendMessageAsync(str).ConfigureAwait(false);
// }
// catch { }
// } // }
// [LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] // public override void Init(CommandGroupBuilder cgb)
// [RequireContext(ContextType.Guild)]
// public async Task VoiPresIgnore(IUserMessage imsg, IVoiceChannel voiceChannel)
// { // {
// var channel = (ITextChannel)imsg.Channel;
// int removed;
// using (var uow = DbHandler.UnitOfWork())
// {
// var config = uow.GuildConfigs.For(channel.Guild.Id);
// LogSetting logSetting = GuildLogSettings.GetOrAdd(channel.Guild.Id, (id) => config.LogSetting);
// removed = logSetting.IgnoredVoicePresenceChannelIds.RemoveWhere(ivpc => ivpc.ChannelId == voiceChannel.Id);
// if (removed == 0)
// logSetting.IgnoredVoicePresenceChannelIds.Add(new IgnoredVoicePresenceChannel { ChannelId = voiceChannel.Id });
// config.LogSetting = logSetting;
// await uow.CompleteAsync().ConfigureAwait(false);
// }
// if (removed == 0) // cgb.CreateCommand(Module.Prefix + "spmom")
// await channel.SendMessageAsync($"`Enabled logging voice presence updates for {voiceChannel.Name} ({voiceChannel.Id}) channel.`").ConfigureAwait(false); // .Description($"Toggles whether mentions of other offline users on your server will send a pm to them. **Needs Manage Server Permissions.**| `{Prefix}spmom`")
// .AddCheck(SimpleCheckers.ManageServer())
// .Do(async e =>
// {
// var specificConfig = SpecificConfigurations.Default.Of(e.Server.Id);
// specificConfig.SendPrivateMessageOnMention =
// !specificConfig.SendPrivateMessageOnMention;
// if (specificConfig.SendPrivateMessageOnMention)
// await channel.SendMessageAsync(":ok: I will send private messages " +
// "to mentioned offline users.").ConfigureAwait(false);
// else // else
// await channel.SendMessageAsync($"`Disabled logging voice presence updates for {voiceChannel.Name} ({voiceChannel.Id}) channel.`").ConfigureAwait(false); // await channel.SendMessageAsync(":ok: I won't send private messages " +
// } // "to mentioned offline users anymore.").ConfigureAwait(false);
// });
// cgb.CreateCommand(Module.Prefix + "logserver")
// .Description($"Toggles logging in this channel. Logs every message sent/deleted/edited on the server. **Bot Owner Only!** | `{Prefix}logserver`")
// .AddCheck(SimpleCheckers.OwnerOnly())
// .AddCheck(SimpleCheckers.ManageServer())
// .Do(async e =>
// {
// var chId = SpecificConfigurations.Default.Of(e.Server.Id).LogServerChannel;
// if (chId == null)
// {
// SpecificConfigurations.Default.Of(e.Server.Id).LogServerChannel = e.Channel.Id;
// await channel.SendMessageAsync($"❗**I WILL BEGIN LOGGING SERVER ACTIVITY IN THIS CHANNEL**❗").ConfigureAwait(false);
// return;
// }
// Channel ch;
// if ((ch = e.Server.TextChannels.Where(tc => tc.Id == chId).FirstOrDefault()) == null)
// return;
// SpecificConfigurations.Default.Of(e.Server.Id).LogServerChannel = null;
// await channel.SendMessageAsync($"❗**NO LONGER LOGGING IN {ch.Mention} CHANNEL**❗").ConfigureAwait(false);
// });
// cgb.CreateCommand(Prefix + "logignore")
// .Description($"Toggles whether the {Prefix}logserver command ignores this channel. Useful if you have hidden admin channel and public log channel. **Bot Owner Only!**| `{Prefix}logignore`")
// .AddCheck(SimpleCheckers.OwnerOnly())
// .AddCheck(SimpleCheckers.ManageServer())
// .Do(async e =>
// {
// var config = SpecificConfigurations.Default.Of(e.Server.Id);
// if (config.LogserverIgnoreChannels.Remove(e.Channel.Id))
// {
// await channel.SendMessageAsync($"`{Prefix}logserver will stop ignoring this channel.`");
// }
// else
// {
// config.LogserverIgnoreChannels.Add(e.Channel.Id);
// await channel.SendMessageAsync($"`{Prefix}logserver will ignore this channel.`");
// }
// });
// cgb.CreateCommand(Module.Prefix + "userpresence")
// .Description($"Starts logging to this channel when someone from the server goes online/offline/idle. **Needs Manage Server Permissions.**| `{Prefix}userpresence`")
// .AddCheck(SimpleCheckers.ManageServer())
// .Do(async e =>
// {
// var chId = SpecificConfigurations.Default.Of(e.Server.Id).LogPresenceChannel;
// if (chId == null)
// {
// SpecificConfigurations.Default.Of(e.Server.Id).LogPresenceChannel = e.Channel.Id;
// await channel.SendMessageAsync($"**User presence notifications enabled.**").ConfigureAwait(false);
// return;
// }
// SpecificConfigurations.Default.Of(e.Server.Id).LogPresenceChannel = null;
// await channel.SendMessageAsync($"**User presence notifications disabled.**").ConfigureAwait(false);
// });
// cgb.CreateCommand(Module.Prefix + "voicepresence")
// .Description($"Toggles logging to this channel whenever someone joins or leaves a voice channel you are in right now. **Needs Manage Server Permissions.**| `{Prefix}voicerpresence`")
// .Parameter("all", ParameterType.Optional)
// .AddCheck(SimpleCheckers.ManageServer())
// .Do(async e =>
// {
// var config = SpecificConfigurations.Default.Of(e.Server.Id);
// if (all?.ToLower() == "all")
// {
// foreach (var voiceChannel in e.Server.VoiceChannels)
// {
// config.VoiceChannelLog.TryAdd(voiceChannel.Id, e.Channel.Id);
// }
// await channel.SendMessageAsync("Started logging user presence for **ALL** voice channels!").ConfigureAwait(false);
// return;
// }
// if (umsg.Author.VoiceChannel == null)
// {
// await channel.SendMessageAsync("💢 You are not in a voice channel right now. If you are, please rejoin it.").ConfigureAwait(false);
// return;
// }
// ulong throwaway;
// if (!config.VoiceChannelLog.TryRemove(umsg.Author.VoiceChannel.Id, out throwaway))
// {
// config.VoiceChannelLog.TryAdd(umsg.Author.VoiceChannel.Id, e.Channel.Id);
// await channel.SendMessageAsync($"`Logging user updates for` {umsg.Author.VoiceChannel.Mention} `voice channel.`").ConfigureAwait(false);
// }
// else
// await channel.SendMessageAsync($"`Stopped logging user updates for` {umsg.Author.VoiceChannel.Mention} `voice channel.`").ConfigureAwait(false);
// });
// } // }
// } // }
//} //}

View File

@ -12,6 +12,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace NadekoBot.Modules.Administration namespace NadekoBot.Modules.Administration
@ -28,6 +29,9 @@ namespace NadekoBot.Modules.Administration
public ConcurrentDictionary<ulong, LogSetting> GuildLogSettings { get; } public ConcurrentDictionary<ulong, LogSetting> GuildLogSettings { get; }
private ConcurrentDictionary<ITextChannel, List<string>> UserPresenceUpdates { get; }
private Timer t;
public LogCommands(DiscordSocketClient client) public LogCommands(DiscordSocketClient client)
{ {
_client = client; _client = client;
@ -40,6 +44,21 @@ namespace NadekoBot.Modules.Administration
.ToDictionary(g => g.GuildId, g => g.LogSetting)); .ToDictionary(g => g.GuildId, g => g.LogSetting));
} }
t = new Timer(async (state) =>
{
var keys = UserPresenceUpdates.Keys.ToList();
foreach (var key in keys)
{
List<string> messages;
UserPresenceUpdates.TryRemove(key, out messages);
foreach (var msg in messages)
{
await key.SendMessageAsync(msg);
}
}
}, null, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10));
_client.MessageReceived += _client_MessageReceived; _client.MessageReceived += _client_MessageReceived;
_client.MessageUpdated += _client_MessageUpdated; _client.MessageUpdated += _client_MessageUpdated;
_client.MessageDeleted += _client_MessageDeleted; _client.MessageDeleted += _client_MessageDeleted;
@ -49,6 +68,90 @@ namespace NadekoBot.Modules.Administration
_client.UserLeft += _client_UserLeft; _client.UserLeft += _client_UserLeft;
_client.UserPresenceUpdated += _client_UserPresenceUpdated; _client.UserPresenceUpdated += _client_UserPresenceUpdated;
_client.UserVoiceStateUpdated += _client_UserVoiceStateUpdated; _client.UserVoiceStateUpdated += _client_UserVoiceStateUpdated;
_client.ChannelCreated += _client_ChannelCreated;
_client.ChannelDestroyed += _client_ChannelDestroyed;
_client.ChannelUpdated += _client_ChannelUpdated;
}
private Task _client_ChannelUpdated(IChannel cbefore, IChannel cafter)
{
var before = cbefore as ITextChannel;
if (before == null)
return Task.CompletedTask;
var after = (ITextChannel)cafter;
LogSetting logSetting;
if (!GuildLogSettings.TryGetValue(before.Guild.Id, out logSetting)
|| !logSetting.IsLogging
|| !logSetting.ChannelUpdated)
return Task.CompletedTask;
ITextChannel logChannel;
if ((logChannel = before.Guild.GetTextChannel(logSetting.ChannelId)) == null)
return Task.CompletedTask;
var task = Task.Run(async () =>
{
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.Topic != after.Topic)
await logChannel.SendMessageAsync($@"`{prettyCurrentTime}` **Channel Topic Changed** `#{after.Name}` ({after.Id})
`Old:` {before.Topic}
`New:` {after.Topic}").ConfigureAwait(false);
});
return Task.CompletedTask;
}
private Task _client_ChannelDestroyed(IChannel ich)
{
var ch = ich as IGuildChannel;
if (ch == null)
return Task.CompletedTask;
LogSetting logSetting;
if (!GuildLogSettings.TryGetValue(ch.Guild.Id, out logSetting)
|| !logSetting.IsLogging
|| !logSetting.ChannelDestroyed)
return Task.CompletedTask;
ITextChannel logChannel;
if ((logChannel = ch.Guild.GetTextChannel(logSetting.ChannelId)) == null)
return Task.CompletedTask;
var task = Task.Run(async () =>
{
await logChannel.SendMessageAsync($"❗`{prettyCurrentTime}` `{(ch is IVoiceChannel ? "Voice" : "Text")} Channel Deleted:` **#{ch.Name}** ({ch.Id})").ConfigureAwait(false);
});
return Task.CompletedTask;
}
private Task _client_ChannelCreated(IChannel ich)
{
var ch = ich as IGuildChannel;
if (ch == null)
return Task.CompletedTask;
LogSetting logSetting;
if (!GuildLogSettings.TryGetValue(ch.Guild.Id, out logSetting)
|| !logSetting.IsLogging
|| !logSetting.ChannelCreated)
return Task.CompletedTask;
ITextChannel logChannel;
if ((logChannel = ch.Guild.GetTextChannel(logSetting.ChannelId)) == null)
return Task.CompletedTask;
var task = Task.Run(async () =>
{
await logChannel.SendMessageAsync($"`{prettyCurrentTime}`🆕`{(ch is IVoiceChannel ? "Voice" : "Text")} Channel Created:` **#{ch.Name}** ({ch.Id})").ConfigureAwait(false);
});
return Task.CompletedTask;
} }
private Task _client_UserVoiceStateUpdated(IUser iusr, IVoiceState before, IVoiceState after) private Task _client_UserVoiceStateUpdated(IUser iusr, IVoiceState before, IVoiceState after)
@ -75,6 +178,7 @@ namespace NadekoBot.Modules.Administration
var task = Task.Run(async () => var task = Task.Run(async () =>
{ {
_log.Info("Voice state");
if (beforeVch?.Guild == afterVch?.Guild) if (beforeVch?.Guild == afterVch?.Guild)
{ {
await logChannel.SendMessageAsync($"🎼`{prettyCurrentTime}` {usr.Username} moved from **{beforeVch.Name}** to **{afterVch.Name}** voice channel.").ConfigureAwait(false); await logChannel.SendMessageAsync($"🎼`{prettyCurrentTime}` {usr.Username} moved from **{beforeVch.Name}** to **{afterVch.Name}** voice channel.").ConfigureAwait(false);
@ -101,9 +205,17 @@ namespace NadekoBot.Modules.Administration
return Task.CompletedTask; return Task.CompletedTask;
ITextChannel logChannel; ITextChannel logChannel;
if ((logChannel = usr.Guild.GetTextChannel(logSetting.ChannelId)) == null) if ((logChannel = usr.Guild.GetTextChannel(logSetting.UserPresenceChannelId)) == null)
return Task.CompletedTask; return Task.CompletedTask;
var task = Task.Run(async () =>
{
if(before.Status != after.Status)
await logChannel.SendMessageAsync($"`{prettyCurrentTime}`**{usr.Username}** is now **{after.Status}**.");
else
await logChannel.SendMessageAsync($"`{prettyCurrentTime}`**{usr.Username}** is now playing **{after.Status}**.");
});
return Task.CompletedTask; return Task.CompletedTask;
} }
@ -115,9 +227,13 @@ namespace NadekoBot.Modules.Administration
|| !logSetting.UserLeft) || !logSetting.UserLeft)
return Task.CompletedTask; return Task.CompletedTask;
ITextChannel logChannel;
if ((logChannel = usr.Guild.GetTextChannel(logSetting.ChannelId)) == null)
return Task.CompletedTask;
var task = Task.Run(async () => var task = Task.Run(async () =>
{ {
await logChannel.SendMessageAsync($"`{prettyCurrentTime}`❗`User left:` **{usr.Username}** ({usr.Id})").ConfigureAwait(false);
}); });
return Task.CompletedTask; return Task.CompletedTask;
@ -131,9 +247,13 @@ namespace NadekoBot.Modules.Administration
|| !logSetting.UserJoined) || !logSetting.UserJoined)
return Task.CompletedTask; return Task.CompletedTask;
ITextChannel logChannel;
if ((logChannel = usr.Guild.GetTextChannel(logSetting.ChannelId)) == null)
return Task.CompletedTask;
var task = Task.Run(async () => var task = Task.Run(async () =>
{ {
await logChannel.SendMessageAsync($"`{prettyCurrentTime}`❗`User joined:` **{usr.Username}** ({usr.Id})").ConfigureAwait(false);
}); });
return Task.CompletedTask; return Task.CompletedTask;
@ -147,9 +267,13 @@ namespace NadekoBot.Modules.Administration
|| !logSetting.UserUnbanned) || !logSetting.UserUnbanned)
return Task.CompletedTask; return Task.CompletedTask;
ITextChannel logChannel;
if ((logChannel = guild.GetTextChannel(logSetting.ChannelId)) == null)
return Task.CompletedTask;
var task = Task.Run(async () => var task = Task.Run(async () =>
{ {
await logChannel.SendMessageAsync($"`{prettyCurrentTime}`♻`User unbanned:` **{usr.Username}** ({usr.Id})").ConfigureAwait(false);
}); });
return Task.CompletedTask; return Task.CompletedTask;
@ -163,9 +287,13 @@ namespace NadekoBot.Modules.Administration
|| !logSetting.UserBanned) || !logSetting.UserBanned)
return Task.CompletedTask; return Task.CompletedTask;
ITextChannel logChannel;
if ((logChannel = guild.GetTextChannel(logSetting.ChannelId)) == null)
return Task.CompletedTask;
var task = Task.Run(async () => var task = Task.Run(async () =>
{ {
await logChannel.SendMessageAsync($"❗`{prettyCurrentTime}`❌`User banned:` **{usr.Username}** ({usr.Id})").ConfigureAwait(false);
}); });
return Task.CompletedTask; return Task.CompletedTask;
@ -177,6 +305,8 @@ namespace NadekoBot.Modules.Administration
if (msg == null) if (msg == null)
return Task.CompletedTask; return Task.CompletedTask;
var ch = msg.Channel as ITextChannel;
var channel = msg.Channel as ITextChannel; var channel = msg.Channel as ITextChannel;
if (channel == null) if (channel == null)
return Task.CompletedTask; return Task.CompletedTask;
@ -187,9 +317,15 @@ namespace NadekoBot.Modules.Administration
|| !logSetting.MessageDeleted) || !logSetting.MessageDeleted)
return Task.CompletedTask; return Task.CompletedTask;
ITextChannel logChannel;
if ((logChannel = channel.Guild.GetTextChannel(logSetting.ChannelId)) == null)
return Task.CompletedTask;
var task = Task.Run(async () => var task = Task.Run(async () =>
{ {
await logChannel.SendMessageAsync($@"🕔`{prettyCurrentTime}` **Message** 🚮 `#{ch.Name}`
👤`{msg.Author.Username}`: {msg.Content}
`Attachements`: {string.Join(", ", msg.Attachments.Select(a=>a.ProxyUrl))}").ConfigureAwait(false);
}); });
return Task.CompletedTask; return Task.CompletedTask;
@ -201,6 +337,10 @@ namespace NadekoBot.Modules.Administration
if (after == null) if (after == null)
return Task.CompletedTask; return Task.CompletedTask;
var before = (optmsg.IsSpecified ? optmsg.Value : null) as IUserMessage;
if (before == null)
return Task.CompletedTask;
var channel = after.Channel as ITextChannel; var channel = after.Channel as ITextChannel;
if (channel == null) if (channel == null)
return Task.CompletedTask; return Task.CompletedTask;
@ -211,9 +351,16 @@ namespace NadekoBot.Modules.Administration
|| !logSetting.MessageUpdated) || !logSetting.MessageUpdated)
return Task.CompletedTask; return Task.CompletedTask;
ITextChannel logChannel;
if ((logChannel = channel.Guild.GetTextChannel(logSetting.ChannelId)) == null)
return Task.CompletedTask;
var task = Task.Run(async () => var task = Task.Run(async () =>
{ {
await logChannel.SendMessageAsync($@"🕔`{prettyCurrentTime}` **Message** 📝 `#{channel.Name}`
👤`{before.Author.Username}`
`Old:` {before.Content}
`New:` {after.Content}").ConfigureAwait(false);
}); });
return Task.CompletedTask; return Task.CompletedTask;
@ -237,7 +384,7 @@ namespace NadekoBot.Modules.Administration
var task = Task.Run(() => var task = Task.Run(() =>
{ {
_log.Info("Message received");
}); });
return Task.CompletedTask; return Task.CompletedTask;
@ -264,6 +411,7 @@ namespace NadekoBot.Modules.Administration
if (channel == null) if (channel == null)
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
var newLogSetting = uow.GuildConfigs.For(guild.Id).LogSetting;
switch (logChannelType) switch (logChannelType)
{ {
case LogChannelType.Text: case LogChannelType.Text:
@ -276,6 +424,7 @@ namespace NadekoBot.Modules.Administration
logSetting.LogUserPresence = false; logSetting.LogUserPresence = false;
break; break;
} }
GuildLogSettings.AddOrUpdate(guild.Id, newLogSetting, (gid, old) => newLogSetting);
uow.Complete(); uow.Complete();
return null; return null;
} }
@ -288,17 +437,18 @@ namespace NadekoBot.Modules.Administration
public async Task LogServer(IUserMessage msg) public async Task LogServer(IUserMessage msg)
{ {
var channel = (ITextChannel)msg.Channel; var channel = (ITextChannel)msg.Channel;
GuildConfig config; LogSetting logSetting;
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
config = uow.GuildConfigs.For(channel.Guild.Id); logSetting = uow.GuildConfigs.For(channel.Guild.Id).LogSetting;
LogSetting logSetting = GuildLogSettings.GetOrAdd(channel.Guild.Id, (id) => config.LogSetting); GuildLogSettings.AddOrUpdate(channel.Guild.Id, (id) => logSetting, (id, old) => logSetting);
logSetting.IsLogging = !logSetting.IsLogging; logSetting.IsLogging = !logSetting.IsLogging;
config.LogSetting = logSetting; if (logSetting.IsLogging)
logSetting.ChannelId = channel.Id;
await uow.CompleteAsync(); await uow.CompleteAsync();
} }
if (config.LogSetting.IsLogging) if (logSetting.IsLogging)
await channel.SendMessageAsync("`Logging enabled.`").ConfigureAwait(false); await channel.SendMessageAsync("`Logging enabled.`").ConfigureAwait(false);
else else
await channel.SendMessageAsync("`Logging disabled.`").ConfigureAwait(false); await channel.SendMessageAsync("`Logging disabled.`").ConfigureAwait(false);
@ -327,40 +477,40 @@ namespace NadekoBot.Modules.Administration
await channel.SendMessageAsync($"`Logging will no longer ignore {channel.Name} ({channel.Id}) channel.`").ConfigureAwait(false); await channel.SendMessageAsync($"`Logging will no longer ignore {channel.Name} ({channel.Id}) channel.`").ConfigureAwait(false);
} }
//[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] [LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
//[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
//public async Task LogAdd(IUserMessage msg, string eventName) public async Task LogAdd(IUserMessage msg, [Remainder] string eventName)
//{ {
// var channel = (ITextChannel)msg.Channel; var channel = (ITextChannel)msg.Channel;
// eventName = eventName.ToLowerInvariant(); //eventName = eventName?.Replace(" ","").ToLowerInvariant();
// switch (eventName) switch (eventName.ToLowerInvariant())
// { {
// case "messagereceived": case "messagereceived":
// case "messageupdated": case "messageupdated":
// case "messagedeleted": case "messagedeleted":
// case "userjoined": case "userjoined":
// case "userleft": case "userleft":
// case "userbanned": case "userbanned":
// case "userunbanned": case "userunbanned":
// case "channelcreated": case "channelcreated":
// case "channeldestroyed": case "channeldestroyed":
// case "channelupdated": case "channelupdated":
// using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
// { {
// var config = uow.GuildConfigs.For(channel.Guild.Id); var logSetting = uow.GuildConfigs.For(channel.Guild.Id).LogSetting;
// LogSetting logSetting = GuildLogSettings.GetOrAdd(channel.Guild.Id, (id) => config.LogSetting); GuildLogSettings.AddOrUpdate(channel.Guild.Id, (id) => logSetting, (id, old) => logSetting);
// logSetting.GetType().GetProperty(eventName).SetValue(logSetting, true); var prop = logSetting.GetType().GetProperty(eventName);
// config.LogSetting = logSetting; prop.SetValue(logSetting, true);
// await uow.CompleteAsync().ConfigureAwait(false); await uow.CompleteAsync().ConfigureAwait(false);
// } }
// await channel.SendMessageAsync($"`Now logging {eventName} event.`").ConfigureAwait(false); await channel.SendMessageAsync($"`Now logging {eventName} event.`").ConfigureAwait(false);
// break; break;
// default: default:
// await channel.SendMessageAsync($"`Event \"{eventName}\" not found.`").ConfigureAwait(false); await channel.SendMessageAsync($"`Event \"{eventName}\" not found.`").ConfigureAwait(false);
// break; break;
// } }
//} }
//[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] //[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
//[RequireContext(ContextType.Guild)] //[RequireContext(ContextType.Guild)]
@ -397,26 +547,27 @@ namespace NadekoBot.Modules.Administration
// } // }
//} //}
//[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] [LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
//[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
//public async Task UserPresence(IUserMessage imsg) public async Task UserPresence(IUserMessage imsg)
//{ {
// var channel = (ITextChannel)imsg.Channel; var channel = (ITextChannel)imsg.Channel;
// bool enabled; bool enabled;
// using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
// { {
// var config = uow.GuildConfigs.For(channel.Guild.Id); var logSetting = uow.GuildConfigs.For(channel.Guild.Id).LogSetting;
// LogSetting logSetting = GuildLogSettings.GetOrAdd(channel.Guild.Id, (id) => config.LogSetting); GuildLogSettings.AddOrUpdate(channel.Guild.Id, (id) => logSetting, (id, old) => logSetting);
// enabled = logSetting.LogUserPresence = !config.LogSetting.LogUserPresence; enabled = logSetting.LogUserPresence = !logSetting.LogUserPresence;
// config.LogSetting = logSetting; if(enabled)
// await uow.CompleteAsync().ConfigureAwait(false); logSetting.UserPresenceChannelId = channel.Id;
// } await uow.CompleteAsync().ConfigureAwait(false);
}
// if (enabled) if (enabled)
// await channel.SendMessageAsync($"`Logging user presence updates in {channel.Name} ({channel.Id}) channel.`").ConfigureAwait(false); await channel.SendMessageAsync($"`Logging user presence updates in {channel.Name} ({channel.Id}) channel.`").ConfigureAwait(false);
// else else
// await channel.SendMessageAsync($"`Stopped logging user presence updates.`").ConfigureAwait(false); await channel.SendMessageAsync($"`Stopped logging user presence updates.`").ConfigureAwait(false);
//} }
//[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] //[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
//[RequireContext(ContextType.Guild)] //[RequireContext(ContextType.Guild)]

View File

@ -56,15 +56,16 @@ namespace NadekoBot
depMap.Add<IGoogleApiService>(Google); depMap.Add<IGoogleApiService>(Google);
//connect //connect
await Client.LoginAsync(TokenType.Bot, Credentials.Token); await Client.LoginAsync(TokenType.Bot, Credentials.Token).ConfigureAwait(false);
await Client.ConnectAsync(); await Client.ConnectAsync().ConfigureAwait(false);
await Client.DownloadAllUsersAsync().ConfigureAwait(false);
_log.Info("Connected"); _log.Info("Connected");
//load commands //load commands
await Commands.LoadAssembly(Assembly.GetEntryAssembly(), depMap); await Commands.LoadAssembly(Assembly.GetEntryAssembly(), depMap).ConfigureAwait(false);
Console.WriteLine(await Stats.Print()); Console.WriteLine(await Stats.Print().ConfigureAwait(false));
await Task.Delay(-1); await Task.Delay(-1);
} }

View File

@ -12,9 +12,9 @@ namespace NadekoBot.Services.Database.Models
public ulong ChannelId { get; set; } public ulong ChannelId { get; set; }
public HashSet<IgnoredLogChannel> IgnoredChannels { get; set; } public HashSet<IgnoredLogChannel> IgnoredChannels { get; set; }
public bool MessageReceived { get; set; } public bool MessageReceived { get; set; } = true;
public bool MessageUpdated { get; set; } public bool MessageUpdated { get; set; } = true;
public bool MessageDeleted { get; set; } public bool MessageDeleted { get; set; } = true;
public bool UserJoined { get; set; } = true; public bool UserJoined { get; set; } = true;
public bool UserLeft { get; set; } = true; public bool UserLeft { get; set; } = true;

View File

@ -13,6 +13,14 @@ namespace NadekoBot.Services.Database.Repositories.Impl
public GuildConfigRepository(DbContext context) : base(context) public GuildConfigRepository(DbContext context) : base(context)
{ {
} }
public new IEnumerable<GuildConfig> GetAll() =>
_set.Include(gc => gc.LogSetting)
.ThenInclude(ls => ls.IgnoredChannels)
.Include(gc => gc.LogSetting)
.ThenInclude(ls => ls.IgnoredVoicePresenceChannelIds)
.ToList();
/// <summary> /// <summary>
/// Gets and creates if it doesn't exist a config for a guild. /// Gets and creates if it doesn't exist a config for a guild.
/// </summary> /// </summary>