mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 03:08:55 -05:00
[Permissions] Fix ValueError for "default" rule in config/ACL (#2200)
This was thrown when the "default" key existed and Permissions tried to iterate over the list mapping keys as ints. Also fixed some issues with saving config with keys as `int` instead of `str`. Signed-off-by: Toby Harradine <tobyharradine@gmail.com>
This commit is contained in:
parent
9e13ca45e6
commit
9217275908
@ -542,7 +542,8 @@ class Permissions(commands.Cog):
|
|||||||
continue
|
continue
|
||||||
conf = self.config.custom(category)
|
conf = self.config.custom(category)
|
||||||
for cmd_name, cmd_rules in rules_dict.items():
|
for cmd_name, cmd_rules in rules_dict.items():
|
||||||
await conf.set_raw(cmd_name, guild_id, value=cmd_rules)
|
cmd_rules = {str(model_id): rule for model_id, rule in cmd_rules.items()}
|
||||||
|
await conf.set_raw(cmd_name, str(guild_id), value=cmd_rules)
|
||||||
cmd_obj = getter(cmd_name)
|
cmd_obj = getter(cmd_name)
|
||||||
if cmd_obj is not None:
|
if cmd_obj is not None:
|
||||||
self._load_rules_for(cmd_obj, {guild_id: cmd_rules})
|
self._load_rules_for(cmd_obj, {guild_id: cmd_rules})
|
||||||
@ -651,14 +652,14 @@ class Permissions(commands.Cog):
|
|||||||
if category in old_rules:
|
if category in old_rules:
|
||||||
for name, rules in old_rules[category].items():
|
for name, rules in old_rules[category].items():
|
||||||
these_rules = new_rules.setdefault(name, {})
|
these_rules = new_rules.setdefault(name, {})
|
||||||
guild_rules = these_rules.setdefault(guild_id, {})
|
guild_rules = these_rules.setdefault(str(guild_id), {})
|
||||||
# Since allow rules would take precedence if the same model ID
|
# Since allow rules would take precedence if the same model ID
|
||||||
# sat in both the allow and deny list, we add the deny entries
|
# sat in both the allow and deny list, we add the deny entries
|
||||||
# first and let any conflicting allow entries overwrite.
|
# first and let any conflicting allow entries overwrite.
|
||||||
for model_id in rules.get("deny", []):
|
for model_id in rules.get("deny", []):
|
||||||
guild_rules[model_id] = False
|
guild_rules[str(model_id)] = False
|
||||||
for model_id in rules.get("allow", []):
|
for model_id in rules.get("allow", []):
|
||||||
guild_rules[model_id] = True
|
guild_rules[str(model_id)] = True
|
||||||
if "default" in rules:
|
if "default" in rules:
|
||||||
default = rules["default"]
|
default = rules["default"]
|
||||||
if default == "allow":
|
if default == "allow":
|
||||||
@ -689,7 +690,9 @@ class Permissions(commands.Cog):
|
|||||||
"""
|
"""
|
||||||
for guild_id, guild_dict in _int_key_map(rule_dict.items()):
|
for guild_id, guild_dict in _int_key_map(rule_dict.items()):
|
||||||
for model_id, rule in _int_key_map(guild_dict.items()):
|
for model_id, rule in _int_key_map(guild_dict.items()):
|
||||||
if rule is True:
|
if model_id == "default":
|
||||||
|
cog_or_command.set_default_rule(rule, guild_id=guild_id)
|
||||||
|
elif rule is True:
|
||||||
cog_or_command.allow_for(model_id, guild_id=guild_id)
|
cog_or_command.allow_for(model_id, guild_id=guild_id)
|
||||||
elif rule is False:
|
elif rule is False:
|
||||||
cog_or_command.deny_to(model_id, guild_id=guild_id)
|
cog_or_command.deny_to(model_id, guild_id=guild_id)
|
||||||
@ -724,9 +727,16 @@ class Permissions(commands.Cog):
|
|||||||
rules.
|
rules.
|
||||||
"""
|
"""
|
||||||
for guild_id, guild_dict in _int_key_map(rule_dict.items()):
|
for guild_id, guild_dict in _int_key_map(rule_dict.items()):
|
||||||
for model_id in map(int, guild_dict.keys()):
|
for model_id in guild_dict.keys():
|
||||||
cog_or_command.clear_rule_for(model_id, guild_id)
|
if model_id == "default":
|
||||||
|
cog_or_command.set_default_rule(None, guild_id=guild_id)
|
||||||
|
else:
|
||||||
|
cog_or_command.clear_rule_for(int(model_id), guild_id=guild_id)
|
||||||
|
|
||||||
|
|
||||||
def _int_key_map(items_view: ItemsView[str, Any]) -> Iterator[Tuple[int, Any]]:
|
def _int_key_map(items_view: ItemsView[str, Any]) -> Iterator[Tuple[Union[str, int], Any]]:
|
||||||
return map(lambda tup: (int(tup[0]), tup[1]), items_view)
|
for k, v in items_view:
|
||||||
|
if k == "default":
|
||||||
|
yield k, v
|
||||||
|
else:
|
||||||
|
yield int(k), v
|
||||||
|
|||||||
@ -3,7 +3,7 @@ from redbot.cogs.permissions.permissions import Permissions, GLOBAL
|
|||||||
|
|
||||||
def test_schema_update():
|
def test_schema_update():
|
||||||
old = {
|
old = {
|
||||||
GLOBAL: {
|
str(GLOBAL): {
|
||||||
"owner_models": {
|
"owner_models": {
|
||||||
"cogs": {
|
"cogs": {
|
||||||
"Admin": {"allow": [78631113035100160], "deny": [96733288462286848]},
|
"Admin": {"allow": [78631113035100160], "deny": [96733288462286848]},
|
||||||
@ -19,7 +19,7 @@ def test_schema_update():
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
43733288462286848: {
|
"43733288462286848": {
|
||||||
"owner_models": {
|
"owner_models": {
|
||||||
"cogs": {
|
"cogs": {
|
||||||
"Admin": {
|
"Admin": {
|
||||||
@ -43,22 +43,22 @@ def test_schema_update():
|
|||||||
assert new == (
|
assert new == (
|
||||||
{
|
{
|
||||||
"Admin": {
|
"Admin": {
|
||||||
GLOBAL: {78631113035100160: True, 96733288462286848: False},
|
str(GLOBAL): {"78631113035100160": True, "96733288462286848": False},
|
||||||
43733288462286848: {24231113035100160: True, 35533288462286848: False},
|
"43733288462286848": {"24231113035100160": True, "35533288462286848": False},
|
||||||
},
|
},
|
||||||
"Audio": {GLOBAL: {133049272517001216: True, "default": False}},
|
"Audio": {str(GLOBAL): {"133049272517001216": True, "default": False}},
|
||||||
"General": {43733288462286848: {133049272517001216: True, "default": False}},
|
"General": {"43733288462286848": {"133049272517001216": True, "default": False}},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cleanup bot": {
|
"cleanup bot": {
|
||||||
GLOBAL: {78631113035100160: True, "default": False},
|
str(GLOBAL): {"78631113035100160": True, "default": False},
|
||||||
43733288462286848: {17831113035100160: True, "default": True},
|
"43733288462286848": {"17831113035100160": True, "default": True},
|
||||||
},
|
},
|
||||||
"ping": {GLOBAL: {96733288462286848: True, "default": True}},
|
"ping": {str(GLOBAL): {"96733288462286848": True, "default": True}},
|
||||||
"set adminrole": {
|
"set adminrole": {
|
||||||
43733288462286848: {
|
"43733288462286848": {
|
||||||
87733288462286848: True,
|
"87733288462286848": True,
|
||||||
95433288462286848: False,
|
"95433288462286848": False,
|
||||||
"default": True,
|
"default": True,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user