Skip to content

Commit 24ab2fa

Browse files
author
0xMett
committed
feat(settings): add /setwelcomedelay and /getwelcomedelay commands
Admin-only group commands to configure per-group auto-delete delay for welcome messages. Setting delay to 0 disables auto-delete.
1 parent 230381f commit 24ab2fa

1 file changed

Lines changed: 74 additions & 4 deletions

File tree

src/python_italy_bot/handlers/settings.py

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,22 @@ def create_settings_handlers() -> list:
3232
CommandHandler("setwelcome", _handle_setwelcome),
3333
CommandHandler("resetwelcome", _handle_resetwelcome),
3434
CommandHandler("getwelcome", _handle_getwelcome),
35+
CommandHandler("setwelcomedelay", _handle_setwelcomedelay),
36+
CommandHandler("getwelcomedelay", _handle_getwelcomedelay),
3537
]
3638

3739

3840
async def _handle_setwelcome(
3941
update: Update, context: ContextTypes.DEFAULT_TYPE
4042
) -> None:
4143
"""Set custom welcome message for the group.
42-
44+
4345
Usage: /setwelcome <message>
44-
46+
4547
Supports placeholders:
4648
- {username}: @username or full name
4749
- {chatname}: group name
48-
50+
4951
Supports button syntax:
5052
- [Button Text](buttonurl://URL)
5153
"""
@@ -134,6 +136,74 @@ async def _handle_getwelcome(
134136
else:
135137
bot_username = (await context.bot.get_me()).username or "bot"
136138
default = captcha_service.get_default_welcome_template(bot_username)
139+
await message.reply_text(strings.GETWELCOME_DEFAULT.format(message=default))
140+
141+
142+
async def _handle_setwelcomedelay(
143+
update: Update, context: ContextTypes.DEFAULT_TYPE
144+
) -> None:
145+
"""Set auto-delete delay for welcome messages. Usage: /setwelcomedelay <minutes>."""
146+
captcha_service: CaptchaService = context.bot_data["captcha_service"]
147+
message = update.message
148+
if message is None or message.from_user is None:
149+
return
150+
151+
chat = update.effective_chat
152+
if chat is None or chat.type == "private":
153+
await message.reply_text(strings.ONLY_IN_GROUPS)
154+
return
155+
156+
if not await _is_admin(context, chat.id, message.from_user.id):
157+
await message.reply_text(strings.ONLY_ADMINS)
158+
return
159+
160+
if message.text is None:
161+
return
162+
163+
parts = message.text.split(maxsplit=1)
164+
if len(parts) < 2 or not parts[1].strip().isdigit():
165+
await message.reply_text(strings.SETWELCOMEDELAY_USAGE)
166+
return
167+
168+
minutes = int(parts[1].strip())
169+
170+
if minutes == 0:
171+
await captcha_service.set_welcome_delay(chat.id, 0)
172+
await message.reply_text(strings.SETWELCOMEDELAY_DISABLED)
173+
else:
174+
await captcha_service.set_welcome_delay(chat.id, minutes)
137175
await message.reply_text(
138-
strings.GETWELCOME_DEFAULT.format(message=default)
176+
strings.SETWELCOMEDELAY_SUCCESS.format(minutes=minutes)
139177
)
178+
179+
logger.info(
180+
"Welcome delay set to %s min for chat %s by admin %s",
181+
minutes,
182+
chat.id,
183+
message.from_user.id,
184+
)
185+
186+
187+
async def _handle_getwelcomedelay(
188+
update: Update, context: ContextTypes.DEFAULT_TYPE
189+
) -> None:
190+
"""Show current auto-delete delay for welcome messages."""
191+
captcha_service: CaptchaService = context.bot_data["captcha_service"]
192+
message = update.message
193+
if message is None or message.from_user is None:
194+
return
195+
196+
chat = update.effective_chat
197+
if chat is None or chat.type == "private":
198+
await message.reply_text(strings.ONLY_IN_GROUPS)
199+
return
200+
201+
if not await _is_admin(context, chat.id, message.from_user.id):
202+
await message.reply_text(strings.ONLY_ADMINS)
203+
return
204+
205+
delay = await captcha_service.get_welcome_delay(chat.id)
206+
if delay is not None:
207+
await message.reply_text(strings.GETWELCOMEDELAY_RESPONSE.format(minutes=delay))
208+
else:
209+
await message.reply_text(strings.GETWELCOMEDELAY_DEFAULT)

0 commit comments

Comments
 (0)