From 3a2ceffdfdbf600c77d008bca02b595d4db3b7c5 Mon Sep 17 00:00:00 2001 From: howardt12345 Date: Wed, 18 Sep 2024 22:14:27 -0400 Subject: [PATCH 1/5] feat: Reacting the reactions in order --- functions/reacts-README.md | 1 + functions/reacts.py | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/functions/reacts-README.md b/functions/reacts-README.md index 947d694..9ff2374 100644 --- a/functions/reacts-README.md +++ b/functions/reacts-README.md @@ -25,6 +25,7 @@ The JSON file should have the following structure and can have the following fie "content": str | list[str], "max_limit": int (optional), "react_with_all": boolean (optional), + "react_in_order": boolean (optional) } ], "possible_replies": [ diff --git a/functions/reacts.py b/functions/reacts.py index 9d73fd9..a374b79 100644 --- a/functions/reacts.py +++ b/functions/reacts.py @@ -7,6 +7,7 @@ from discord import Message from random import randint, sample import ast +from time import sleep from modules.probability import mock_bernoulli @@ -52,6 +53,8 @@ class ReactEvent(BaseModel): class ReactEventReaction(ReactEvent): # Whether all of the possible reactions should be added to the message. react_with_all: bool = False + # Whether to react the reactions in order + react_in_order: bool = False class ReactEventReply(ReactEvent): @@ -130,17 +133,27 @@ async def react_to_message( ) -> bool: reaction_happened = False for possible_reaction in possible_reactions: + sleep(0.05) # If the matched linked results exists check if the reaction is linked to a match if not evaluate_event_condition(possible_reaction.condition, criteria_links): continue # List of reactions in random order reactions_list = ( - sample(possible_reaction.content, len(possible_reaction.content)) - if isinstance(possible_reaction.content, list) - else [possible_reaction.content] + ( + sample(possible_reaction.content, len(possible_reaction.content)) + if isinstance(possible_reaction.content, list) + else [possible_reaction.content] + ) + if not possible_reaction.react_in_order + else ( + possible_reaction.content + if isinstance(possible_reaction.content, list) + else [possible_reaction.content] + ) ) reaction_count = 0 for reaction in reactions_list: + sleep(0.001) if possible_reaction.react_with_all: # If all of the possible reactions should be added if await add_reaction(message, reaction): reaction_happened = True @@ -169,6 +182,7 @@ async def reply_to_message( ) -> bool: reply_happened = False for possible_reply in possible_replies: + sleep(0.05) # If the matched linked results exists check if the reply is linked to a match if not evaluate_event_condition(possible_reply.condition, criteria_links): continue @@ -177,6 +191,7 @@ async def reply_to_message( if isinstance(possible_reply.content, list): reply_count = 0 for reply in possible_reply.content: + sleep(0.001) if possible_reply.max_limit > 0 and reply_count >= possible_reply.max_limit: break if await send_message( From 0e380eadf99916b5d0aa6b71a14553fb33537a0e Mon Sep 17 00:00:00 2001 From: howardt12345 Date: Wed, 18 Sep 2024 22:41:06 -0400 Subject: [PATCH 2/5] feat: Taiwan #1 --- resources/reacts/tw.json | 20 ++++++++ tests/test_reacts/test_reacts_tw.py | 77 +++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/resources/reacts/tw.json b/resources/reacts/tw.json index 6fb1113..37f4f47 100644 --- a/resources/reacts/tw.json +++ b/resources/reacts/tw.json @@ -54,6 +54,15 @@ "嘉義", "美麗島" ] + }, + { + "match_whole_word": true, + "keywords": [ + "(?:台|臺)灣(?:[\\s-]*(?:is|be|are|=|:|\\bis\\b|\\bare\\b))?[\\s-]*(?:(?:number|#|n[o°]|n°)\\s*(?:1|one|won|一|壹|①)|(?:first|1st|best|top|greatest|最(?:棒|強|好)|第一))", + "Taiwan(?:[\\s-]*(?:is|be|are|=|:|\\bis\\b|\\bare\\b))?[\\s-]*(?:(?:number|#|n[o°]|n°)\\s*(?:1|one|won|①)|(?:first|1st|best|top|greatest))", + "Taiwán(?:[\\s-]*(?:is|be|are|=|:|\\bis\\b|\\bare\\b))?[\\s-]*(?:(?:number|#|n[o°]|n°)\\s*(?:1|one|won|①)|(?:first|1st|best|top|greatest))" + ], + "criteria_link": "tw_no_1" } ], "possible_reactions": [ @@ -85,6 +94,17 @@ "<:PineappleCake:1156373382565212323>", "<:StinkyTofu:1156373383643152445>" ] + }, + { + "condition": "tw_no_1", + "chance": 1, + "content": [ + "<:tw_heart:1133045893227102299>", + "#️⃣", + "1️⃣" + ], + "react_with_all": true, + "react_in_order": true } ], "possible_replies": [ diff --git a/tests/test_reacts/test_reacts_tw.py b/tests/test_reacts/test_reacts_tw.py index baadb01..781461b 100644 --- a/tests/test_reacts/test_reacts_tw.py +++ b/tests/test_reacts/test_reacts_tw.py @@ -57,6 +57,53 @@ "美麗島", ) +TEST_CASES_TW_NO_1_TRUE = [ + "Taiwan #1", + "Taiwan number 1", + "Taiwan is number one", + "Taiwan Number One", + "Taiwan is #1", + "Taiwan No. 1", + "Taiwan n° 1", + "Taiwán número uno", + "臺灣第一", + "台灣最棒", + "Taiwan is the best", + "Taiwan = first", + "Taiwan: top", + "Taiwan are greatest", + "Taiwan - number 1", + "Taiwan # 1", + "臺灣第一名", + "台灣最強", + "台灣是第一", + "Taiwan 第一", + "臺灣 number one", + "Taiwan won", + "Taiwan ①", + "Taiwan 壹", + "TAIWAN NUMBER ONE", + "taiwan #1", + "I believe Taiwan is truly number one in Asia", + "Taiwan, often called Formosa, is number 1 in my heart" +] + +TEST_CASES_TW_NO_1_FALSE = [ + "Thailand #1", + "Taiwan is great", + "Number 1 Taiwan", + "Taiwan number 2", + "Taiwan was number 1", + "Taiwanese number 1", + "Taiwan numbers", + "Taiwan", + "#1", + "Number one", + "Taiwan #", + "Taiwan number" +] + + ALL_TEST_CASES = TEST_CASE_EN + TEST_CASE_TW @@ -100,3 +147,33 @@ def test_react_tw_regex_no_match(test_str: str): """Tests that these strings return FALSE.""" # * surrounded by text assert not check_resource_match(f"a{test_str}b", resource_name='tw') + +@pytest.mark.parametrize( + "test_str", + TEST_CASES_TW_NO_1_TRUE, +) +def test_react_tw_no_1_regex_yes_match(test_str: str): + """Tests that these strings return TRUE.""" + # * isolated string + assert check_resource_match(test_str, resource_name='tw') + # * surrounded by spaces + assert check_resource_match(f" {test_str} ", resource_name='tw') + # * to lowercase + assert check_resource_match(test_str.lower(), resource_name='tw') + # * to title case + assert check_resource_match(test_str.title(), resource_name='tw') + +@pytest.mark.parametrize( + "test_str", + TEST_CASES_TW_NO_1_FALSE, +) +def test_react_tw_no_1_regex_no_match(test_str: str): + """Tests that these strings return FALSE.""" + # * isolated string + assert not check_resource_match(test_str, resource_name='tw') + # * surrounded by spaces + assert not check_resource_match(f" {test_str} ", resource_name='tw') + # * to lowercase + assert not check_resource_match(test_str.lower(), resource_name='tw') + # * to title case + assert not check_resource_match(test_str.title(), resource_name='tw') \ No newline at end of file From f74b974d6306472bf4eef64ea7f4801cbbe8b8a4 Mon Sep 17 00:00:00 2001 From: Howard Tseng Date: Wed, 18 Sep 2024 23:02:29 -0400 Subject: [PATCH 3/5] testing on specific criteria --- functions/reacts.py | 10 ++++++--- resources/reacts/tw.json | 6 +++--- tests/test_reacts/test_reacts_tw.py | 32 ++++++++++++++--------------- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/functions/reacts.py b/functions/reacts.py index a374b79..0e4b320 100644 --- a/functions/reacts.py +++ b/functions/reacts.py @@ -95,10 +95,14 @@ def load_react_resources() -> dict[str, ReactResource]: # For testing in pytest to test a specific resource -def check_resource_match(message_content: str, resource_name: str) -> bool: +def check_resource_match(message_content: str, resource_name: str, criteria_link: str | None = None) -> bool: resource = REACT_RESOURCES[resource_name] - matches = check_matches(message_content, resource.criteria) - return bool(matches) + if criteria_link: + for criteria in resource.criteria: + if criteria.criteria_link == criteria_link: + return bool(check_matches(message_content, [criteria])) + else: + return bool(check_matches(message_content, resource.criteria)) def regex_search(message_content: str, possible_match: ReactCriteria) -> bool: diff --git a/resources/reacts/tw.json b/resources/reacts/tw.json index 37f4f47..50afc76 100644 --- a/resources/reacts/tw.json +++ b/resources/reacts/tw.json @@ -58,9 +58,9 @@ { "match_whole_word": true, "keywords": [ - "(?:台|臺)灣(?:[\\s-]*(?:is|be|are|=|:|\\bis\\b|\\bare\\b))?[\\s-]*(?:(?:number|#|n[o°]|n°)\\s*(?:1|one|won|一|壹|①)|(?:first|1st|best|top|greatest|最(?:棒|強|好)|第一))", - "Taiwan(?:[\\s-]*(?:is|be|are|=|:|\\bis\\b|\\bare\\b))?[\\s-]*(?:(?:number|#|n[o°]|n°)\\s*(?:1|one|won|①)|(?:first|1st|best|top|greatest))", - "Taiwán(?:[\\s-]*(?:is|be|are|=|:|\\bis\\b|\\bare\\b))?[\\s-]*(?:(?:number|#|n[o°]|n°)\\s*(?:1|one|won|①)|(?:first|1st|best|top|greatest))" + "Taiwan(?:[\\s-]+(?:is|be|are|=|:|\\bis\\b|\\bare\\b))?[\\s-]+(?:(?:number|#|n[o°]\\.?|n°\\.?)\\s*(?:1|one|①)|(?:first|1st|best|top|greatest))(?!\\s*\\d)", + "(?:台|臺)灣(?:[\\s-]+(?:是|=|:)?)?[\\s-]+(?:(?:第|#|No\\.?)\\s*(?:1|一|壹|①)|(?:最(?:棒|強|好)|冠軍))(?!\\s*\\d)", + "tâi-uân(?:[\\s-]*(?:tē-it))" ], "criteria_link": "tw_no_1" } diff --git a/tests/test_reacts/test_reacts_tw.py b/tests/test_reacts/test_reacts_tw.py index 781461b..48dab55 100644 --- a/tests/test_reacts/test_reacts_tw.py +++ b/tests/test_reacts/test_reacts_tw.py @@ -57,7 +57,7 @@ "美麗島", ) -TEST_CASES_TW_NO_1_TRUE = [ +TEST_CASES_TW_NO_1_TRUE = ( "Taiwan #1", "Taiwan number 1", "Taiwan is number one", @@ -65,7 +65,6 @@ "Taiwan is #1", "Taiwan No. 1", "Taiwan n° 1", - "Taiwán número uno", "臺灣第一", "台灣最棒", "Taiwan is the best", @@ -85,10 +84,10 @@ "TAIWAN NUMBER ONE", "taiwan #1", "I believe Taiwan is truly number one in Asia", - "Taiwan, often called Formosa, is number 1 in my heart" -] + "Taiwan, often called Formosa, is number 1 in my heart", +) -TEST_CASES_TW_NO_1_FALSE = [ +TEST_CASES_TW_NO_1_FALSE = ( "Thailand #1", "Taiwan is great", "Number 1 Taiwan", @@ -100,9 +99,8 @@ "#1", "Number one", "Taiwan #", - "Taiwan number" -] - + "Taiwan number", +) ALL_TEST_CASES = TEST_CASE_EN + TEST_CASE_TW @@ -148,6 +146,7 @@ def test_react_tw_regex_no_match(test_str: str): # * surrounded by text assert not check_resource_match(f"a{test_str}b", resource_name='tw') + @pytest.mark.parametrize( "test_str", TEST_CASES_TW_NO_1_TRUE, @@ -155,13 +154,14 @@ def test_react_tw_regex_no_match(test_str: str): def test_react_tw_no_1_regex_yes_match(test_str: str): """Tests that these strings return TRUE.""" # * isolated string - assert check_resource_match(test_str, resource_name='tw') + assert check_resource_match(test_str, resource_name='tw', criteria_link="tw_no_1") # * surrounded by spaces - assert check_resource_match(f" {test_str} ", resource_name='tw') + assert check_resource_match(f" {test_str} ", resource_name='tw', criteria_link="tw_no_1") # * to lowercase - assert check_resource_match(test_str.lower(), resource_name='tw') + assert check_resource_match(test_str.lower(), resource_name='tw', criteria_link="tw_no_1") # * to title case - assert check_resource_match(test_str.title(), resource_name='tw') + assert check_resource_match(test_str.title(), resource_name='tw', criteria_link="tw_no_1") + @pytest.mark.parametrize( "test_str", @@ -170,10 +170,10 @@ def test_react_tw_no_1_regex_yes_match(test_str: str): def test_react_tw_no_1_regex_no_match(test_str: str): """Tests that these strings return FALSE.""" # * isolated string - assert not check_resource_match(test_str, resource_name='tw') + assert not check_resource_match(test_str, resource_name='tw', criteria_link="tw_no_1") # * surrounded by spaces - assert not check_resource_match(f" {test_str} ", resource_name='tw') + assert not check_resource_match(f" {test_str} ", resource_name='tw', criteria_link="tw_no_1") # * to lowercase - assert not check_resource_match(test_str.lower(), resource_name='tw') + assert not check_resource_match(test_str.lower(), resource_name='tw', criteria_link="tw_no_1") # * to title case - assert not check_resource_match(test_str.title(), resource_name='tw') \ No newline at end of file + assert not check_resource_match(test_str.title(), resource_name='tw', criteria_link="tw_no_1") From 913cd50fe5db0d7aa00be10de7ca91c2f9de95b3 Mon Sep 17 00:00:00 2001 From: howardt12345 Date: Thu, 19 Sep 2024 11:46:37 -0400 Subject: [PATCH 4/5] query adjustments --- resources/reacts/tw.json | 8 +++++--- tests/test_reacts/test_reacts_tw.py | 16 +++++++++++----- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/resources/reacts/tw.json b/resources/reacts/tw.json index 50afc76..e22f9cf 100644 --- a/resources/reacts/tw.json +++ b/resources/reacts/tw.json @@ -58,9 +58,11 @@ { "match_whole_word": true, "keywords": [ - "Taiwan(?:[\\s-]+(?:is|be|are|=|:|\\bis\\b|\\bare\\b))?[\\s-]+(?:(?:number|#|n[o°]\\.?|n°\\.?)\\s*(?:1|one|①)|(?:first|1st|best|top|greatest))(?!\\s*\\d)", - "(?:台|臺)灣(?:[\\s-]+(?:是|=|:)?)?[\\s-]+(?:(?:第|#|No\\.?)\\s*(?:1|一|壹|①)|(?:最(?:棒|強|好)|冠軍))(?!\\s*\\d)", - "tâi-uân(?:[\\s-]*(?:tē-it))" + "Taiwan(?:[\\s-]*(?:is|be|are|=|:|\\bis\\b|\\bare\\b))?[\\s-]*(?:(?:number|#|no?\\.?|n°\\.?)\\s*(?:1|one|won|①|壹)|(?:first|1st|best|top|greatest|the\\s+best))(?![\\s-]*\\d)", + "(?:台|臺)灣(?:[\\s-]*(?:是|=|:)?)?[\\s-]*(?:(?:第|#|No\\.?)\\s*(?:1|一|壹|①)|(?:最(?:棒|強|好)|冠軍|第一名))(?![\\s-]*\\d)", + "tâi-uân[\\s-]*tē-it", + "Taiwan[\\s-]*(?:第一|最棒|最強|最好)", + "(?:台|臺)灣[\\s-]*(?:number[\\s-]*one|is[\\s-]*number[\\s-]*one)" ], "criteria_link": "tw_no_1" } diff --git a/tests/test_reacts/test_reacts_tw.py b/tests/test_reacts/test_reacts_tw.py index 48dab55..3f349e7 100644 --- a/tests/test_reacts/test_reacts_tw.py +++ b/tests/test_reacts/test_reacts_tw.py @@ -78,13 +78,12 @@ "台灣是第一", "Taiwan 第一", "臺灣 number one", - "Taiwan won", - "Taiwan ①", - "Taiwan 壹", "TAIWAN NUMBER ONE", "taiwan #1", - "I believe Taiwan is truly number one in Asia", - "Taiwan, often called Formosa, is number 1 in my heart", + "台灣冠軍", + "臺灣 is number one", + "Taiwan第一", + "tâi-uân tē-it" ) TEST_CASES_TW_NO_1_FALSE = ( @@ -100,6 +99,13 @@ "Number one", "Taiwan #", "Taiwan number", + "Taiwan is second", + "Taiwan and Japan are both great", + "Taiwan's number 1 export", + "Taiwan has won", + "Taiwan will be number 1", + "Taiwan used to be number 1", + "Taiwan aims to be number 1" ) ALL_TEST_CASES = TEST_CASE_EN + TEST_CASE_TW From 612081e3ab4103ea06c171f5e06fb52015e6c85f Mon Sep 17 00:00:00 2001 From: howardt12345 Date: Fri, 20 Sep 2024 09:55:27 -0400 Subject: [PATCH 5/5] lower sleep --- functions/reacts.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/functions/reacts.py b/functions/reacts.py index 0e4b320..3058e0d 100644 --- a/functions/reacts.py +++ b/functions/reacts.py @@ -137,7 +137,7 @@ async def react_to_message( ) -> bool: reaction_happened = False for possible_reaction in possible_reactions: - sleep(0.05) + sleep(0.005) # If the matched linked results exists check if the reaction is linked to a match if not evaluate_event_condition(possible_reaction.condition, criteria_links): continue @@ -186,7 +186,7 @@ async def reply_to_message( ) -> bool: reply_happened = False for possible_reply in possible_replies: - sleep(0.05) + sleep(0.005) # If the matched linked results exists check if the reply is linked to a match if not evaluate_event_condition(possible_reply.condition, criteria_links): continue