diff --git a/Boyfriend/Boyfriend.cs b/Boyfriend/Boyfriend.cs index df38768..b0ec6ce 100644 --- a/Boyfriend/Boyfriend.cs +++ b/Boyfriend/Boyfriend.cs @@ -83,7 +83,7 @@ public static class Boyfriend { if (!RemovedRolesDictionary.ContainsKey(id)) RemovedRolesDictionary.Add(id, new Dictionary>()); - if (GuildConfigDictionary.ContainsKey(id)) return GuildConfigDictionary[id]; + if (GuildConfigDictionary.TryGetValue(id, out var cfg)) return cfg; var path = $"config_{id}.json"; @@ -110,7 +110,7 @@ public static class Boyfriend { } public static Dictionary> GetRemovedRoles(ulong id) { - if (RemovedRolesDictionary.ContainsKey(id)) return RemovedRolesDictionary[id]; + if (RemovedRolesDictionary.TryGetValue(id, out var dict)) return dict; var path = $"removedroles_{id}.json"; @@ -126,7 +126,7 @@ public static class Boyfriend { } public static SocketGuild FindGuild(ulong channel) { - if (GuildCache.ContainsKey(channel)) return GuildCache[channel]; + if (GuildCache.TryGetValue(channel, out var gld)) return gld; foreach (var guild in Client.Guilds) { // ReSharper disable once LoopCanBeConvertedToQuery foreach (var x in guild.Channels) diff --git a/Boyfriend/Commands/MuteCommand.cs b/Boyfriend/Commands/MuteCommand.cs index 998c65e..bc608f9 100644 --- a/Boyfriend/Commands/MuteCommand.cs +++ b/Boyfriend/Commands/MuteCommand.cs @@ -26,8 +26,8 @@ public sealed class MuteCommand : ICommand { var rolesRemoved = Boyfriend.GetRemovedRoles(cmd.Context.Guild.Id); - if (rolesRemoved.ContainsKey(toMute.Id)) { - foreach (var roleId in rolesRemoved[toMute.Id]) await toMute.AddRoleAsync(roleId); + if (rolesRemoved.TryGetValue(toMute.Id, out var mutedRemovedRoles)) { + foreach (var roleId in mutedRemovedRoles) await toMute.AddRoleAsync(roleId); rolesRemoved.Remove(toMute.Id); cmd.ConfigWriteScheduled = true; cmd.Reply(Messages.RolesReturned, ":warning: "); diff --git a/Boyfriend/Commands/UnmuteCommand.cs b/Boyfriend/Commands/UnmuteCommand.cs index 8297830..b6a1aa9 100644 --- a/Boyfriend/Commands/UnmuteCommand.cs +++ b/Boyfriend/Commands/UnmuteCommand.cs @@ -23,8 +23,8 @@ public sealed class UnmuteCommand : ICommand { if (role != null && toUnmute.Roles.Contains(role)) { var rolesRemoved = Boyfriend.GetRemovedRoles(cmd.Context.Guild.Id); - if (rolesRemoved.ContainsKey(toUnmute.Id)) { - await toUnmute.AddRolesAsync(rolesRemoved[toUnmute.Id]); + if (rolesRemoved.TryGetValue(toUnmute.Id, out var unmutedRemovedRoles)) { + await toUnmute.AddRolesAsync(unmutedRemovedRoles); rolesRemoved.Remove(toUnmute.Id); cmd.ConfigWriteScheduled = true; } diff --git a/Boyfriend/Utils.cs b/Boyfriend/Utils.cs index 03fa038..85114a8 100644 --- a/Boyfriend/Utils.cs +++ b/Boyfriend/Utils.cs @@ -65,7 +65,7 @@ public static class Utils { public static SocketRole? GetMuteRole(SocketGuild guild) { var id = ulong.Parse(Boyfriend.GetGuildConfig(guild.Id)["MuteRole"]); - if (MuteRoleCache.ContainsKey(id)) return MuteRoleCache[id]; + if (MuteRoleCache.TryGetValue(id, out var cachedMuteRole)) return cachedMuteRole; SocketRole? role = null; foreach (var x in guild.Roles) { if (x.Id != id) continue; @@ -97,7 +97,7 @@ public static class Utils { public static string GetMessage(string name) { var propertyName = name; name = $"{Messages.Culture}/{name}"; - if (ReflectionMessageCache.ContainsKey(name)) return ReflectionMessageCache[name]; + if (ReflectionMessageCache.TryGetValue(name, out var cachedMessage)) return cachedMessage; var toReturn = typeof(Messages).GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Static)?.GetValue(null)