diff --git a/src/NadekoBot/Modules/Administration/Administration.cs b/src/NadekoBot/Modules/Administration/Administration.cs index d7f72186..912d457d 100644 --- a/src/NadekoBot/Modules/Administration/Administration.cs +++ b/src/NadekoBot/Modules/Administration/Administration.cs @@ -88,8 +88,12 @@ namespace NadekoBot.Modules.Administration foreach (var toOverwrite in guild.GetTextChannels()) { - await toOverwrite.AddPermissionOverwriteAsync(muteRole, new OverwritePermissions(sendMessages: PermValue.Deny, attachFiles: PermValue.Deny)) - .ConfigureAwait(false); + try + { + await toOverwrite.AddPermissionOverwriteAsync(muteRole, new OverwritePermissions(sendMessages: PermValue.Deny, attachFiles: PermValue.Deny)) + .ConfigureAwait(false); + } + catch { } await Task.Delay(200).ConfigureAwait(false); } } diff --git a/src/NadekoBot/Modules/Administration/Commands/AntiRaidCommands.cs b/src/NadekoBot/Modules/Administration/Commands/AntiRaidCommands.cs index 2550a94d..60b6b412 100644 --- a/src/NadekoBot/Modules/Administration/Commands/AntiRaidCommands.cs +++ b/src/NadekoBot/Modules/Administration/Commands/AntiRaidCommands.cs @@ -36,7 +36,6 @@ namespace NadekoBot.Modules.Administration public int UserThreshold { get; set; } public int Seconds { get; set; } public PunishmentAction Action { get; set; } - public IRole MuteRole { get; set; } public int UsersCount { get; set; } public ConcurrentHashSet RaidUsers { get; set; } = new ConcurrentHashSet(); } @@ -45,7 +44,6 @@ namespace NadekoBot.Modules.Administration { public PunishmentAction Action { get; set; } public int MessageThreshold { get; set; } = 3; - public IRole MuteRole { get; set; } public ConcurrentDictionary UserStats { get; set; } = new ConcurrentDictionary(); } @@ -114,9 +112,9 @@ namespace NadekoBot.Modules.Administration { if (spamSettings.UserStats.TryRemove(msg.Author.Id, out stats)) { - var log = await PunishUser((IGuildUser)msg.Author, spamSettings.Action, spamSettings.MuteRole, ProtectionType.Spamming) + await PunishUsers(spamSettings.Action, await GetMuteRole(channel.Guild), ProtectionType.Spamming, (IGuildUser)msg.Author) .ConfigureAwait(false); - await channel.Guild.SendMessageToOwnerAsync(log).ConfigureAwait(false); } + } } } catch { } @@ -142,16 +140,11 @@ namespace NadekoBot.Modules.Administration if (settings.UsersCount >= settings.UserThreshold) { - var users = settings.RaidUsers.ToList(); + var users = settings.RaidUsers.ToArray(); settings.RaidUsers.Clear(); - string msg = ""; - foreach (var gu in users) - { - msg += await PunishUser(gu, settings.Action, settings.MuteRole, ProtectionType.Raiding).ConfigureAwait(false); - } - try { await usr.Guild.SendMessageToOwnerAsync(msg).ConfigureAwait(false); } catch { } - } + await PunishUsers(settings.Action, await GetMuteRole(usr.Guild), ProtectionType.Raiding, users).ConfigureAwait(false); + } await Task.Delay(1000 * settings.Seconds).ConfigureAwait(false); settings.RaidUsers.TryRemove(usr); @@ -161,50 +154,49 @@ namespace NadekoBot.Modules.Administration return Task.CompletedTask; }; } - - private async Task PunishUser(IGuildUser gu, PunishmentAction action, IRole muteRole, ProtectionType pt) + + private async Task PunishUsers(PunishmentAction action, IRole muteRole, ProtectionType pt, params IGuildUser[] gus) { - switch (action) + foreach (var gu in gus) { - case PunishmentAction.Mute: - try - { - await gu.AddRolesAsync(muteRole); - return $"{Format.Bold(gu.ToString())} was **MUTED** due to `{pt}` protection on **{gu.Guild.Name}** server.\n"; - } - catch (Exception ex) { _log.Warn(ex, "I can't apply punishement"); } - break; - case PunishmentAction.Kick: - try - { - await gu.Guild.AddBanAsync(gu, 7); + switch (action) + { + case PunishmentAction.Mute: try { - await gu.Guild.RemoveBanAsync(gu); + await gu.AddRolesAsync(muteRole); } - catch + catch (Exception ex) { _log.Warn(ex, "I can't apply punishement"); } + break; + case PunishmentAction.Kick: + try { - await gu.Guild.RemoveBanAsync(gu); - // try it twice, really don't want to ban user if - // only kick has been specified as the punishement + await gu.Guild.AddBanAsync(gu, 7); + try + { + await gu.Guild.RemoveBanAsync(gu); + } + catch + { + await gu.Guild.RemoveBanAsync(gu); + // try it twice, really don't want to ban user if + // only kick has been specified as the punishement + } } - return $"{Format.Bold(gu.ToString())} was **KICKED** due to `{pt}` protection on **{gu.Guild.Name}** server.\n"; - - } - catch (Exception ex) { _log.Warn(ex, "I can't apply punishment"); } - break; - case PunishmentAction.Ban: - try - { - await gu.Guild.AddBanAsync(gu, 7); - return $"{Format.Bold(gu.ToString())} was **BANNED** due to `{pt}` protection on **{gu.Guild.Name}** server.\n"; - } - catch (Exception ex) { _log.Warn(ex, "I can't apply punishment"); } - break; - default: - break; + catch (Exception ex) { _log.Warn(ex, "I can't apply punishment"); } + break; + case PunishmentAction.Ban: + try + { + await gu.Guild.AddBanAsync(gu, 7); + } + catch (Exception ex) { _log.Warn(ex, "I can't apply punishment"); } + break; + default: + break; + } } - return String.Empty; + await LogCommands.TriggeredAntiProtection(gus, action, pt).ConfigureAwait(false); } @@ -227,10 +219,9 @@ namespace NadekoBot.Modules.Administration return; } - IRole muteRole; try { - muteRole = await GetMuteRole(channel.Guild).ConfigureAwait(false); + await GetMuteRole(channel.Guild).ConfigureAwait(false); } catch (Exception ex) { @@ -246,7 +237,6 @@ namespace NadekoBot.Modules.Administration Action = action, Seconds = seconds, UserThreshold = userThreshold, - MuteRole = muteRole, }; antiRaidGuilds.AddOrUpdate(channel.Guild.Id, setting, (id, old) => setting); @@ -271,10 +261,22 @@ namespace NadekoBot.Modules.Administration } else { + try + { + await GetMuteRole(channel.Guild).ConfigureAwait(false); + } + catch (Exception ex) + { + await channel.SendMessageAsync("Failed creating a mute role. Give me ManageRoles permission" + + "or create 'nadeko-mute' role with disabled SendMessages and try again.") + .ConfigureAwait(false); + _log.Warn(ex); + return; + } + if (antiSpamGuilds.TryAdd(channel.Guild.Id, new AntiSpamSetting() { Action = action, - MuteRole = await GetMuteRole(channel.Guild).ConfigureAwait(false), MessageThreshold = messageCount, })) await channel.SendMessageAsync("`Anti-Spam feature enabled on this server.`").ConfigureAwait(false); diff --git a/src/NadekoBot/Modules/Administration/Commands/LogCommand.cs b/src/NadekoBot/Modules/Administration/Commands/LogCommand.cs index 23aa8e7c..a366baef 100644 --- a/src/NadekoBot/Modules/Administration/Commands/LogCommand.cs +++ b/src/NadekoBot/Modules/Administration/Commands/LogCommand.cs @@ -22,21 +22,20 @@ namespace NadekoBot.Modules.Administration [Group] public class LogCommands { - private ShardedDiscordClient _client { get; } - private Logger _log { get; } + private static ShardedDiscordClient _client { get; } + private static Logger _log { get; } - private string prettyCurrentTime => $"【{DateTime.Now:HH:mm:ss}】"; + private static string prettyCurrentTime => $"【{DateTime.Now:HH:mm:ss}】"; - public ConcurrentDictionary GuildLogSettings { get; } + public static ConcurrentDictionary GuildLogSettings { get; } - private ConcurrentDictionary> UserPresenceUpdates { get; } = new ConcurrentDictionary>(); - private Timer t; + private static ConcurrentDictionary> UserPresenceUpdates { get; } = new ConcurrentDictionary>(); + private static Timer timerReference { get; } private IGoogleApiService _google { get; } - public LogCommands(ShardedDiscordClient client, IGoogleApiService google) + static LogCommands() { - _client = client; - _google = google; + _client = NadekoBot.Client; _log = LogManager.GetCurrentClassLogger(); using (var uow = DbHandler.UnitOfWork()) @@ -45,7 +44,7 @@ namespace NadekoBot.Modules.Administration .ToDictionary(g => g.GuildId, g => g.LogSetting)); } - t = new Timer(async (state) => + timerReference = new Timer(async (state) => { try { @@ -63,8 +62,10 @@ namespace NadekoBot.Modules.Administration _log.Warn(ex); } }, null, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10)); - + } + public LogCommands(ShardedDiscordClient client) + { //_client.MessageReceived += _client_MessageReceived; _client.MessageUpdated += _client_MessageUpdated; _client.MessageDeleted += _client_MessageDeleted; @@ -81,6 +82,36 @@ namespace NadekoBot.Modules.Administration _client.ChannelUpdated += _client_ChannelUpdated; } + public static async Task TriggeredAntiProtection(IGuildUser[] users, PunishmentAction action, ProtectionType protection) + { + if (users.Length == 0) + return; + + LogSetting logSetting; + if (!GuildLogSettings.TryGetValue(users.First().Guild.Id, out logSetting) + || !logSetting.IsLogging) + return; + ITextChannel logChannel; + if ((logChannel = TryGetLogChannel(users.First().Guild, logSetting)) == null) + return; + + var punishment = ""; + if (action == PunishmentAction.Mute) + { + punishment = "MUTED"; + } + else if (action == PunishmentAction.Kick) + { + punishment = "KICKED"; + } + else if (action == PunishmentAction.Ban) + { + punishment = "BANNED"; + } + await logChannel.SendMessageAsync(String.Join("\n",users.Select(user=>$"{Format.Bold(user.ToString())} was **{punishment}** due to `{protection}` protection on **{user.Guild.Name}** server."))) + .ConfigureAwait(false); + } + private Task _client_UserUpdated(IGuildUser before, IGuildUser after) { LogSetting logSetting; @@ -461,7 +492,7 @@ namespace NadekoBot.Modules.Administration // } private enum LogChannelType { Text, Voice, UserPresence }; - private ITextChannel TryGetLogChannel(IGuild guild, LogSetting logSetting, LogChannelType logChannelType = LogChannelType.Text) + private static ITextChannel TryGetLogChannel(IGuild guild, LogSetting logSetting, LogChannelType logChannelType = LogChannelType.Text) { ulong id = 0; switch (logChannelType) diff --git a/src/NadekoBot/Modules/Games/Commands/CleverBotCommands.cs b/src/NadekoBot/Modules/Games/Commands/CleverBotCommands.cs index 5858fb63..8ee2d9b0 100644 --- a/src/NadekoBot/Modules/Games/Commands/CleverBotCommands.cs +++ b/src/NadekoBot/Modules/Games/Commands/CleverBotCommands.cs @@ -83,7 +83,7 @@ namespace NadekoBot.Modules.Games catch (Exception ex) { _log.Warn(ex, "Eror sending response"); - await msg.Channel.SendMessageAsync(response).ConfigureAwait(false); // try twice :\ + await msg.Channel.SendMessageAsync(msg.Author.Mention+" "+response).ConfigureAwait(false); // try twice :\ } return true; } diff --git a/src/NadekoBot/Modules/Searches/Searches.cs b/src/NadekoBot/Modules/Searches/Searches.cs index 9a370f80..38d398ed 100644 --- a/src/NadekoBot/Modules/Searches/Searches.cs +++ b/src/NadekoBot/Modules/Searches/Searches.cs @@ -206,9 +206,16 @@ $@"🌍 **Weather for** 【{obj["target"]}】 var arg = name; if (string.IsNullOrWhiteSpace(arg)) { - await channel.SendMessageAsync("💢 Please enter a card name to search for.").ConfigureAwait(false); + await channel.SendMessageAsync("💢 `Please enter a card name to search for.`").ConfigureAwait(false); return; } + + if (string.IsNullOrWhiteSpace(NadekoBot.Credentials.MashapeKey)) + { + await channel.SendMessageAsync("💢 `Bot owner didn't specify MashapeApiKey. You can't use this functionality.`").ConfigureAwait(false); + return; + } + await umsg.Channel.TriggerTypingAsync().ConfigureAwait(false); string response = ""; using (var http = new HttpClient()) @@ -256,10 +263,16 @@ $@"🌍 **Weather for** 【{obj["target"]}】 { var channel = (ITextChannel)umsg.Channel; + if (string.IsNullOrWhiteSpace(NadekoBot.Credentials.MashapeKey)) + { + await channel.SendMessageAsync("💢 `Bot owner didn't specify MashapeApiKey. You can't use this functionality.`").ConfigureAwait(false); + return; + } + var arg = query; if (string.IsNullOrWhiteSpace(arg)) { - await channel.SendMessageAsync("💢 Please enter a search term.").ConfigureAwait(false); + await channel.SendMessageAsync("💢 `Please enter a search term.`").ConfigureAwait(false); return; } await umsg.Channel.TriggerTypingAsync().ConfigureAwait(false); @@ -293,9 +306,15 @@ $@"🌍 **Weather for** 【{obj["target"]}】 var arg = query; if (string.IsNullOrWhiteSpace(arg)) { - await channel.SendMessageAsync("💢 Please enter a search term.").ConfigureAwait(false); + await channel.SendMessageAsync("💢 `Please enter a search term.`").ConfigureAwait(false); return; } + if (string.IsNullOrWhiteSpace(NadekoBot.Credentials.MashapeKey)) + { + await channel.SendMessageAsync("💢 `Bot owner didn't specify MashapeApiKey. You can't use this functionality.`").ConfigureAwait(false); + return; + } + await umsg.Channel.TriggerTypingAsync().ConfigureAwait(false); string res = ""; using (var http = new HttpClient()) diff --git a/src/NadekoBot/Resources/CommandStrings.resx b/src/NadekoBot/Resources/CommandStrings.resx index c14896e3..ad45c5ee 100644 --- a/src/NadekoBot/Resources/CommandStrings.resx +++ b/src/NadekoBot/Resources/CommandStrings.resx @@ -2470,7 +2470,7 @@ setmuterole - Sets a name of the role which will be assigned to people who should be muted. Default is nadeko-mute. After specifying this role, restart commands which use mute as punishment. + Sets a name of the role which will be assigned to people who should be muted. Default is nadeko-mute. `{0}setmuterole Silenced`