NadekoBot/NadekoBot.Core/Modules/Administration/LogCommands.cs

183 lines
9.0 KiB
C#
Raw Normal View History

using Discord;
using Discord.Commands;
using NadekoBot.Extensions;
using NadekoBot.Core.Services;
using NadekoBot.Core.Services.Database.Models;
using System;
using System.Linq;
using System.Threading.Tasks;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common;
using NadekoBot.Common.Attributes;
using NadekoBot.Common.TypeReaders.Models;
using NadekoBot.Modules.Administration.Services;
using static NadekoBot.Modules.Administration.Services.LogCommandService;
namespace NadekoBot.Modules.Administration
{
public partial class Administration
{
[Group]
[NoPublicBot]
2017-07-15 16:34:34 +00:00
public class LogCommands : NadekoSubmodule<LogCommandService>
{
private readonly DbService _db;
2017-07-15 16:34:34 +00:00
public LogCommands(DbService db)
{
_db = db;
2016-12-12 23:44:52 +00:00
}
public enum EnableDisable
{
2016-12-12 23:44:52 +00:00
Enable,
Disable
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.Administrator)]
[OwnerOnly]
2017-01-01 11:54:02 +00:00
public async Task LogServer(PermissionAction action)
{
2017-01-01 11:54:44 +00:00
var channel = (ITextChannel)Context.Channel;
2016-09-06 01:59:00 +00:00
LogSetting logSetting;
using (var uow = _db.UnitOfWork)
{
logSetting = uow.GuildConfigs.LogSettingsFor(channel.Guild.Id).LogSetting;
2017-07-15 16:34:34 +00:00
_service.GuildLogSettings.AddOrUpdate(channel.Guild.Id, (id) => logSetting, (id, old) => logSetting);
2016-12-12 23:44:52 +00:00
logSetting.LogOtherId =
logSetting.MessageUpdatedId =
logSetting.MessageDeletedId =
logSetting.UserJoinedId =
logSetting.UserLeftId =
logSetting.UserBannedId =
logSetting.UserUnbannedId =
logSetting.UserUpdatedId =
logSetting.ChannelCreatedId =
logSetting.ChannelDestroyedId =
logSetting.ChannelUpdatedId =
logSetting.LogUserPresenceId =
logSetting.LogVoicePresenceId =
2017-01-08 23:33:42 +00:00
logSetting.UserMutedId =
2016-12-12 23:44:52 +00:00
logSetting.LogVoicePresenceTTSId = (action.Value ? channel.Id : (ulong?)null);
await uow.CompleteAsync().ConfigureAwait(false);
}
2016-12-12 23:44:52 +00:00
if (action.Value)
2017-02-14 18:06:02 +00:00
await ReplyConfirmLocalized("log_all").ConfigureAwait(false);
else
2017-02-14 18:06:02 +00:00
await ReplyConfirmLocalized("log_disabled").ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.Administrator)]
[OwnerOnly]
2017-01-01 11:54:02 +00:00
public async Task LogIgnore()
{
2017-01-01 11:54:44 +00:00
var channel = (ITextChannel)Context.Channel;
int removed;
using (var uow = _db.UnitOfWork)
{
var config = uow.GuildConfigs.LogSettingsFor(channel.Guild.Id);
2017-07-15 16:34:34 +00:00
LogSetting logSetting = _service.GuildLogSettings.GetOrAdd(channel.Guild.Id, (id) => config.LogSetting);
removed = logSetting.IgnoredChannels.RemoveWhere(ilc => ilc.ChannelId == channel.Id);
2016-10-18 01:26:41 +00:00
config.LogSetting.IgnoredChannels.RemoveWhere(ilc => ilc.ChannelId == channel.Id);
if (removed == 0)
2016-10-18 01:26:41 +00:00
{
var toAdd = new IgnoredLogChannel { ChannelId = channel.Id };
logSetting.IgnoredChannels.Add(toAdd);
config.LogSetting.IgnoredChannels.Add(toAdd);
}
await uow.CompleteAsync().ConfigureAwait(false);
}
if (removed == 0)
2017-02-14 18:06:02 +00:00
await ReplyConfirmLocalized("log_ignore", Format.Bold(channel.Mention + "(" + channel.Id + ")")).ConfigureAwait(false);
else
2017-02-14 18:06:02 +00:00
await ReplyConfirmLocalized("log_not_ignore", Format.Bold(channel.Mention + "(" + channel.Id + ")")).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
2016-09-06 01:59:00 +00:00
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.Administrator)]
2016-12-24 13:13:48 +00:00
[OwnerOnly]
2017-01-01 11:54:02 +00:00
public async Task LogEvents()
2016-09-06 01:59:00 +00:00
{
await Context.Channel.SendConfirmAsync(Format.Bold(GetText("log_events")) + "\n" +
$"```fix\n{string.Join(", ", Enum.GetNames(typeof(LogType)).Cast<string>())}```")
2017-02-17 22:57:28 +00:00
.ConfigureAwait(false);
2016-09-06 01:59:00 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-09-06 21:34:00 +00:00
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.Administrator)]
2016-12-24 13:13:48 +00:00
[OwnerOnly]
2017-01-01 11:54:02 +00:00
public async Task Log(LogType type)
2016-09-06 21:34:00 +00:00
{
2017-01-01 11:54:44 +00:00
var channel = (ITextChannel)Context.Channel;
2016-12-12 23:44:52 +00:00
ulong? channelId = null;
using (var uow = _db.UnitOfWork)
2016-09-06 21:34:00 +00:00
{
var logSetting = uow.GuildConfigs.LogSettingsFor(channel.Guild.Id).LogSetting;
2017-07-15 16:34:34 +00:00
_service.GuildLogSettings.AddOrUpdate(channel.Guild.Id, (id) => logSetting, (id, old) => logSetting);
2016-12-12 23:44:52 +00:00
switch (type)
{
case LogType.Other:
2017-10-17 05:03:56 +00:00
channelId = logSetting.LogOtherId = (logSetting.LogOtherId == null ? (ulong?)channel.Id : default);
2016-12-12 23:44:52 +00:00
break;
case LogType.MessageUpdated:
2017-10-17 05:03:56 +00:00
channelId = logSetting.MessageUpdatedId = (logSetting.MessageUpdatedId == null ? (ulong?)channel.Id : default);
2016-12-12 23:44:52 +00:00
break;
case LogType.MessageDeleted:
2017-10-17 05:03:56 +00:00
channelId = logSetting.MessageDeletedId = (logSetting.MessageDeletedId == null ? (ulong?)channel.Id : default);
2016-12-12 23:44:52 +00:00
break;
case LogType.UserJoined:
2017-10-17 05:03:56 +00:00
channelId = logSetting.UserJoinedId = (logSetting.UserJoinedId == null ? (ulong?)channel.Id : default);
2016-12-12 23:44:52 +00:00
break;
case LogType.UserLeft:
2017-10-17 05:03:56 +00:00
channelId = logSetting.UserLeftId = (logSetting.UserLeftId == null ? (ulong?)channel.Id : default);
2016-12-12 23:44:52 +00:00
break;
case LogType.UserBanned:
2017-10-17 05:03:56 +00:00
channelId = logSetting.UserBannedId = (logSetting.UserBannedId == null ? (ulong?)channel.Id : default);
2016-12-12 23:44:52 +00:00
break;
case LogType.UserUnbanned:
2017-10-17 05:03:56 +00:00
channelId = logSetting.UserUnbannedId = (logSetting.UserUnbannedId == null ? (ulong?)channel.Id : default);
2016-12-12 23:44:52 +00:00
break;
case LogType.UserUpdated:
2017-10-17 05:03:56 +00:00
channelId = logSetting.UserUpdatedId = (logSetting.UserUpdatedId == null ? (ulong?)channel.Id : default);
2016-12-12 23:44:52 +00:00
break;
case LogType.UserMuted:
2017-10-17 05:03:56 +00:00
channelId = logSetting.UserMutedId = (logSetting.UserMutedId == null ? (ulong?)channel.Id : default);
break;
2016-12-12 23:44:52 +00:00
case LogType.ChannelCreated:
2017-10-17 05:03:56 +00:00
channelId = logSetting.ChannelCreatedId = (logSetting.ChannelCreatedId == null ? (ulong?)channel.Id : default);
2016-12-12 23:44:52 +00:00
break;
case LogType.ChannelDestroyed:
2017-10-17 05:03:56 +00:00
channelId = logSetting.ChannelDestroyedId = (logSetting.ChannelDestroyedId == null ? (ulong?)channel.Id : default);
2016-12-12 23:44:52 +00:00
break;
case LogType.ChannelUpdated:
2017-10-17 05:03:56 +00:00
channelId = logSetting.ChannelUpdatedId = (logSetting.ChannelUpdatedId == null ? (ulong?)channel.Id : default);
2016-12-12 23:44:52 +00:00
break;
case LogType.UserPresence:
2017-10-17 05:03:56 +00:00
channelId = logSetting.LogUserPresenceId = (logSetting.LogUserPresenceId == null ? (ulong?)channel.Id : default);
2016-12-12 23:44:52 +00:00
break;
case LogType.VoicePresence:
2017-10-17 05:03:56 +00:00
channelId = logSetting.LogVoicePresenceId = (logSetting.LogVoicePresenceId == null ? (ulong?)channel.Id : default);
2016-12-12 23:44:52 +00:00
break;
case LogType.VoicePresenceTTS:
2017-10-17 05:03:56 +00:00
channelId = logSetting.LogVoicePresenceTTSId = (logSetting.LogVoicePresenceTTSId == null ? (ulong?)channel.Id : default);
2016-12-12 23:44:52 +00:00
break;
}
2016-09-06 21:34:00 +00:00
await uow.CompleteAsync().ConfigureAwait(false);
}
2016-12-12 23:44:52 +00:00
if (channelId != null)
2017-02-14 18:06:02 +00:00
await ReplyConfirmLocalized("log", Format.Bold(type.ToString())).ConfigureAwait(false);
2016-09-06 21:34:00 +00:00
else
2017-02-14 18:06:02 +00:00
await ReplyConfirmLocalized("log_stop", Format.Bold(type.ToString())).ConfigureAwait(false);
2016-09-06 21:34:00 +00:00
}
}
}
}