mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 03:08:55 -05:00
[Core] Message modifiers (#477)
Message modifiers offer an easy way for cogs to edit outcoming messages without resorting to hacky methods
This commit is contained in:
parent
dd9fc4f7c2
commit
90272b378a
53
red.py
53
red.py
@ -46,8 +46,61 @@ class Bot(commands.Bot):
|
|||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
self.counter = Counter()
|
self.counter = Counter()
|
||||||
self.uptime = datetime.datetime.now()
|
self.uptime = datetime.datetime.now()
|
||||||
|
self._message_modifiers = []
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
async def send_message(self, *args, **kwargs):
|
||||||
|
if self._message_modifiers:
|
||||||
|
if "content" in kwargs:
|
||||||
|
pass
|
||||||
|
elif len(args) == 2:
|
||||||
|
args = list(args)
|
||||||
|
kwargs["content"] = args.pop()
|
||||||
|
else:
|
||||||
|
await super().send_message(*args, **kwargs)
|
||||||
|
return
|
||||||
|
|
||||||
|
content = kwargs['content']
|
||||||
|
for m in self._message_modifiers:
|
||||||
|
try:
|
||||||
|
content = str(m(content))
|
||||||
|
except: # Faulty modifiers should not
|
||||||
|
pass # break send_message
|
||||||
|
kwargs['content'] = content
|
||||||
|
|
||||||
|
await super().send_message(*args, **kwargs)
|
||||||
|
|
||||||
|
def add_message_modifier(self, func):
|
||||||
|
"""
|
||||||
|
Adds a message modifier to the bot
|
||||||
|
|
||||||
|
A message modifier is a callable that accepts a message's
|
||||||
|
content as the first positional argument.
|
||||||
|
Before a message gets sent, func will get called with
|
||||||
|
the message's content as the only argument. The message's
|
||||||
|
content will then be modified to be the func's return
|
||||||
|
value.
|
||||||
|
Exceptions thrown by the callable will be catched and
|
||||||
|
silenced.
|
||||||
|
"""
|
||||||
|
if not callable(func):
|
||||||
|
raise TypeError("The message modifier function "
|
||||||
|
"must be a callable.")
|
||||||
|
|
||||||
|
self._message_modifiers.append(func)
|
||||||
|
|
||||||
|
def remove_message_modifier(self, func):
|
||||||
|
"""Removes a message modifier from the bot"""
|
||||||
|
if func not in self._message_modifiers:
|
||||||
|
raise RuntimeError("Function not present in the message "
|
||||||
|
"modifiers.")
|
||||||
|
|
||||||
|
self._message_modifiers.remove(func)
|
||||||
|
|
||||||
|
def clear_message_modifiers(self):
|
||||||
|
"""Removes all message modifiers from the bot"""
|
||||||
|
self._message_modifiers.clear()
|
||||||
|
|
||||||
async def send_cmd_help(self, ctx):
|
async def send_cmd_help(self, ctx):
|
||||||
if ctx.invoked_subcommand:
|
if ctx.invoked_subcommand:
|
||||||
pages = bot.formatter.format_help_for(ctx, ctx.invoked_subcommand)
|
pages = bot.formatter.format_help_for(ctx, ctx.invoked_subcommand)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user