clean MuteCommands.cs

This commit is contained in:
Kwoth 2017-02-14 15:53:51 +01:00
parent e5d85dd818
commit f9c9896926
4 changed files with 24 additions and 28 deletions

View File

@ -22,9 +22,6 @@ namespace NadekoBot.Modules.Administration
[NadekoModule("Administration", ".")] [NadekoModule("Administration", ".")]
public partial class Administration : NadekoModule public partial class Administration : NadekoModule
{ {
private static ConcurrentDictionary<ulong, string> GuildMuteRoles { get; } = new ConcurrentDictionary<ulong, string>();
private static ConcurrentHashSet<ulong> DeleteMessagesOnCommand { get; } = new ConcurrentHashSet<ulong>(); private static ConcurrentHashSet<ulong> DeleteMessagesOnCommand { get; } = new ConcurrentHashSet<ulong>();
private new static Logger _log { get; } private new static Logger _log { get; }
@ -206,7 +203,7 @@ namespace NadekoBot.Modules.Administration
return; return;
} }
var roleName = args[0].ToUpperInvariant(); var roleName = args[0].ToUpperInvariant();
var role = Context.Guild.Roles.Where(r => r.Name.ToUpperInvariant() == roleName).FirstOrDefault(); var role = Context.Guild.Roles.FirstOrDefault(r => r.Name.ToUpperInvariant() == roleName);
if (role == null) if (role == null)
{ {
@ -513,7 +510,7 @@ namespace NadekoBot.Modules.Administration
await Context.Channel.SendMessageAsync(send).ConfigureAwait(false); await Context.Channel.SendMessageAsync(send).ConfigureAwait(false);
} }
IGuild nadekoSupportServer; IGuild _nadekoSupportServer;
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
public async Task Donators() public async Task Donators()
{ {
@ -525,16 +522,13 @@ namespace NadekoBot.Modules.Administration
} }
await Context.Channel.SendConfirmAsync("Thanks to the people listed below for making this project happen!", string.Join("⭐", donatorsOrdered.Select(d => d.Name))).ConfigureAwait(false); await Context.Channel.SendConfirmAsync("Thanks to the people listed below for making this project happen!", string.Join("⭐", donatorsOrdered.Select(d => d.Name))).ConfigureAwait(false);
nadekoSupportServer = nadekoSupportServer ?? NadekoBot.Client.GetGuild(117523346618318850); _nadekoSupportServer = _nadekoSupportServer ?? NadekoBot.Client.GetGuild(117523346618318850);
if (nadekoSupportServer == null) var patreonRole = _nadekoSupportServer?.GetRole(236667642088259585);
return;
var patreonRole = nadekoSupportServer.GetRole(236667642088259585);
if (patreonRole == null) if (patreonRole == null)
return; return;
var usrs = (await nadekoSupportServer.GetUsersAsync()).Where(u => u.RoleIds.Contains(236667642088259585u)); var usrs = (await _nadekoSupportServer.GetUsersAsync()).Where(u => u.RoleIds.Contains(236667642088259585u));
await Context.Channel.SendConfirmAsync("Patreon supporters", string.Join("⭐", usrs.Select(d => d.Username))).ConfigureAwait(false); await Context.Channel.SendConfirmAsync("Patreon supporters", string.Join("⭐", usrs.Select(d => d.Username))).ConfigureAwait(false);
} }

View File

@ -8,8 +8,6 @@ using NadekoBot.Services.Database.Models;
using NLog; using NLog;
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -20,9 +18,8 @@ namespace NadekoBot.Modules.Administration
[Group] [Group]
public class MuteCommands : NadekoSubmodule public class MuteCommands : NadekoSubmodule
{ {
private static ConcurrentDictionary<ulong, string> GuildMuteRoles { get; } = new ConcurrentDictionary<ulong, string>(); private static ConcurrentDictionary<ulong, string> guildMuteRoles { get; }
private static ConcurrentDictionary<ulong, ConcurrentHashSet<ulong>> mutedUsers { get; }
private static ConcurrentDictionary<ulong, ConcurrentHashSet<ulong>> MutedUsers { get; } = new ConcurrentDictionary<ulong, ConcurrentHashSet<ulong>>();
public static event Action<IGuildUser, MuteType> UserMuted = delegate { }; public static event Action<IGuildUser, MuteType> UserMuted = delegate { };
public static event Action<IGuildUser, MuteType> UserUnmuted = delegate { }; public static event Action<IGuildUser, MuteType> UserUnmuted = delegate { };
@ -37,11 +34,11 @@ namespace NadekoBot.Modules.Administration
static MuteCommands() static MuteCommands()
{ {
var configs = NadekoBot.AllGuildConfigs; var configs = NadekoBot.AllGuildConfigs;
GuildMuteRoles = new ConcurrentDictionary<ulong, string>(configs guildMuteRoles = new ConcurrentDictionary<ulong, string>(configs
.Where(c => !string.IsNullOrWhiteSpace(c.MuteRoleName)) .Where(c => !string.IsNullOrWhiteSpace(c.MuteRoleName))
.ToDictionary(c => c.GuildId, c => c.MuteRoleName)); .ToDictionary(c => c.GuildId, c => c.MuteRoleName));
MutedUsers = new ConcurrentDictionary<ulong, ConcurrentHashSet<ulong>>(configs.ToDictionary( mutedUsers = new ConcurrentDictionary<ulong, ConcurrentHashSet<ulong>>(configs.ToDictionary(
k => k.GuildId, k => k.GuildId,
v => new ConcurrentHashSet<ulong>(v.MutedUsers.Select(m => m.UserId)) v => new ConcurrentHashSet<ulong>(v.MutedUsers.Select(m => m.UserId))
)); ));
@ -54,7 +51,7 @@ namespace NadekoBot.Modules.Administration
try try
{ {
ConcurrentHashSet<ulong> muted; ConcurrentHashSet<ulong> muted;
MutedUsers.TryGetValue(usr.Guild.Id, out muted); mutedUsers.TryGetValue(usr.Guild.Id, out muted);
if (muted == null || !muted.Contains(usr.Id)) if (muted == null || !muted.Contains(usr.Id))
return; return;
@ -79,7 +76,7 @@ namespace NadekoBot.Modules.Administration
UserId = usr.Id UserId = usr.Id
}); });
ConcurrentHashSet<ulong> muted; ConcurrentHashSet<ulong> muted;
if (MutedUsers.TryGetValue(usr.Guild.Id, out muted)) if (mutedUsers.TryGetValue(usr.Guild.Id, out muted))
muted.Add(usr.Id); muted.Add(usr.Id);
await uow.CompleteAsync().ConfigureAwait(false); await uow.CompleteAsync().ConfigureAwait(false);
@ -99,7 +96,7 @@ namespace NadekoBot.Modules.Administration
UserId = usr.Id UserId = usr.Id
}); });
ConcurrentHashSet<ulong> muted; ConcurrentHashSet<ulong> muted;
if (MutedUsers.TryGetValue(usr.Guild.Id, out muted)) if (mutedUsers.TryGetValue(usr.Guild.Id, out muted))
muted.TryRemove(usr.Id); muted.TryRemove(usr.Id);
await uow.CompleteAsync().ConfigureAwait(false); await uow.CompleteAsync().ConfigureAwait(false);
} }
@ -110,7 +107,7 @@ namespace NadekoBot.Modules.Administration
{ {
const string defaultMuteRoleName = "nadeko-mute"; const string defaultMuteRoleName = "nadeko-mute";
var muteRoleName = GuildMuteRoles.GetOrAdd(guild.Id, defaultMuteRoleName); var muteRoleName = guildMuteRoles.GetOrAdd(guild.Id, defaultMuteRoleName);
var muteRole = guild.Roles.FirstOrDefault(r => r.Name == muteRoleName); var muteRole = guild.Roles.FirstOrDefault(r => r.Name == muteRoleName);
if (muteRole == null) if (muteRole == null)
@ -132,7 +129,10 @@ namespace NadekoBot.Modules.Administration
await toOverwrite.AddPermissionOverwriteAsync(muteRole, new OverwritePermissions(sendMessages: PermValue.Deny, attachFiles: PermValue.Deny)) await toOverwrite.AddPermissionOverwriteAsync(muteRole, new OverwritePermissions(sendMessages: PermValue.Deny, attachFiles: PermValue.Deny))
.ConfigureAwait(false); .ConfigureAwait(false);
} }
catch { } catch
{
// ignored
}
await Task.Delay(200).ConfigureAwait(false); await Task.Delay(200).ConfigureAwait(false);
} }
} }
@ -145,7 +145,6 @@ namespace NadekoBot.Modules.Administration
[Priority(1)] [Priority(1)]
public async Task SetMuteRole([Remainder] string name) public async Task SetMuteRole([Remainder] string name)
{ {
//var channel = (ITextChannel)Context.Channel;
name = name.Trim(); name = name.Trim();
if (string.IsNullOrWhiteSpace(name)) if (string.IsNullOrWhiteSpace(name))
return; return;
@ -154,7 +153,7 @@ namespace NadekoBot.Modules.Administration
{ {
var config = uow.GuildConfigs.For(Context.Guild.Id, set => set); var config = uow.GuildConfigs.For(Context.Guild.Id, set => set);
config.MuteRoleName = name; config.MuteRoleName = name;
GuildMuteRoles.AddOrUpdate(Context.Guild.Id, name, (id, old) => name); guildMuteRoles.AddOrUpdate(Context.Guild.Id, name, (id, old) => name);
await uow.CompleteAsync().ConfigureAwait(false); await uow.CompleteAsync().ConfigureAwait(false);
} }
await Context.Channel.SendConfirmAsync("☑️ **New mute role set.**").ConfigureAwait(false); await Context.Channel.SendConfirmAsync("☑️ **New mute role set.**").ConfigureAwait(false);

View File

@ -14,6 +14,7 @@ using System.Collections.Generic;
using NadekoBot.Modules.Permissions; using NadekoBot.Modules.Permissions;
using NadekoBot.TypeReaders; using NadekoBot.TypeReaders;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Immutable;
using System.Diagnostics; using System.Diagnostics;
using NadekoBot.Modules.Music; using NadekoBot.Modules.Music;
using NadekoBot.Services.Database.Models; using NadekoBot.Services.Database.Models;
@ -44,7 +45,7 @@ namespace NadekoBot
public static ConcurrentDictionary<string, string> ModulePrefixes { get; private set; } public static ConcurrentDictionary<string, string> ModulePrefixes { get; private set; }
public static bool Ready { get; private set; } public static bool Ready { get; private set; }
public static IEnumerable<GuildConfig> AllGuildConfigs { get; } public static ImmutableArray<GuildConfig> AllGuildConfigs { get; }
public static BotConfig BotConfig { get; } public static BotConfig BotConfig { get; }
static NadekoBot() static NadekoBot()
@ -54,7 +55,7 @@ namespace NadekoBot
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
AllGuildConfigs = uow.GuildConfigs.GetAllGuildConfigs(); AllGuildConfigs = uow.GuildConfigs.GetAllGuildConfigs().ToImmutableArray();
BotConfig = uow.BotConfig.GetOrCreate(); BotConfig = uow.BotConfig.GetOrCreate();
OkColor = new Color(Convert.ToUInt32(BotConfig.OkColor, 16)); OkColor = new Color(Convert.ToUInt32(BotConfig.OkColor, 16));
ErrorColor = new Color(Convert.ToUInt32(BotConfig.ErrorColor, 16)); ErrorColor = new Color(Convert.ToUInt32(BotConfig.ErrorColor, 16));

View File

@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=modules_005Cadministration_005Ccommands/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>