From e1d8d2e38a8f140b2408b25d0a67474c36fca999 Mon Sep 17 00:00:00 2001 From: Wituareard <87208350+Wituareard@users.noreply.github.com> Date: Fri, 5 Jul 2024 01:54:40 +0200 Subject: [PATCH] Remove role "Wants to help" when onboarded --- config.py | 4 ++++ config.yml | 5 ++++- onboarding/onboarding_config.py | 21 +++++++++++++++++++++ onboarding/onboarding_manager.py | 14 ++++++++++---- onboarding/utils.py | 10 +++++++++- 5 files changed, 48 insertions(+), 6 deletions(-) diff --git a/config.py b/config.py index 5934033..0c7b213 100644 --- a/config.py +++ b/config.py @@ -81,5 +81,9 @@ def airtable_token(self) -> str: @property def slow_repeat_interval(self) -> int: return self.get("slow_repeat_interval") + + @property + def wants_to_help_role_id(self) -> int: + return self.get("wants_to_help_role_id") CONFIG = Config("config.yml", "secrets.yml") \ No newline at end of file diff --git a/config.yml b/config.yml index bb56e1e..b52c1f8 100644 --- a/config.yml +++ b/config.yml @@ -26,4 +26,7 @@ admin_user_ids: - 232972493957103618 # Pato - 346706777305251840 # Chris G staging: - - 252771041464680449 # Maxime F \ No newline at end of file + - 252771041464680449 # Maxime F +wants_to_help_role_id: + prod: 1247238509064880218 + staging: 0 \ No newline at end of file diff --git a/onboarding/onboarding_config.py b/onboarding/onboarding_config.py index a2b5189..9414574 100644 --- a/onboarding/onboarding_config.py +++ b/onboarding/onboarding_config.py @@ -1,5 +1,6 @@ from messages.m_onboarding import * from airtable_client import TABLES +from config import CONFIG # ------------- # STANDARD CONDITIONS @@ -336,6 +337,18 @@ { "type": "database set onboarder" }, + { + "type": "database update tick", + "condition": { + "type": "discord has role", + "role_id": CONFIG.wants_to_help_role_id + }, + "field_name": "Wants to help" + }, + { + "type": "discord remove role", + "role_id": CONFIG.wants_to_help_role_id + }, { "type": "message", "condition": DATABASE_WEBSITE, @@ -373,6 +386,14 @@ "type": "database update untick", "field_name": "Check In" }, + { + "type": "discord add role", + "condition": { + "type": "database check ticked", + "field_name": "Wants to help" + }, + "role_id": CONFIG.wants_to_help_role_id + }, { "type": "message", "message": M_UNDO_START_ONBOARDING diff --git a/onboarding/onboarding_manager.py b/onboarding/onboarding_manager.py index 5abfd4d..c42b232 100644 --- a/onboarding/onboarding_manager.py +++ b/onboarding/onboarding_manager.py @@ -2,7 +2,7 @@ import discord from airtable import Airtable -from onboarding.utils import reset_onboarder, reset_researcher, update_onboarder, update_researcher +from onboarding.utils import reset_onboarder, reset_researcher, update_onboarder, update_researcher, remove_role, add_role from onboarding.onboarding_config import CONFIG class ConditionException(Exception): @@ -102,7 +102,7 @@ async def command_trigger(self, command: str, subcommand: str, user: discord.Use signature = self.trigger_signature_from_command(command, subcommand) await self.handle_stage(signature, user, message, None, record, table, params) - def solve_condition(self, condition: Dict, record: Dict, user: discord.User, params: Dict) -> bool: + def solve_condition(self, condition: Dict, record: Dict, user: discord.User, message: discord.Message, params: Dict) -> bool: satisfied = False if condition["type"] == "database check ticked": satisfied = record["fields"].get(condition["field_name"], False) @@ -116,6 +116,8 @@ def solve_condition(self, condition: Dict, record: Dict, user: discord.User, par satisfied = record["fields"].get("Researcher Id", "") == str(user.id) elif condition["type"] == "database check is onboarder": satisfied = record["fields"].get("Onboarder Id", "") == str(user.id) + elif condition["type"] == "discord has role": + satisfied = not not message.author.get_role(condition["role_id"]) elif condition["type"] == "check param value": satisfied = params[condition["param_name"]] == condition["param_value"] else: @@ -135,7 +137,7 @@ async def handle_stage(self, signature: str, user: discord.User, message: discor for condition in stage.get("conditions", []): print(f"Condition: {condition['type']}") try: - satisfied = self.solve_condition(condition, record, user, params) + satisfied = self.solve_condition(condition, record, user, message, params) except ConditionException: print(f"Error: unknown condition type: {condition['type']}") return @@ -151,7 +153,7 @@ async def handle_stage(self, signature: str, user: discord.User, message: discor condition = action["condition"] print(f"Evaluating condition attached to action: {condition['type']}") try: - satisfied = self.solve_condition(condition, record, user, params) + satisfied = self.solve_condition(condition, record, user, message, params) except ConditionException: print(f"Error: unknown condition type: {condition['type']}") return @@ -181,6 +183,10 @@ async def handle_stage(self, signature: str, user: discord.User, message: discor table.update(record["id"], {action["field_name"]: params[action["param_name"]]}, typecast=True) elif action["type"] == "message": await user.send(format_message(action["message"], record, params)) + elif action["type"] == "discord remove role": + await remove_role(message.author, action["role_id"]) + elif action["type"] == "discord add role": + await add_role(message.author, action["role_id"]) else: print(f"Error: unknown action type: {action['type']}") return diff --git a/onboarding/utils.py b/onboarding/utils.py index 8b9b398..b7c099d 100644 --- a/onboarding/utils.py +++ b/onboarding/utils.py @@ -176,4 +176,12 @@ def format_message_discord(message: str, record: Dict, **kwargs) -> str: name=record["fields"].get("Newcomer Name"), user_id=record["fields"].get("Newcomer Id"), **kwargs - ) \ No newline at end of file + ) + +async def remove_role(member: discord.Member, role_id: int): + role = member.guild.get_role(role_id) + await member.remove_roles(role) + +async def add_role(member: discord.Member, role_id): + role = member.guild.get_role(role_id) + await member.add_roles(role) \ No newline at end of file