add on_message_handlers to Bot, by extending Bot
Bot is now extended as Hitomi, so that we can add on_message_handlers = [], which will store functions to call when on_message happens. this is necessary because i want to have two things react to on_message, but the discord.py code only supports one coroutine for on_message. so, this is a hook into it to do what i want not sure if i'll end up needing this for on_message_edit or on_message_delete, but i'm skeptical right now
This commit is contained in:
parent
50a5c3d94a
commit
6a5985b002
@ -4,4 +4,14 @@ from django.conf import settings
|
|||||||
|
|
||||||
BOT_DESCRIPTION = "A simple Discord bot."
|
BOT_DESCRIPTION = "A simple Discord bot."
|
||||||
|
|
||||||
hitomi = commands.Bot(command_prefix='!', description=BOT_DESCRIPTION, max_messages=settings.DISCORD_BOT_MAX_MESSAGES)
|
|
||||||
|
class Hitomi(commands.Bot):
|
||||||
|
"""Extend the discord.py Bot, to add more cool stuff."""
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
"""Initialize bot, and cool stuff."""
|
||||||
|
super(Hitomi, self).__init__(*args, **kwargs)
|
||||||
|
self.on_message_handlers = []
|
||||||
|
|
||||||
|
|
||||||
|
hitomi = Hitomi(command_prefix='!', description=BOT_DESCRIPTION, max_messages=settings.DISCORD_BOT_MAX_MESSAGES)
|
||||||
|
@ -17,6 +17,16 @@ async def on_ready():
|
|||||||
logger.info("Logged in as {0:s} ({1:s})".format(hitomi.user.name, hitomi.user.id))
|
logger.info("Logged in as {0:s} ({1:s})".format(hitomi.user.name, hitomi.user.id))
|
||||||
|
|
||||||
|
|
||||||
|
@hitomi.event
|
||||||
|
async def on_message(message):
|
||||||
|
"""Call all registered on_message handlers."""
|
||||||
|
for handler in hitomi.on_message_handlers:
|
||||||
|
handler(message)
|
||||||
|
|
||||||
|
# let other stuff happen
|
||||||
|
await hitomi.process_commands(message)
|
||||||
|
|
||||||
|
|
||||||
async def run_bot():
|
async def run_bot():
|
||||||
"""Initialize and begin the bot in an asyncio context."""
|
"""Initialize and begin the bot in an asyncio context."""
|
||||||
await hitomi.login(settings.DISCORD_BOT_TOKEN)
|
await hitomi.login(settings.DISCORD_BOT_TOKEN)
|
||||||
|
@ -9,8 +9,7 @@ from bot import hitomi
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@hitomi.event
|
def on_message(message):
|
||||||
async def on_message(message):
|
|
||||||
"""Log the seen message."""
|
"""Log the seen message."""
|
||||||
try:
|
try:
|
||||||
log_file = log_file_for_message(message)
|
log_file = log_file_for_message(message)
|
||||||
@ -23,8 +22,8 @@ async def on_message(message):
|
|||||||
logger.error("error in creating log line")
|
logger.error("error in creating log line")
|
||||||
logger.exception(ex)
|
logger.exception(ex)
|
||||||
|
|
||||||
# let other stuff happen
|
|
||||||
await hitomi.process_commands(message)
|
hitomi.on_message_handlers.append(on_message)
|
||||||
|
|
||||||
|
|
||||||
@hitomi.event
|
@hitomi.event
|
||||||
|
Loading…
x
Reference in New Issue
Block a user