235 lines
11 KiB
C#
235 lines
11 KiB
C#
|
|
#if !GLOBAL_NADEKO
|
|
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;
|
|
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]
|
|
public class LogCommands : NadekoSubmodule<LogCommandService>
|
|
{
|
|
private readonly DbService _db;
|
|
|
|
public LogCommands(DbService db)
|
|
{
|
|
_db = db;
|
|
}
|
|
|
|
public enum EnableDisable
|
|
{
|
|
Enable,
|
|
Disable
|
|
}
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
[RequireContext(ContextType.Guild)]
|
|
[RequireUserPermission(GuildPermission.Administrator)]
|
|
[OwnerOnly]
|
|
public async Task LogServer(PermissionAction action)
|
|
{
|
|
var channel = (ITextChannel)Context.Channel;
|
|
LogSetting logSetting;
|
|
using (var uow = _db.UnitOfWork)
|
|
{
|
|
logSetting = uow.GuildConfigs.LogSettingsFor(channel.Guild.Id).LogSetting;
|
|
_service.GuildLogSettings.AddOrUpdate(channel.Guild.Id, (id) => logSetting, (id, old) => logSetting);
|
|
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 =
|
|
logSetting.UserMutedId =
|
|
logSetting.LogVoicePresenceTTSId = (action.Value ? channel.Id : (ulong?)null);
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
|
}
|
|
if (action.Value)
|
|
await ReplyConfirmLocalized("log_all").ConfigureAwait(false);
|
|
else
|
|
await ReplyConfirmLocalized("log_disabled").ConfigureAwait(false);
|
|
}
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
[RequireContext(ContextType.Guild)]
|
|
[RequireUserPermission(GuildPermission.Administrator)]
|
|
[OwnerOnly]
|
|
public async Task LogIgnore()
|
|
{
|
|
var channel = (ITextChannel)Context.Channel;
|
|
int removed;
|
|
using (var uow = _db.UnitOfWork)
|
|
{
|
|
var config = uow.GuildConfigs.LogSettingsFor(channel.Guild.Id);
|
|
LogSetting logSetting = _service.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)
|
|
{
|
|
var toAdd = new IgnoredLogChannel { ChannelId = channel.Id };
|
|
logSetting.IgnoredChannels.Add(toAdd);
|
|
config.LogSetting.IgnoredChannels.Add(toAdd);
|
|
}
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
|
}
|
|
|
|
if (removed == 0)
|
|
await ReplyConfirmLocalized("log_ignore", Format.Bold(channel.Mention + "(" + channel.Id + ")")).ConfigureAwait(false);
|
|
else
|
|
await ReplyConfirmLocalized("log_not_ignore", Format.Bold(channel.Mention + "(" + channel.Id + ")")).ConfigureAwait(false);
|
|
}
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
[RequireContext(ContextType.Guild)]
|
|
[RequireUserPermission(GuildPermission.Administrator)]
|
|
[OwnerOnly]
|
|
public async Task LogEvents()
|
|
{
|
|
_service.GuildLogSettings.TryGetValue(Context.Guild.Id, out LogSetting l);
|
|
var str = string.Join("\n", Enum.GetNames(typeof(LogType))
|
|
.Select(x =>
|
|
{
|
|
var val = l == null ? null : GetLogProperty(l, Enum.Parse<LogType>(x));
|
|
if (val != null)
|
|
return $"{Format.Bold(x)} <#{val}>";
|
|
return Format.Bold(x);
|
|
}));
|
|
|
|
await Context.Channel.SendConfirmAsync(Format.Bold(GetText("log_events")) + "\n" +
|
|
str)
|
|
.ConfigureAwait(false);
|
|
}
|
|
|
|
private ulong? GetLogProperty(LogSetting l, LogType type)
|
|
{
|
|
switch (type)
|
|
{
|
|
case LogType.Other:
|
|
return l.LogOtherId;
|
|
case LogType.MessageUpdated:
|
|
return l.MessageUpdatedId;
|
|
case LogType.MessageDeleted:
|
|
return l.MessageDeletedId;
|
|
case LogType.UserJoined:
|
|
return l.UserJoinedId;
|
|
case LogType.UserLeft:
|
|
return l.UserLeftId;
|
|
case LogType.UserBanned:
|
|
return l.UserBannedId;
|
|
case LogType.UserUnbanned:
|
|
return l.UserUnbannedId;
|
|
case LogType.UserUpdated:
|
|
return l.UserUpdatedId;
|
|
case LogType.ChannelCreated:
|
|
return l.ChannelCreatedId;
|
|
case LogType.ChannelDestroyed:
|
|
return l.ChannelDestroyedId;
|
|
case LogType.ChannelUpdated:
|
|
return l.ChannelUpdatedId;
|
|
case LogType.UserPresence:
|
|
return l.LogUserPresenceId;
|
|
case LogType.VoicePresence:
|
|
return l.LogVoicePresenceId;
|
|
case LogType.VoicePresenceTTS:
|
|
return l.LogVoicePresenceTTSId;
|
|
case LogType.UserMuted:
|
|
return l.UserMutedId;
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
[RequireContext(ContextType.Guild)]
|
|
[RequireUserPermission(GuildPermission.Administrator)]
|
|
[OwnerOnly]
|
|
public async Task Log(LogType type)
|
|
{
|
|
var channel = (ITextChannel)Context.Channel;
|
|
ulong? channelId = null;
|
|
using (var uow = _db.UnitOfWork)
|
|
{
|
|
var logSetting = uow.GuildConfigs.LogSettingsFor(channel.Guild.Id).LogSetting;
|
|
_service.GuildLogSettings.AddOrUpdate(channel.Guild.Id, (id) => logSetting, (id, old) => logSetting);
|
|
switch (type)
|
|
{
|
|
case LogType.Other:
|
|
channelId = logSetting.LogOtherId = (logSetting.LogOtherId == null ? (ulong?)channel.Id : default);
|
|
break;
|
|
case LogType.MessageUpdated:
|
|
channelId = logSetting.MessageUpdatedId = (logSetting.MessageUpdatedId == null ? (ulong?)channel.Id : default);
|
|
break;
|
|
case LogType.MessageDeleted:
|
|
channelId = logSetting.MessageDeletedId = (logSetting.MessageDeletedId == null ? (ulong?)channel.Id : default);
|
|
break;
|
|
case LogType.UserJoined:
|
|
channelId = logSetting.UserJoinedId = (logSetting.UserJoinedId == null ? (ulong?)channel.Id : default);
|
|
break;
|
|
case LogType.UserLeft:
|
|
channelId = logSetting.UserLeftId = (logSetting.UserLeftId == null ? (ulong?)channel.Id : default);
|
|
break;
|
|
case LogType.UserBanned:
|
|
channelId = logSetting.UserBannedId = (logSetting.UserBannedId == null ? (ulong?)channel.Id : default);
|
|
break;
|
|
case LogType.UserUnbanned:
|
|
channelId = logSetting.UserUnbannedId = (logSetting.UserUnbannedId == null ? (ulong?)channel.Id : default);
|
|
break;
|
|
case LogType.UserUpdated:
|
|
channelId = logSetting.UserUpdatedId = (logSetting.UserUpdatedId == null ? (ulong?)channel.Id : default);
|
|
break;
|
|
case LogType.UserMuted:
|
|
channelId = logSetting.UserMutedId = (logSetting.UserMutedId == null ? (ulong?)channel.Id : default);
|
|
break;
|
|
case LogType.ChannelCreated:
|
|
channelId = logSetting.ChannelCreatedId = (logSetting.ChannelCreatedId == null ? (ulong?)channel.Id : default);
|
|
break;
|
|
case LogType.ChannelDestroyed:
|
|
channelId = logSetting.ChannelDestroyedId = (logSetting.ChannelDestroyedId == null ? (ulong?)channel.Id : default);
|
|
break;
|
|
case LogType.ChannelUpdated:
|
|
channelId = logSetting.ChannelUpdatedId = (logSetting.ChannelUpdatedId == null ? (ulong?)channel.Id : default);
|
|
break;
|
|
case LogType.UserPresence:
|
|
channelId = logSetting.LogUserPresenceId = (logSetting.LogUserPresenceId == null ? (ulong?)channel.Id : default);
|
|
break;
|
|
case LogType.VoicePresence:
|
|
channelId = logSetting.LogVoicePresenceId = (logSetting.LogVoicePresenceId == null ? (ulong?)channel.Id : default);
|
|
break;
|
|
case LogType.VoicePresenceTTS:
|
|
channelId = logSetting.LogVoicePresenceTTSId = (logSetting.LogVoicePresenceTTSId == null ? (ulong?)channel.Id : default);
|
|
break;
|
|
}
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
|
}
|
|
|
|
if (channelId != null)
|
|
await ReplyConfirmLocalized("log", Format.Bold(type.ToString())).ConfigureAwait(false);
|
|
else
|
|
await ReplyConfirmLocalized("log_stop", Format.Bold(type.ToString())).ConfigureAwait(false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif |