Repository: Cog-Creators/Red-DiscordBot

Issue:
[permissions] Clearing rules, doesn't allow command to be used
# Command bugs

<!-- 
Did you find a bug with a command? Fill out the following:
-->

#### Command name

clearglobalrules or clearserverrules after setting a default server or default global rule

#### What cog is this command from?

permissions

#### What were you expecting to happen?

After clearing the rules for the command to work as intended

#### What actually happened?

Command was still denied as if the permissions were in place

#### How can we reproduce this issue?

Deny a command with permissions such as ping with [p]permissions setdefaultglobalrule deny ping

Then clear the rules with clearglobalrules

Try and have a user use the denied command, after the permissions were cleared and it will still be blocked, after a bot restart it will become available.

Only seems to occur if you use setdefaultglobalrule or setdefaultserverrule when setting the permissions as a normal server or global rule clears and allows the command to work,

Might just want to send a message after clearing rules to reboot the bot, or there could be another way of handling the clearing of permissions



Discussion:

Resolving patch:
diff --git a/redbot/cogs/permissions/permissions.py b/redbot/cogs/permissions/permissions.py
--- a/redbot/cogs/permissions/permissions.py
+++ b/redbot/cogs/permissions/permissions.py
@@ -544,7 +544,7 @@ async def _clear_rules(self, guild_id: int) -> None:
 
         Handles config.
         """
-        self.bot.clear_permission_rules(guild_id)
+        self.bot.clear_permission_rules(guild_id, preserve_default_rule=False)
         for category in (COG, COMMAND):
             async with self.config.custom(category).all() as all_rules:
                 for name, rules in all_rules.items():
diff --git a/redbot/core/bot.py b/redbot/core/bot.py
--- a/redbot/core/bot.py
+++ b/redbot/core/bot.py
@@ -770,7 +770,7 @@ def remove_command(self, name: str) -> None:
             for subcommand in set(command.walk_commands()):
                 subcommand.requires.reset()
 
-    def clear_permission_rules(self, guild_id: Optional[int]) -> None:
+    def clear_permission_rules(self, guild_id: Optional[int], **kwargs) -> None:
         """Clear all permission overrides in a scope.
 
         Parameters
@@ -780,11 +780,15 @@ def clear_permission_rules(self, guild_id: Optional[int]) -> None:
             ``None``, this will clear all global rules and leave all
             guild rules untouched.
 
+        **kwargs
+            Keyword arguments to be passed to each required call of
+            ``commands.Requires.clear_all_rules``
+
         """
         for cog in self.cogs.values():
-            cog.requires.clear_all_rules(guild_id)
+            cog.requires.clear_all_rules(guild_id, **kwargs)
         for command in self.walk_commands():
-            command.requires.clear_all_rules(guild_id)
+            command.requires.clear_all_rules(guild_id, **kwargs)
 
     def add_permissions_hook(self, hook: commands.CheckPredicate) -> None:
         """Add a permissions hook.
diff --git a/redbot/core/commands/requires.py b/redbot/core/commands/requires.py
--- a/redbot/core/commands/requires.py
+++ b/redbot/core/commands/requires.py
@@ -398,11 +398,9 @@ class for an explanation.
         else:
             rules[model_id] = rule
 
-    def clear_all_rules(self, guild_id: int) -> None:
+    def clear_all_rules(self, guild_id: int, *, preserve_default_rule: bool = True) -> None:
         """Clear all rules of a particular scope.
 
-        This will preserve the default rule, if set.
-
         Parameters
         ----------
         guild_id : int
@@ -410,6 +408,12 @@ def clear_all_rules(self, guild_id: int) -> None:
             `Requires.GLOBAL`, this will clear all global rules and
             leave all guild rules untouched.
 
+        Other Parameters
+        ----------------
+        preserve_default_rule : bool
+            Whether to preserve the default rule or not.
+            This defaults to being preserved
+
         """
         if guild_id:
             rules = self._guild_rules.setdefault(guild_id, _RulesDict())
@@ -417,7 +421,7 @@ def clear_all_rules(self, guild_id: int) -> None:
             rules = self._global_rules
         default = rules.get(self.DEFAULT, None)
         rules.clear()
-        if default is not None:
+        if default is not None and preserve_default_rule:
             rules[self.DEFAULT] = default
 
     def reset(self) -> None:


Test patch: