Skip to content

Commit 41b3db6

Browse files
authored
feat minimum character requirement for thread creation. (#3380)
1 parent a794ebd commit 41b3db6

File tree

7 files changed

+80
-4
lines changed

7 files changed

+80
-4
lines changed

cogs/modmail.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,7 @@ async def contact(
16371637
creator=creator,
16381638
category=category,
16391639
manual_trigger=manual_trigger,
1640+
# The minimum character check is enforced in ThreadManager.create
16401641
)
16411642

16421643
if thread.cancelled:

core/_color_data.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
Slightly modified to conform with usage.
44
"""
55

6-
76
BASE_COLORS = {
87
"b": "0000ff",
98
"g": "007f00",

core/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ class ConfigManager:
129129
# regex
130130
"use_regex_autotrigger": False,
131131
"use_hoisted_top_role": True,
132+
# Minimum characters for thread creation
133+
"thread_min_characters": 0,
134+
"thread_min_characters_title": "Message too short",
135+
"thread_min_characters_response": "Your message is too short to create a thread. Please provide more details.",
136+
"thread_min_characters_footer": "Minimum {min_characters} characters required.",
132137
}
133138

134139
private_keys = {

core/config_help.json

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1222,5 +1222,48 @@
12221222
"If this configuration is enabled, only roles that are hoisted (displayed seperately in member list) will be used. If a user has no hoisted roles, it will return 'None'.",
12231223
"If you would like to display the top role of a user regardless of if it's hoisted or not, disable `use_hoisted_top_role`."
12241224
]
1225+
},
1226+
"thread_min_characters": {
1227+
"default": "0",
1228+
"description": "The minimum number of characters required in the initial message to create a thread. Set to 0 to disable.",
1229+
"examples": [
1230+
"`{prefix}config set thread_min_characters 20`"
1231+
],
1232+
"notes": [
1233+
"If a user tries to create a thread with a message shorter than this, an error will be shown.",
1234+
"See also: `thread_min_characters_title`, `thread_min_characters_response`, `thread_min_characters_footer`."
1235+
]
1236+
},
1237+
"thread_min_characters_title": {
1238+
"default": "Message too short",
1239+
"description": "The title of the error embed when a user tries to create a thread with too few characters.",
1240+
"examples": [
1241+
"`{prefix}config set thread_min_characters_title Too short!`"
1242+
],
1243+
"notes": [
1244+
"See also: `thread_min_characters`, `thread_min_characters_response`, `thread_min_characters_footer`."
1245+
]
1246+
},
1247+
"thread_min_characters_response": {
1248+
"default": "Your message is too short to create a thread. Please provide more details.",
1249+
"description": "The description of the error embed when a user tries to create a thread with too few characters.",
1250+
"examples": [
1251+
"`{prefix}config set thread_min_characters_response Please write a longer message.`"
1252+
],
1253+
"notes": [
1254+
"You can use `{min_characters}` as a placeholder for the minimum required characters.",
1255+
"See also: `thread_min_characters`, `thread_min_characters_title`, `thread_min_characters_footer`."
1256+
]
1257+
},
1258+
"thread_min_characters_footer": {
1259+
"default": "Minimum {min_characters} characters required.",
1260+
"description": "The footer of the error embed when a user tries to create a thread with too few characters.",
1261+
"examples": [
1262+
"`{prefix}config set thread_min_characters_footer At least {min_characters} characters needed.`"
1263+
],
1264+
"notes": [
1265+
"You can use `{min_characters}` as a placeholder for the minimum required characters.",
1266+
"See also: `thread_min_characters`, `thread_min_characters_title`, `thread_min_characters_response`."
1267+
]
12251268
}
1226-
}
1269+
}

core/thread.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,33 @@ async def create(
14061406
) -> Thread:
14071407
"""Creates a Modmail thread"""
14081408

1409+
# Minimum character check
1410+
min_chars = self.bot.config.get("thread_min_characters")
1411+
if min_chars is None:
1412+
min_chars = 0
1413+
try:
1414+
min_chars = int(min_chars)
1415+
except ValueError:
1416+
min_chars = 0
1417+
if min_chars > 0 and message is not None and message.content is not None:
1418+
if len(message.content.strip()) < min_chars:
1419+
embed = discord.Embed(
1420+
title=self.bot.config["thread_min_characters_title"],
1421+
description=self.bot.config["thread_min_characters_response"].replace(
1422+
"{min_characters}", str(min_chars)
1423+
),
1424+
color=self.bot.error_color,
1425+
)
1426+
embed.set_footer(
1427+
text=self.bot.config["thread_min_characters_footer"].replace(
1428+
"{min_characters}", str(min_chars)
1429+
)
1430+
)
1431+
await message.channel.send(embed=embed)
1432+
thread = Thread(self, recipient)
1433+
thread.cancelled = True
1434+
return thread
1435+
14091436
# checks for existing thread in cache
14101437
thread = self.cache.get(recipient.id)
14111438
if thread:

core/time.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Source:
44
https://github.com/Rapptz/RoboDanny/blob/rewrite/cogs/utils/time.py
55
"""
6+
67
from __future__ import annotations
78

89
import datetime

core/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def create_not_found_embed(word, possibilities, name, n=2, cutoff=0.6) -> discor
370370

371371
def parse_alias(alias, *, split=True):
372372
def encode_alias(m):
373-
return "\x1AU" + base64.b64encode(m.group(1).encode()).decode() + "\x1AU"
373+
return "\x1aU" + base64.b64encode(m.group(1).encode()).decode() + "\x1aU"
374374

375375
def decode_alias(m):
376376
return base64.b64decode(m.group(1).encode()).decode()
@@ -392,7 +392,7 @@ def decode_alias(m):
392392
iterate = [alias]
393393

394394
for a in iterate:
395-
a = re.sub("\x1AU(.+?)\x1AU", decode_alias, a)
395+
a = re.sub("\x1aU(.+?)\x1aU", decode_alias, a)
396396
if a[0] == a[-1] == '"':
397397
a = a[1:-1]
398398
aliases.append(a)

0 commit comments

Comments
 (0)