Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cleanup] [p]cleanup bot includes aliases and custom commands #2213

Merged
merged 1 commit into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions redbot/cogs/cleanup/cleanup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re
from datetime import datetime, timedelta
from typing import Union, List, Callable
from typing import Union, List, Callable, Set

import discord

Expand Down Expand Up @@ -323,15 +323,35 @@ async def cleanup_bot(self, ctx: commands.Context, number: int, delete_pinned: b
if "" in prefixes:
prefixes.remove("")

cc_cog = self.bot.get_cog("CustomCommands")
if cc_cog is not None:
command_names: Set[str] = await cc_cog.get_command_names(ctx.guild)
is_cc = lambda name: name in command_names
else:
is_cc = lambda name: False
alias_cog = self.bot.get_cog("Alias")
if alias_cog is not None:
alias_names: Set[str] = (
set((a.name for a in await alias_cog.unloaded_global_aliases()))
| set(a.name for a in await alias_cog.unloaded_aliases(ctx.guild))
)
is_alias = lambda name: name in alias_names
else:
is_alias = lambda name: False

bot_id = self.bot.user.id

def check(m):
if m.author.id == self.bot.user.id:
if m.author.id == bot_id:
return True
elif m == ctx.message:
return True
p = discord.utils.find(m.content.startswith, prefixes)
if p and len(p) > 0:
cmd_name = m.content[len(p) :].split(" ")[0]
return bool(self.bot.get_command(cmd_name))
return (
bool(self.bot.get_command(cmd_name)) or is_alias(cmd_name) or is_cc(cmd_name)
)
return False

to_delete = await self.get_messages_for_deletion(
Expand Down
13 changes: 12 additions & 1 deletion redbot/cogs/customcom/customcom.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from datetime import datetime, timedelta
from inspect import Parameter
from collections import OrderedDict
from typing import Mapping, Tuple, Dict
from typing import Mapping, Tuple, Dict, Set

import discord

Expand Down Expand Up @@ -553,3 +553,14 @@ def transform_parameter(result, message) -> str:
else:
return raw_result
return str(getattr(first, second, raw_result))

async def get_command_names(self, guild: discord.Guild) -> Set[str]:
"""Get all custom command names in a guild.

Returns
--------
Set[str]
A set of all custom command names.

"""
return set(await CommandObj.get_commands(self.config.guild(guild)))