Skip to content

Commit

Permalink
Add auto react module (#614)
Browse files Browse the repository at this point in the history
This adds a module that allows the bot to react based on keywords or phrases defined in the config
  • Loading branch information
ajax146 authored Sep 1, 2023
1 parent 4f3cd5b commit 2421d0c
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions techsupport_bot/extensions/autoreact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""Module for the autoreact extension for the discord bot."""
import base
from base import auxiliary


async def setup(bot):
"""Adding the autoreact extension to the config file to get info."""
config = bot.ExtensionConfig()
config.add(
key="react_map",
datatype="dict",
title="Mapping of phrases",
description="Lowercase phrase to reaction wanted",
default={"hello": "👋"},
)

await bot.add_cog(AutoReact(bot=bot, extension_name="autoreact"))
bot.add_extension_config("autoreact", config)


class AutoReact(base.MatchCog):
"""Class for the autoreact to make it to discord."""

async def match(self, config, _, content):
"""A match function to determine if somehting should be reacted to
Args:
config (munch.Munch): The guild config for the running bot
_ (commands.Context): The context in which the message was sent in
content (str): The string content of the message
Returns:
bool: True if there needs to be a reaction, False otherwise
"""
search_content = f" {content} "
search_content = search_content.lower()
for word in config.extensions.autoreact.react_map.value:
if f" {word.lower()} " in search_content:
return True
return False

async def response(self, config, ctx, content, _):
"""The function to generate and add reactions
Args:
config (munch.Munch): The guild config for the running bot
ctx (commands.Context): The context in which the message was sent in
content (str): The string content of the message
_ (bool): The result from the match function
"""
search_content = f" {content} "
search_content = search_content.lower()
reactions = []
for word in config.extensions.autoreact.react_map.value:
if f" {word.lower()} " in search_content:
reaction = config.extensions.autoreact.react_map.value.get(word)
if reaction not in reactions:
reactions.append(
config.extensions.autoreact.react_map.value.get(word)
)
await auxiliary.add_list_of_reactions(message=ctx.message, reactions=reactions)

0 comments on commit 2421d0c

Please sign in to comment.