Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auto react module #614

Merged
merged 7 commits into from
Sep 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading