summaryrefslogtreecommitdiff
path: root/src/moyai_bot/lib.py
diff options
context:
space:
mode:
authorseth <[email protected]>2023-01-09 01:06:06 -0500
committerseth <[email protected]>2023-01-09 02:07:50 -0500
commitb0ca5f3393beb1d02f987e3c3f6c11d9f1df85d9 (patch)
treed7b14c500b59a7e3db0f48c3f568850a12f8fcbc /src/moyai_bot/lib.py
parentc6e02dfdb25aa2d172e4ddc26ee681280bc41645 (diff)
feat: split messages that are too long
Diffstat (limited to 'src/moyai_bot/lib.py')
-rw-r--r--src/moyai_bot/lib.py27
1 files changed, 23 insertions, 4 deletions
diff --git a/src/moyai_bot/lib.py b/src/moyai_bot/lib.py
index 45f2099..65360a1 100644
--- a/src/moyai_bot/lib.py
+++ b/src/moyai_bot/lib.py
@@ -1,10 +1,13 @@
import importlib.resources
import random
+from math import ceil
import discord
from moyai_bot import copypastas
+CHAR_LIMIT: int = 2000
+
def get_random_response(moyai):
responses = [
@@ -24,11 +27,27 @@ def get_random_response(moyai):
return random.choice(responses)
+def split_msg(msg: str):
+ """
+ splits a message into multiple parts so that it
+ can fit into the discord character limit
+ """
+ split = ceil(len(msg) / ceil(len(msg) / CHAR_LIMIT))
+ return [msg[i:i + split] for i in range(0, len(msg), split)]
+
+
def get_copypasta(name):
try:
res = importlib.resources.read_text(copypastas, name + ".txt")
- if res != "":
- return res
except OSError:
- pass
- return f"couldn't send copypasta: {name} :("
+ return "something went wrong :("
+
+ if res == "":
+ return f"couldn't send copypasta: {name} :("
+
+ if len(res) >= CHAR_LIMIT:
+ res = split_msg(res)
+ else:
+ res = [res]
+
+ return res