Skip to content

Commit

Permalink
improved regex and added Lithuanian keywords for Baltic heart
Browse files Browse the repository at this point in the history
  • Loading branch information
chanomkaimuk committed Aug 11, 2023
1 parent 908c357 commit 7288fc1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
27 changes: 27 additions & 0 deletions commands/react_baltics/consts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from re import compile, IGNORECASE, UNICODE

POSSIBLE_REACTS = (
"<:tw_baltics_heart:1132524940587962388>",
"<:bubblemilktea:1132632348651966596>",
)

KEYWORDS = (
# Lithuanian
"Taivanas",
"Taivane",
"Taivano",
"Taivanui",
"Taivanietis",
"Taivanietė",
"Taivaniečiai",
"Taivaniečių",
"Taivaniečiui",
"Taivaniečiams",
"Taivanietiškas",
"Taivana"
# Latvian
# Estonian
)


BALTIC_REGEX = compile(rf"({'|'.join(KEYWORDS)})", flags=IGNORECASE | UNICODE)
25 changes: 25 additions & 0 deletions commands/react_baltics/react_baltics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import discord
import random
from .consts import POSSIBLE_REACTS, BALTIC_REGEX


def is_baltic_message(message: discord.Message):
return BALTIC_REGEX.search(message.content)


def mock_bernoulli(p: float) -> bool:
"""Returns True with probability p.
Args:
p (float): a float between 0 and 1.
Returns:
bool: True or False.
"""
return random.random() < p


async def send_react_baltic(message: discord.Message):
for react in POSSIBLE_REACTS:
if mock_bernoulli(0.69):
await message.add_reaction(react)
8 changes: 6 additions & 2 deletions commands/reacttw/consts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from re import compile
from re import compile, IGNORECASE, UNICODE

POSSIBLE_REACTS = (
"<:flag_twi:1133045891780071436>",
Expand Down Expand Up @@ -44,10 +44,13 @@
"台灣",
"臺灣",
"臺北",
"台北",
"新北",
"桃園",
"臺中",
"台中",
"臺南",
"台南",
"高雄",
"新竹",
"苗栗",
Expand All @@ -59,6 +62,7 @@
"宜蘭",
"花蓮",
"臺東",
"台東",
"澎湖",
"金門",
"連江",
Expand All @@ -69,4 +73,4 @@
)


TW_REGEX = compile(rf"({'|'.join(KEYWORDS)})")
TW_REGEX = compile(rf"({'|'.join(KEYWORDS)})", flags=IGNORECASE | UNICODE)
2 changes: 1 addition & 1 deletion commands/reacttw/react_tw.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


def is_TW_message(message: discord.Message):
return TW_REGEX.search(message.content.upper())
return TW_REGEX.search(message.content)


def mock_bernoulli(p: float) -> bool:
Expand Down
4 changes: 4 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from commands.one_o_one import one_o_one
from commands import hgs
from commands.reacttw import react_tw
from commands.react_baltics import react_baltics
from commands.shiba import random_shiba
import sys

Expand Down Expand Up @@ -101,5 +102,8 @@ async def on_message(message: discord.Message):
if react_tw.is_TW_message(message):
await react_tw.send_react_tw(message)

if react_baltics.is_baltic_message(message):
await react_baltics.send_react_baltic(message)


client.run(TOKEN)

0 comments on commit 7288fc1

Please sign in to comment.