;gmod, ;cmd, ;lgp and .resetglobalperms added. Bot owner can disable modules or commands bot-wide.

This commit is contained in:
Kwoth
2017-04-09 22:28:42 +02:00
parent a89d00ff31
commit 98c330d6a6
20 changed files with 2157 additions and 53 deletions

View File

@@ -67,6 +67,23 @@ namespace NadekoBot.Modules.Administration
await ReplyConfirmLocalized("perms_reset").ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[OwnerOnly]
public async Task ResetGlobalPermissions()
{
using (var uow = DbHandler.UnitOfWork())
{
var gc = uow.BotConfig.GetOrCreate();
gc.BlockedCommands.Clear();
gc.BlockedModules.Clear();
GlobalPermissionCommands.BlockedCommands.Clear();
GlobalPermissionCommands.BlockedModules.Clear();
await uow.CompleteAsync();
}
await ReplyConfirmLocalized("global_perms_reset").ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.Administrator)]

View File

@@ -22,7 +22,7 @@ namespace NadekoBot.Modules.Administration
[Group]
public class GameChannelCommands : NadekoSubmodule
{
private static readonly Timer _t;
//private static readonly Timer _t;
private static readonly ConcurrentHashSet<ulong> gameVoiceChannels = new ConcurrentHashSet<ulong>();

View File

@@ -0,0 +1,118 @@
using Discord;
using Discord.Commands;
using NadekoBot.Attributes;
using NadekoBot.DataStructures;
using NadekoBot.Extensions;
using NadekoBot.Services;
using NadekoBot.Services.Database;
using NadekoBot.TypeReaders;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Permissions
{
public partial class Permissions
{
[Group]
public class GlobalPermissionCommands : NadekoSubmodule
{
public static readonly ConcurrentHashSet<string> BlockedModules;
public static readonly ConcurrentHashSet<string> BlockedCommands;
static GlobalPermissionCommands()
{
BlockedModules = new ConcurrentHashSet<string>(NadekoBot.BotConfig.BlockedModules.Select(x => x.Name));
BlockedCommands = new ConcurrentHashSet<string>(NadekoBot.BotConfig.BlockedCommands.Select(x => x.Name));
}
[NadekoCommand, Usage, Description, Aliases]
[OwnerOnly]
public async Task Lgp()
{
if (!BlockedModules.Any() && !BlockedCommands.Any())
{
await ReplyErrorLocalized("lgp_none").ConfigureAwait(false);
return;
}
var embed = new EmbedBuilder().WithOkColor();
if (BlockedModules.Any())
embed.AddField(efb => efb.WithName(GetText("blocked_modules")).WithValue(string.Join("\n", BlockedModules)).WithIsInline(false));
if (BlockedCommands.Any())
embed.AddField(efb => efb.WithName(GetText("blocked_commands")).WithValue(string.Join("\n", BlockedCommands)).WithIsInline(false));
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[OwnerOnly]
public async Task Gmod(ModuleInfo module)
{
var moduleName = module.Name.ToLowerInvariant();
if (BlockedModules.Add(moduleName))
{
using (var uow = DbHandler.UnitOfWork())
{
var bc = uow.BotConfig.GetOrCreate();
bc.BlockedModules.Add(new Services.Database.Models.BlockedCmdOrMdl
{
Name = moduleName,
});
uow.Complete();
}
await ReplyConfirmLocalized("gmod_add", Format.Bold(module.Name)).ConfigureAwait(false);
return;
}
else if (BlockedModules.TryRemove(moduleName))
{
using (var uow = DbHandler.UnitOfWork())
{
var bc = uow.BotConfig.GetOrCreate();
bc.BlockedModules.RemoveWhere(x => x.Name == moduleName);
uow.Complete();
}
await ReplyConfirmLocalized("gmod_remove", Format.Bold(module.Name)).ConfigureAwait(false);
return;
}
}
[NadekoCommand, Usage, Description, Aliases]
[OwnerOnly]
public async Task Gcmd(CommandOrCrInfo cmd)
{
var commandName = cmd.Name.ToLowerInvariant();
if (BlockedCommands.Add(commandName))
{
using (var uow = DbHandler.UnitOfWork())
{
var bc = uow.BotConfig.GetOrCreate();
bc.BlockedCommands.Add(new Services.Database.Models.BlockedCmdOrMdl
{
Name = commandName,
});
uow.Complete();
}
await ReplyConfirmLocalized("gcmd_add", Format.Bold(cmd.Name)).ConfigureAwait(false);
return;
}
else if (BlockedCommands.TryRemove(commandName))
{
using (var uow = DbHandler.UnitOfWork())
{
var bc = uow.BotConfig.GetOrCreate();
bc.BlockedCommands.RemoveWhere(x => x.Name == commandName);
uow.Complete();
}
await ReplyConfirmLocalized("gcmd_remove", Format.Bold(cmd.Name)).ConfigureAwait(false);
return;
}
}
}
}
}

View File

@@ -16,7 +16,6 @@ namespace NadekoBot.Modules.Utility
{
public partial class Utility
{
public class CommandAliasEqualityComparer : IEqualityComparer<CommandAlias>
{
public bool Equals(CommandAlias x, CommandAlias y) => x.Trigger == y.Trigger;
@@ -41,6 +40,11 @@ namespace NadekoBot.Modules.Utility
.ToDictionary(ca => ca.Trigger, ca => ca.Mapping))));
}
public static void Unload()
{
AliasMaps.Clear();
}
[NadekoCommand, Usage, Description, Aliases]
[RequireUserPermission(GuildPermission.Administrator)]
[RequireContext(ContextType.Guild)]

View File

@@ -3,6 +3,7 @@ using Discord.Commands;
using NadekoBot.Attributes;
using NadekoBot.Extensions;
using NadekoBot.Services;
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading.Tasks;
@@ -16,43 +17,50 @@ namespace NadekoBot.Modules.Utility
{
static CrossServerTextChannel()
{
NadekoBot.Client.MessageReceived += async imsg =>
NadekoBot.Client.MessageReceived += Client_MessageReceived;
}
public static void Unload()
{
NadekoBot.Client.MessageReceived -= Client_MessageReceived;
}
private static async Task Client_MessageReceived(Discord.WebSocket.SocketMessage imsg)
{
try
{
try
if (imsg.Author.IsBot)
return;
var msg = imsg as IUserMessage;
if (msg == null)
return;
var channel = imsg.Channel as ITextChannel;
if (channel == null)
return;
if (msg.Author.Id == NadekoBot.Client.CurrentUser.Id) return;
foreach (var subscriber in Subscribers)
{
if (imsg.Author.IsBot)
return;
var msg = imsg as IUserMessage;
if (msg == null)
return;
var channel = imsg.Channel as ITextChannel;
if (channel == null)
return;
if (msg.Author.Id == NadekoBot.Client.CurrentUser.Id) return;
foreach (var subscriber in Subscribers)
var set = subscriber.Value;
if (!set.Contains(channel))
continue;
foreach (var chan in set.Except(new[] { channel }))
{
var set = subscriber.Value;
if (!set.Contains(channel))
continue;
foreach (var chan in set.Except(new[] {channel}))
try
{
try
{
await chan.SendMessageAsync(GetMessage(channel, (IGuildUser) msg.Author,
msg)).ConfigureAwait(false);
}
catch
{
// ignored
}
await chan.SendMessageAsync(GetMessage(channel, (IGuildUser)msg.Author,
msg)).ConfigureAwait(false);
}
catch
{
// ignored
}
}
}
catch
{
// ignored
}
};
}
catch
{
// ignored
}
}
private static string GetMessage(ITextChannel channel, IGuildUser user, IUserMessage message) =>

View File

@@ -48,7 +48,6 @@ namespace NadekoBot.Modules.Utility
Task.Run(Run);
}
private async Task Run()
{
source = new CancellationTokenSource();
@@ -124,7 +123,12 @@ namespace NadekoBot.Modules.Utility
{
var _ = Task.Run(async () =>
{
#if !GLOBAL_NADEKO
await Task.Delay(5000).ConfigureAwait(false);
#else
await Task.Delay(30000).ConfigureAwait(false);
#endif
//todo this is pretty terrible
Repeaters = new ConcurrentDictionary<ulong, ConcurrentQueue<RepeatRunner>>(NadekoBot.AllGuildConfigs
.ToDictionary(gc => gc.GuildId,
gc => new ConcurrentQueue<RepeatRunner>(gc.GuildRepeaters
@@ -134,6 +138,21 @@ namespace NadekoBot.Modules.Utility
});
}
public static void Unload()
{
_ready = false;
foreach (var kvp in Repeaters)
{
RepeatRunner r;
while (kvp.Value.TryDequeue(out r))
{
r.Stop();
}
}
Repeaters.Clear();
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.ManageMessages)]

View File

@@ -29,6 +29,11 @@ namespace NadekoBot.Modules.Utility
patreon = PatreonThingy.Instance;
}
public static void Unload()
{
patreon.Updater.Change(Timeout.Infinite, Timeout.Infinite);
}
[NadekoCommand, Usage, Description, Aliases]
[OwnerOnly]
public async Task PatreonRewardsReload()
@@ -86,7 +91,7 @@ namespace NadekoBot.Modules.Utility
public ImmutableArray<PatreonUserAndReward> Pledges { get; private set; }
public DateTime LastUpdate { get; private set; } = DateTime.UtcNow;
private readonly Timer update;
public readonly Timer Updater;
private readonly SemaphoreSlim claimLockJustInCase = new SemaphoreSlim(1, 1);
private readonly Logger _log;
@@ -97,7 +102,7 @@ namespace NadekoBot.Modules.Utility
if (string.IsNullOrWhiteSpace(NadekoBot.Credentials.PatreonAccessToken))
return;
_log = LogManager.GetCurrentClassLogger();
update = new Timer(async (_) => await LoadPledges(), null, TimeSpan.Zero, Interval);
Updater = new Timer(async (_) => await LoadPledges(), null, TimeSpan.Zero, Interval);
}
public async Task LoadPledges()

View File

@@ -32,10 +32,15 @@ namespace NadekoBot.Modules.Utility
};
private new static readonly Logger _log;
private static readonly CancellationTokenSource cancelSource;
private static readonly CancellationToken cancelAllToken;
static RemindCommands()
{
_log = LogManager.GetCurrentClassLogger();
cancelSource = new CancellationTokenSource();
cancelAllToken = cancelSource.Token;
List<Reminder> reminders;
using (var uow = DbHandler.UnitOfWork())
{
@@ -45,11 +50,17 @@ namespace NadekoBot.Modules.Utility
foreach (var r in reminders)
{
Task.Run(() => StartReminder(r));
Task.Run(() => StartReminder(r, cancelAllToken));
}
}
private static async Task StartReminder(Reminder r)
public static void Unload()
{
if (!cancelSource.IsCancellationRequested)
cancelSource.Cancel();
}
private static async Task StartReminder(Reminder r, CancellationToken t)
{
var now = DateTime.Now;
@@ -58,7 +69,7 @@ namespace NadekoBot.Modules.Utility
if (time.TotalMilliseconds > int.MaxValue)
return;
await Task.Delay(time).ConfigureAwait(false);
await Task.Delay(time, t).ConfigureAwait(false);
try
{
IMessageChannel ch;
@@ -188,7 +199,7 @@ namespace NadekoBot.Modules.Utility
{
// ignored
}
await StartReminder(rem);
await StartReminder(rem, cancelAllToken);
}
[NadekoCommand, Usage, Description, Aliases]

View File

@@ -49,14 +49,19 @@ namespace NadekoBot.Modules.Utility
}
Units = data.ToList();
}
catch (Exception e)
catch (Exception ex)
{
_log.Warn("Could not load units: " + e.Message);
_log.Warn("Could not load units: " + ex.Message);
}
_timer = new Timer(async (obj) => await UpdateCurrency(), null, _updateInterval, _updateInterval);
}
public static void Unload()
{
_timer.Change(Timeout.Infinite, Timeout.Infinite);
}
public static async Task UpdateCurrency()
{
try

View File

@@ -25,6 +25,12 @@ namespace NadekoBot.Modules.Utility
{
private static ConcurrentDictionary<ulong, Timer> _rotatingRoleColors = new ConcurrentDictionary<ulong, Timer>();
public static void Unload()
{
_rotatingRoleColors.ForEach(x => x.Value?.Change(Timeout.Infinite, Timeout.Infinite));
_rotatingRoleColors.Clear();
}
//[NadekoCommand, Usage, Description, Aliases]
//[RequireContext(ContextType.Guild)]
//public async Task Midorina([Remainder] string arg)
@@ -49,7 +55,7 @@ namespace NadekoBot.Modules.Utility
// var roleStrings = roles
// .Select(x => $"{reactions[j++]} -> {x.Name}");
// var msg = await Context.Channel.SendConfirmAsync("Pick a Role",
// string.Join("\n", roleStrings)).ConfigureAwait(false);
@@ -100,6 +106,7 @@ namespace NadekoBot.Modules.Utility
// }
// }));
//}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]