[Cleanup] [p]cleanup bot includes aliases and CCs (#2213)

Resolves #1920.

Signed-off-by: Toby Harradine <tobyharradine@gmail.com>
This commit is contained in:
Toby Harradine 2018-10-15 22:29:07 +11:00 committed by GitHub
parent 1ba922eba2
commit ad51fa830b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 4 deletions

View File

@ -1,6 +1,6 @@
import re import re
from datetime import datetime, timedelta from datetime import datetime, timedelta
from typing import Union, List, Callable from typing import Union, List, Callable, Set
import discord import discord
@ -323,15 +323,35 @@ class Cleanup(commands.Cog):
if "" in prefixes: if "" in prefixes:
prefixes.remove("") 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): def check(m):
if m.author.id == self.bot.user.id: if m.author.id == bot_id:
return True return True
elif m == ctx.message: elif m == ctx.message:
return True return True
p = discord.utils.find(m.content.startswith, prefixes) p = discord.utils.find(m.content.startswith, prefixes)
if p and len(p) > 0: if p and len(p) > 0:
cmd_name = m.content[len(p) :].split(" ")[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 return False
to_delete = await self.get_messages_for_deletion( to_delete = await self.get_messages_for_deletion(

View File

@ -3,7 +3,7 @@ import random
from datetime import datetime, timedelta from datetime import datetime, timedelta
from inspect import Parameter from inspect import Parameter
from collections import OrderedDict from collections import OrderedDict
from typing import Mapping, Tuple, Dict from typing import Mapping, Tuple, Dict, Set
import discord import discord
@ -553,3 +553,14 @@ class CustomCommands(commands.Cog):
else: else:
return raw_result return raw_result
return str(getattr(first, second, 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)))