mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-01-31 09:09:00 +03:00
Use TryGetValue instead of ContainsKey and retrieving afterwards
This commit is contained in:
parent
59d9423b5f
commit
7afd00bf30
4 changed files with 9 additions and 9 deletions
|
@ -83,7 +83,7 @@ public static class Boyfriend {
|
||||||
if (!RemovedRolesDictionary.ContainsKey(id))
|
if (!RemovedRolesDictionary.ContainsKey(id))
|
||||||
RemovedRolesDictionary.Add(id, new Dictionary<ulong, ReadOnlyCollection<ulong>>());
|
RemovedRolesDictionary.Add(id, new Dictionary<ulong, ReadOnlyCollection<ulong>>());
|
||||||
|
|
||||||
if (GuildConfigDictionary.ContainsKey(id)) return GuildConfigDictionary[id];
|
if (GuildConfigDictionary.TryGetValue(id, out var cfg)) return cfg;
|
||||||
|
|
||||||
var path = $"config_{id}.json";
|
var path = $"config_{id}.json";
|
||||||
|
|
||||||
|
@ -110,7 +110,7 @@ public static class Boyfriend {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Dictionary<ulong, ReadOnlyCollection<ulong>> GetRemovedRoles(ulong id) {
|
public static Dictionary<ulong, ReadOnlyCollection<ulong>> GetRemovedRoles(ulong id) {
|
||||||
if (RemovedRolesDictionary.ContainsKey(id)) return RemovedRolesDictionary[id];
|
if (RemovedRolesDictionary.TryGetValue(id, out var dict)) return dict;
|
||||||
|
|
||||||
var path = $"removedroles_{id}.json";
|
var path = $"removedroles_{id}.json";
|
||||||
|
|
||||||
|
@ -126,7 +126,7 @@ public static class Boyfriend {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SocketGuild FindGuild(ulong channel) {
|
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) {
|
foreach (var guild in Client.Guilds) {
|
||||||
// ReSharper disable once LoopCanBeConvertedToQuery
|
// ReSharper disable once LoopCanBeConvertedToQuery
|
||||||
foreach (var x in guild.Channels)
|
foreach (var x in guild.Channels)
|
||||||
|
|
|
@ -26,8 +26,8 @@ public sealed class MuteCommand : ICommand {
|
||||||
|
|
||||||
var rolesRemoved = Boyfriend.GetRemovedRoles(cmd.Context.Guild.Id);
|
var rolesRemoved = Boyfriend.GetRemovedRoles(cmd.Context.Guild.Id);
|
||||||
|
|
||||||
if (rolesRemoved.ContainsKey(toMute.Id)) {
|
if (rolesRemoved.TryGetValue(toMute.Id, out var mutedRemovedRoles)) {
|
||||||
foreach (var roleId in rolesRemoved[toMute.Id]) await toMute.AddRoleAsync(roleId);
|
foreach (var roleId in mutedRemovedRoles) await toMute.AddRoleAsync(roleId);
|
||||||
rolesRemoved.Remove(toMute.Id);
|
rolesRemoved.Remove(toMute.Id);
|
||||||
cmd.ConfigWriteScheduled = true;
|
cmd.ConfigWriteScheduled = true;
|
||||||
cmd.Reply(Messages.RolesReturned, ":warning: ");
|
cmd.Reply(Messages.RolesReturned, ":warning: ");
|
||||||
|
|
|
@ -23,8 +23,8 @@ public sealed class UnmuteCommand : ICommand {
|
||||||
if (role != null && toUnmute.Roles.Contains(role)) {
|
if (role != null && toUnmute.Roles.Contains(role)) {
|
||||||
var rolesRemoved = Boyfriend.GetRemovedRoles(cmd.Context.Guild.Id);
|
var rolesRemoved = Boyfriend.GetRemovedRoles(cmd.Context.Guild.Id);
|
||||||
|
|
||||||
if (rolesRemoved.ContainsKey(toUnmute.Id)) {
|
if (rolesRemoved.TryGetValue(toUnmute.Id, out var unmutedRemovedRoles)) {
|
||||||
await toUnmute.AddRolesAsync(rolesRemoved[toUnmute.Id]);
|
await toUnmute.AddRolesAsync(unmutedRemovedRoles);
|
||||||
rolesRemoved.Remove(toUnmute.Id);
|
rolesRemoved.Remove(toUnmute.Id);
|
||||||
cmd.ConfigWriteScheduled = true;
|
cmd.ConfigWriteScheduled = true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,7 +65,7 @@ public static class Utils {
|
||||||
|
|
||||||
public static SocketRole? GetMuteRole(SocketGuild guild) {
|
public static SocketRole? GetMuteRole(SocketGuild guild) {
|
||||||
var id = ulong.Parse(Boyfriend.GetGuildConfig(guild.Id)["MuteRole"]);
|
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;
|
SocketRole? role = null;
|
||||||
foreach (var x in guild.Roles) {
|
foreach (var x in guild.Roles) {
|
||||||
if (x.Id != id) continue;
|
if (x.Id != id) continue;
|
||||||
|
@ -97,7 +97,7 @@ public static class Utils {
|
||||||
public static string GetMessage(string name) {
|
public static string GetMessage(string name) {
|
||||||
var propertyName = name;
|
var propertyName = name;
|
||||||
name = $"{Messages.Culture}/{name}";
|
name = $"{Messages.Culture}/{name}";
|
||||||
if (ReflectionMessageCache.ContainsKey(name)) return ReflectionMessageCache[name];
|
if (ReflectionMessageCache.TryGetValue(name, out var cachedMessage)) return cachedMessage;
|
||||||
|
|
||||||
var toReturn =
|
var toReturn =
|
||||||
typeof(Messages).GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Static)?.GetValue(null)
|
typeof(Messages).GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Static)?.GetValue(null)
|
||||||
|
|
Loading…
Reference in a new issue