From ead27eacc6e459ee791038d99b921fd4662a67bd Mon Sep 17 00:00:00 2001 From: Kwoth Date: Tue, 18 Oct 2016 03:03:46 +0200 Subject: [PATCH] Fixed .resetperms, fixed crashes --- .../Modules/Administration/Administration.cs | 9 ++ .../Commands/AutoAssignRoleCommands.cs | 6 +- .../Administration/Commands/LogCommand.cs | 4 +- .../Commands/RatelimitCommand.cs | 5 +- .../Commands/ServerGreetCommands.cs | 110 ++++++++++-------- .../Commands/VoicePlusTextCommands.cs | 1 + 6 files changed, 80 insertions(+), 55 deletions(-) diff --git a/src/NadekoBot/Modules/Administration/Administration.cs b/src/NadekoBot/Modules/Administration/Administration.cs index b3a7cd36..2aab66e7 100644 --- a/src/NadekoBot/Modules/Administration/Administration.cs +++ b/src/NadekoBot/Modules/Administration/Administration.cs @@ -16,6 +16,7 @@ using NadekoBot.Services.Database.Models; using System.Net.Http; using ImageProcessorCore; using System.IO; +using static NadekoBot.Modules.Permissions.Permissions; namespace NadekoBot.Modules.Administration { @@ -62,6 +63,14 @@ namespace NadekoBot.Modules.Administration { var config = uow.GuildConfigs.PermissionsFor(channel.Guild.Id); config.RootPermission = Permission.GetDefaultRoot(); + var toAdd = new PermissionCache() + { + RootPermission = config.RootPermission, + PermRole = config.PermissionRole, + Verbose = config.VerbosePermissions, + }; + Permissions.Permissions.Cache.AddOrUpdate(channel.Guild.Id, + toAdd, (id, old) => toAdd); await uow.CompleteAsync(); } diff --git a/src/NadekoBot/Modules/Administration/Commands/AutoAssignRoleCommands.cs b/src/NadekoBot/Modules/Administration/Commands/AutoAssignRoleCommands.cs index dd35927c..47abda51 100644 --- a/src/NadekoBot/Modules/Administration/Commands/AutoAssignRoleCommands.cs +++ b/src/NadekoBot/Modules/Administration/Commands/AutoAssignRoleCommands.cs @@ -5,6 +5,7 @@ using NadekoBot.Attributes; using NadekoBot.Services; using NadekoBot.Services.Database; using NadekoBot.Services.Database.Models; +using NLog; using System; using System.Linq; using System.Threading.Tasks; @@ -16,9 +17,12 @@ namespace NadekoBot.Modules.Administration [Group] public class AutoAssignRoleCommands { + private Logger _log { get; } + public AutoAssignRoleCommands() { var _client = NadekoBot.Client; + this._log = LogManager.GetCurrentClassLogger(); _client.UserJoined += (user) => { var t = Task.Run(async () => @@ -35,7 +39,7 @@ namespace NadekoBot.Modules.Administration var role = user.Guild.Roles.FirstOrDefault(r => r.Id == conf.AutoAssignRoleId); if (role != null) - await user.AddRolesAsync(role); + try { await user.AddRolesAsync(role); } catch (Exception ex) { _log.Warn(ex); } }); return Task.CompletedTask; }; diff --git a/src/NadekoBot/Modules/Administration/Commands/LogCommand.cs b/src/NadekoBot/Modules/Administration/Commands/LogCommand.cs index f7aba236..4d437938 100644 --- a/src/NadekoBot/Modules/Administration/Commands/LogCommand.cs +++ b/src/NadekoBot/Modules/Administration/Commands/LogCommand.cs @@ -112,7 +112,7 @@ namespace NadekoBot.Modules.Administration } else return; - await logChannel.SendMessageAsync(str).ConfigureAwait(false); + try { await logChannel.SendMessageAsync(str).ConfigureAwait(false); } catch (Exception ex) { _log.Warn(ex); } }); return Task.CompletedTask; @@ -367,7 +367,7 @@ namespace NadekoBot.Modules.Administration 👤`{msg.Author.Username}`: {msg.Resolve(userHandling:UserMentionHandling.NameAndDiscriminator)}"; if (msg.Attachments.Any()) str += $"{Environment.NewLine}`Attachements`: {string.Join(", ", msg.Attachments.Select(a => a.ProxyUrl))}"; - await logChannel.SendMessageAsync(str).ConfigureAwait(false); + try { await logChannel.SendMessageAsync(str).ConfigureAwait(false); } catch (Exception ex) { _log.Warn(ex); } }); return Task.CompletedTask; diff --git a/src/NadekoBot/Modules/Administration/Commands/RatelimitCommand.cs b/src/NadekoBot/Modules/Administration/Commands/RatelimitCommand.cs index 51ca481e..1ca14edf 100644 --- a/src/NadekoBot/Modules/Administration/Commands/RatelimitCommand.cs +++ b/src/NadekoBot/Modules/Administration/Commands/RatelimitCommand.cs @@ -3,6 +3,7 @@ using Discord.Commands; using Discord.WebSocket; using NadekoBot.Attributes; using NadekoBot.Extensions; +using NLog; using System; using System.Collections.Concurrent; using System.Threading; @@ -16,6 +17,7 @@ namespace NadekoBot.Modules.Administration public class RatelimitCommand { public static ConcurrentDictionary RatelimitingChannels = new ConcurrentDictionary(); + private Logger _log { get; } private ShardedDiscordClient _client { get; } @@ -63,6 +65,7 @@ namespace NadekoBot.Modules.Administration public RatelimitCommand() { this._client = NadekoBot.Client; + this._log = LogManager.GetCurrentClassLogger(); _client.MessageReceived += (umsg) => { @@ -78,7 +81,7 @@ namespace NadekoBot.Modules.Administration return; if (limiter.CheckUserRatelimit(usrMsg.Author.Id)) - await usrMsg.DeleteAsync(); + try { await usrMsg.DeleteAsync(); } catch (Exception ex) { _log.Warn(ex); } }); return Task.CompletedTask; }; diff --git a/src/NadekoBot/Modules/Administration/Commands/ServerGreetCommands.cs b/src/NadekoBot/Modules/Administration/Commands/ServerGreetCommands.cs index 8d6e7f16..107aface 100644 --- a/src/NadekoBot/Modules/Administration/Commands/ServerGreetCommands.cs +++ b/src/NadekoBot/Modules/Administration/Commands/ServerGreetCommands.cs @@ -31,34 +31,38 @@ namespace NadekoBot.Modules.Administration { var leftTask = Task.Run(async () => { - GuildConfig conf; - using (var uow = DbHandler.UnitOfWork()) - { - conf = uow.GuildConfigs.For(user.Guild.Id); - } - - if (!conf.SendChannelByeMessage) return; - var channel = (await user.Guild.GetTextChannelsAsync()).SingleOrDefault(c => c.Id == conf.ByeMessageChannelId); - - if (channel == null) //maybe warn the server owner that the channel is missing - return; - - var msg = conf.ChannelByeMessageText.Replace("%user%", user.Username).Replace("%id%", user.Id.ToString()).Replace("%server%", user.Guild.Name); - if (string.IsNullOrWhiteSpace(msg)) - return; try { - var toDelete = await channel.SendMessageAsync(msg.SanitizeMentions()).ConfigureAwait(false); - if (conf.AutoDeleteByeMessages) + GuildConfig conf; + using (var uow = DbHandler.UnitOfWork()) { - var t = Task.Run(async () => - { - await Task.Delay(conf.AutoDeleteGreetMessagesTimer * 1000).ConfigureAwait(false); // 5 minutes - await toDelete.DeleteAsync().ConfigureAwait(false); - }); + conf = uow.GuildConfigs.For(user.Guild.Id); } + + if (!conf.SendChannelByeMessage) return; + var channel = (await user.Guild.GetTextChannelsAsync()).SingleOrDefault(c => c.Id == conf.ByeMessageChannelId); + + if (channel == null) //maybe warn the server owner that the channel is missing + return; + + var msg = conf.ChannelByeMessageText.Replace("%user%", user.Username).Replace("%id%", user.Id.ToString()).Replace("%server%", user.Guild.Name); + if (string.IsNullOrWhiteSpace(msg)) + return; + try + { + var toDelete = await channel.SendMessageAsync(msg.SanitizeMentions()).ConfigureAwait(false); + if (conf.AutoDeleteByeMessages) + { + var t = Task.Run(async () => + { + await Task.Delay(conf.AutoDeleteGreetMessagesTimer * 1000).ConfigureAwait(false); // 5 minutes + try { await toDelete.DeleteAsync().ConfigureAwait(false); } catch { } + }); + } + } + catch (Exception ex) { _log.Warn(ex); } } - catch (Exception ex) { _log.Warn(ex); } + catch { } }); return Task.CompletedTask; } @@ -67,50 +71,54 @@ namespace NadekoBot.Modules.Administration { var joinedTask = Task.Run(async () => { - GuildConfig conf; - using (var uow = DbHandler.UnitOfWork()) + try { - conf = uow.GuildConfigs.For(user.Guild.Id); - } - - if (conf.SendChannelGreetMessage) - { - var channel = (await user.Guild.GetTextChannelsAsync()).SingleOrDefault(c => c.Id == conf.GreetMessageChannelId); - if (channel != null) //maybe warn the server owner that the channel is missing + GuildConfig conf; + using (var uow = DbHandler.UnitOfWork()) { - var msg = conf.ChannelGreetMessageText.Replace("%user%", user.Mention).Replace("%id%", user.Id.ToString()).Replace("%server%", user.Guild.Name); - if (!string.IsNullOrWhiteSpace(msg)) + conf = uow.GuildConfigs.For(user.Guild.Id); + } + + if (conf.SendChannelGreetMessage) + { + var channel = (await user.Guild.GetTextChannelsAsync()).SingleOrDefault(c => c.Id == conf.GreetMessageChannelId); + if (channel != null) //maybe warn the server owner that the channel is missing { - try + var msg = conf.ChannelGreetMessageText.Replace("%user%", user.Mention).Replace("%id%", user.Id.ToString()).Replace("%server%", user.Guild.Name); + if (!string.IsNullOrWhiteSpace(msg)) { - var toDelete = await channel.SendMessageAsync(msg.SanitizeMentions()).ConfigureAwait(false); - if (conf.AutoDeleteGreetMessages) + try { - var t = Task.Run(async () => + var toDelete = await channel.SendMessageAsync(msg.SanitizeMentions()).ConfigureAwait(false); + if (conf.AutoDeleteGreetMessages) { - await Task.Delay(conf.AutoDeleteGreetMessagesTimer * 1000).ConfigureAwait(false); // 5 minutes - await toDelete.DeleteAsync().ConfigureAwait(false); - }); + var t = Task.Run(async () => + { + await Task.Delay(conf.AutoDeleteGreetMessagesTimer * 1000).ConfigureAwait(false); // 5 minutes + try { await toDelete.DeleteAsync().ConfigureAwait(false); } catch { } + }); + } } + catch (Exception ex) { _log.Warn(ex); } } - catch (Exception ex) { _log.Warn(ex); } } } - } - if (conf.SendDmGreetMessage) - { - var channel = await user.CreateDMChannelAsync(); - - if (channel != null) + if (conf.SendDmGreetMessage) { - var msg = conf.DmGreetMessageText.Replace("%user%", user.Username).Replace("%server%", user.Guild.Name); - if (!string.IsNullOrWhiteSpace(msg)) + var channel = await user.CreateDMChannelAsync(); + + if (channel != null) { - await channel.SendMessageAsync(msg).ConfigureAwait(false); + var msg = conf.DmGreetMessageText.Replace("%user%", user.Username).Replace("%server%", user.Guild.Name); + if (!string.IsNullOrWhiteSpace(msg)) + { + await channel.SendMessageAsync(msg).ConfigureAwait(false); + } } } } + catch { } }); return Task.CompletedTask; } diff --git a/src/NadekoBot/Modules/Administration/Commands/VoicePlusTextCommands.cs b/src/NadekoBot/Modules/Administration/Commands/VoicePlusTextCommands.cs index 9696c188..0d0d7da3 100644 --- a/src/NadekoBot/Modules/Administration/Commands/VoicePlusTextCommands.cs +++ b/src/NadekoBot/Modules/Administration/Commands/VoicePlusTextCommands.cs @@ -54,6 +54,7 @@ namespace NadekoBot.Modules.Administration { uow.GuildConfigs.For(before.Guild.Id).VoicePlusTextEnabled = false; voicePlusTextCache.TryUpdate(guild.Id, false, true); + await uow.CompleteAsync().ConfigureAwait(false); } return; }