Skip to content
Permalink
Browse files Browse the repository at this point in the history
[act] don't trigger on disabled cmds
  • Loading branch information
Zephyrkul committed Sep 9, 2020
1 parent cbc70ed commit 6b9f3b8
Showing 1 changed file with 44 additions and 28 deletions.
72 changes: 44 additions & 28 deletions act/act.py
@@ -1,16 +1,19 @@
import itertools
import functools
import random
import re
from typing import Union

import aiohttp
import discord
import inflection
from redbot.core import Config, checks, commands
from redbot.core import bot, Config, checks, commands
from redbot.core.i18n import get_locale
from redbot.core.utils.chat_formatting import italics

from .helpers import *

fmt_re = re.compile(r"{(?:0|user)(?:\.([^\{]+))?}")


class Act(commands.Cog):
"""
Expand All @@ -26,24 +29,36 @@ async def red_get_data_for_user(self, *, user_id):
async def red_delete_data_for_user(self, *, requester, user_id):
pass # No data to delete

def __init__(self, bot):
def __init__(self, bot: bot.Red):
super().__init__()
self.bot = bot
self.config = Config.get_conf(self, identifier=2_113_674_295, force_registration=True)
self.config.register_global(custom={}, tenorkey=None)
self.config.register_guild(custom={})
self.try_after = None

async def initialize(self, bot):
async def initialize(self, bot: bot.Red):
# temporary backwards compatibility
key = await self.config.tenorkey()
if not key:
return
await bot.set_shared_api_tokens("tenor", api_key=key)
await self.config.tenorkey.clear()

@staticmethod
def repl(target: discord.Member, match: re.Match):
if attr := match.group(1):
print(attr)
if attr.startswith("_") or "." in attr:
return str(target)
try:
return str(getattr(target, attr))
except AttributeError:
return str(target)
return str(target)

@commands.command(hidden=True)
async def act(self, ctx, *, target: Union[discord.Member, str] = None):
async def act(self, ctx: commands.Context, *, target: Union[discord.Member, str] = None):
"""
Acts on the specified user.
"""
Expand Down Expand Up @@ -87,7 +102,8 @@ async def act(self, ctx, *, target: Union[discord.Member, str] = None):
action.insert(iverb + 1, target.mention)
message = italics(" ".join(action))
else:
message = message.format(target, user=target)
assert isinstance(message, str)
message = fmt_re.sub(functools.partial(self.repl, target), message)

# add reaction gif
if self.try_after and ctx.message.created_at < self.try_after:
Expand Down Expand Up @@ -131,15 +147,15 @@ async def act(self, ctx, *, target: Union[discord.Member, str] = None):

@commands.group()
@checks.is_owner()
async def actset(self, ctx):
async def actset(self, ctx: commands.Context):
"""
Configure various settings for the act cog.
"""

@actset.group(aliases=["custom"], invoke_without_command=True)
@checks.admin_or_permissions(manage_guild=True)
@commands.guild_only()
async def customize(self, ctx, command: str.lower, *, response: str = None):
async def customize(self, ctx: commands.Context, command: str.lower, *, response: str = None):
"""
Customize the response to an action.
Expand All @@ -148,13 +164,19 @@ async def customize(self, ctx, command: str.lower, *, response: str = None):
"""
if not response:
await self.config.guild(ctx.guild).clear_raw("custom", command)
await ctx.tick()
else:
await self.config.guild(ctx.guild).set_raw("custom", command, value=response)
await ctx.tick()
await ctx.send(
fmt_re.sub(functools.partial(self.repl, ctx.author), response),
allowed_mentions=discord.AllowedMentions(users=False),
)

@customize.command(name="global")
@checks.is_owner()
async def customize_global(self, ctx, command: str.lower, *, response: str = None):
async def customize_global(
self, ctx: commands.Context, command: str.lower, *, response: str = None
):
"""
Globally customize the response to an action.
Expand All @@ -170,7 +192,7 @@ async def customize_global(self, ctx, command: str.lower, *, response: str = Non
@actset.group(invoke_without_command=True)
@checks.admin_or_permissions(manage_guild=True)
@commands.guild_only()
async def ignore(self, ctx, command: str.lower):
async def ignore(self, ctx: commands.Context, command: str.lower):
"""
Ignore or unignore the specified action.
Expand All @@ -189,7 +211,7 @@ async def ignore(self, ctx, command: str.lower):

@ignore.command(name="global")
@checks.is_owner()
async def ignore_global(self, ctx, command: str.lower):
async def ignore_global(self, ctx: commands.Context, command: str.lower):
"""
Globally ignore or unignore the specified action.
Expand All @@ -205,7 +227,7 @@ async def ignore_global(self, ctx, command: str.lower):

@actset.command()
@checks.is_owner()
async def tenorkey(self, ctx):
async def tenorkey(self, ctx: commands.Context):
"""
Sets a Tenor GIF API key to enable reaction gifs with act commands.
Expand All @@ -225,20 +247,14 @@ async def tenorkey(self, ctx):
await ctx.maybe_send_embed("\n".join(instructions))

@commands.Cog.listener()
async def on_message(self, message):
if message.author.bot:
async def on_command_error(
self, ctx: commands.Context, error: commands.CommandError, unhandled_by_cog: bool = False
):
if ctx.command == self.act:
return

ctx = await self.bot.get_context(message)
if ctx.prefix is None or not ctx.invoked_with.replace("_", "").isalpha():
if isinstance(error, commands.UserFeedbackCheckFailure):
# UserFeedbackCheckFailure inherits from CheckFailure
return

if ctx.valid and ctx.command.enabled:
try:
if await ctx.command.can_run(ctx):
return
except commands.errors.CheckFailure:
return

ctx.command = self.act
await self.bot.invoke(ctx)
elif isinstance(error, (commands.CheckFailure, commands.CommandNotFound)):
ctx.command = self.act
await ctx.bot.invoke(ctx)

0 comments on commit 6b9f3b8

Please sign in to comment.