aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillermo Serrahima <dev@vylion.com>2026-02-02 16:32:47 +0100
committerGuillermo Serrahima <dev@vylion.com>2026-02-02 16:32:47 +0100
commite47dec1c86a871776d476f802c74abf74b93cc61 (patch)
treef77628da4e01e22b6592189de6d4296b091176e0
parenta9ae7587026e17a6347379584ee512b96429d31a (diff)
downloadvelascobot2-e47dec1c86a871776d476f802c74abf74b93cc61.tar.xz
velascobot2-e47dec1c86a871776d476f802c74abf74b93cc61.zip
More rudimentary error catches
-rw-r--r--velascobot2/bot.py170
1 files changed, 140 insertions, 30 deletions
diff --git a/velascobot2/bot.py b/velascobot2/bot.py
index 2cc2588..fe2e2ce 100644
--- a/velascobot2/bot.py
+++ b/velascobot2/bot.py
@@ -69,7 +69,14 @@ explanation = (
def static_reply(text: str, format=None):
async def reply(update: Update, context: ContextTypes.DEFAULT_TYPE):
assert update.message
- await update.message.reply_text(text, parse_mode=format)
+ try:
+ await update.message.reply_text(text, parse_mode=format)
+ except (
+ telegram.error.Forbidden,
+ telegram.error.BadRequest,
+ telegram.error.NetworkError,
+ ) as e:
+ logger.error(str(e))
return reply
@@ -103,7 +110,14 @@ async def ping(update: Update, _context: ContextTypes.DEFAULT_TYPE) -> None:
"""Pongs back."""
assert update.message is not None
- await update.message.reply_text("Pong!")
+ try:
+ await update.message.reply_text("Pong!")
+ except (
+ telegram.error.Forbidden,
+ telegram.error.BadRequest,
+ telegram.error.NetworkError,
+ ) as e:
+ logger.error(str(e))
async def error_handler(update: object, context: ContextTypes.DEFAULT_TYPE):
@@ -179,9 +193,16 @@ class Bot:
chat_id = str(update.message.chat.id)
chat_data = self.messenger.get_chat_data(chat_id)
if chat_data is None:
- await update.message.reply_text(
- "I've processed no messages so far."
- )
+ try:
+ await update.message.reply_text(
+ "I've processed no messages so far."
+ )
+ except (
+ telegram.error.Forbidden,
+ telegram.error.BadRequest,
+ telegram.error.NetworkError,
+ ) as e:
+ logger.error(str(e))
return
num = (
@@ -189,9 +210,16 @@ class Bot:
if chat_data.processed_messages > 0
else "no"
)
- await update.message.reply_text(
- "I've processed {} messages so far.".format(num)
- )
+ try:
+ await update.message.reply_text(
+ "I've processed {} messages so far.".format(num)
+ )
+ except (
+ telegram.error.Forbidden,
+ telegram.error.BadRequest,
+ telegram.error.NetworkError,
+ ) as e:
+ logger.error(str(e))
async def period(
self, update: Update, _context: ContextTypes.DEFAULT_TYPE
@@ -207,18 +235,32 @@ class Bot:
assert update.message.text
words = update.message.text.split()
if len(words) <= 1:
- await update.message.reply_text(
- f"The current speech period is {chat_data.message_period}"
- )
+ try:
+ await update.message.reply_text(
+ f"The current speech period is {chat_data.message_period}"
+ )
+ except (
+ telegram.error.Forbidden,
+ telegram.error.BadRequest,
+ telegram.error.NetworkError,
+ ) as e:
+ logger.error(str(e))
return
if chat_data.restricted:
assert update.message.from_user
user = await chat.get_member(update.message.from_user.id)
if not self.superuser(user):
- await update.message.reply_text(
- "You do not have permissions to do that."
- )
+ try:
+ await update.message.reply_text(
+ "You do not have permissions to do that."
+ )
+ except (
+ telegram.error.Forbidden,
+ telegram.error.BadRequest,
+ telegram.error.NetworkError,
+ ) as e:
+ logger.error(str(e))
return
try:
period = int(words[1])
@@ -226,6 +268,12 @@ class Bot:
await update.message.reply_text(
f"Period of speaking set to {period}."
)
+ except (
+ telegram.error.Forbidden,
+ telegram.error.BadRequest,
+ telegram.error.NetworkError,
+ ) as e:
+ logger.error(str(e))
except Exception:
await update.message.reply_text(
"Format was confusing; period unchanged from"
@@ -246,18 +294,32 @@ class Bot:
assert update.message.text
words = update.message.text.split()
if len(words) <= 1:
- await update.message.reply_text(
- f"The current answer chance is {chat_data.answer_chance}"
- )
+ try:
+ await update.message.reply_text(
+ f"The current answer chance is {chat_data.answer_chance}"
+ )
+ except (
+ telegram.error.Forbidden,
+ telegram.error.BadRequest,
+ telegram.error.NetworkError,
+ ) as e:
+ logger.error(str(e))
return
if chat_data.restricted:
assert update.message.from_user
user = await chat.get_member(update.message.from_user.id)
if not self.superuser(user):
- await update.message.reply_text(
- "You do not have permissions to do that."
- )
+ try:
+ await update.message.reply_text(
+ "You do not have permissions to do that."
+ )
+ except (
+ telegram.error.Forbidden,
+ telegram.error.BadRequest,
+ telegram.error.NetworkError,
+ ) as e:
+ logger.error(str(e))
return
try:
chance = float(words[1])
@@ -265,6 +327,12 @@ class Bot:
await update.message.reply_text(
f"Chance of answering set to {chance}."
)
+ except (
+ telegram.error.Forbidden,
+ telegram.error.BadRequest,
+ telegram.error.NetworkError,
+ ) as e:
+ logger.error(str(e))
except Exception:
await update.message.reply_text(
"Format was confusing; answer chance unchanged from"
@@ -278,7 +346,14 @@ class Bot:
assert update.message
if "group" not in update.message.chat.type:
- await update.message.reply_text("That only works in groups.")
+ try:
+ await update.message.reply_text("That only works in groups.")
+ except (
+ telegram.error.Forbidden,
+ telegram.error.BadRequest,
+ telegram.error.NetworkError,
+ ) as e:
+ logger.error(str(e))
return
chat = update.message.chat
chat_id = str(chat.id)
@@ -288,15 +363,29 @@ class Bot:
assert chat_data
if chat_data.restricted:
if not self.superuser(user):
- await update.message.reply_text(
- "You do not have permissions to do that."
- )
+ try:
+ await update.message.reply_text(
+ "You do not have permissions to do that."
+ )
+ except (
+ telegram.error.Forbidden,
+ telegram.error.BadRequest,
+ telegram.error.NetworkError,
+ ) as e:
+ logger.error(str(e))
return
self.messenger.update_set_restricted(chat_id, not chat_data.restricted)
allowed = (
"let only admins" if not chat_data.restricted else "let everyone"
)
- await update.message.reply_text(f"I will {allowed} configure me now.")
+ try:
+ await update.message.reply_text(f"I will {allowed} configure me now.")
+ except (
+ telegram.error.Forbidden,
+ telegram.error.BadRequest,
+ telegram.error.NetworkError,
+ ) as e:
+ logger.error(str(e))
async def silence(
self, update: Update, _context: ContextTypes.DEFAULT_TYPE
@@ -305,7 +394,14 @@ class Bot:
assert update.message
if "group" not in update.message.chat.type:
- await update.message.reply_text("That only works in groups.")
+ try:
+ await update.message.reply_text("That only works in groups.")
+ except (
+ telegram.error.Forbidden,
+ telegram.error.BadRequest,
+ telegram.error.NetworkError,
+ ) as e:
+ logger.error(str(e))
return
chat = update.message.chat
chat_id = str(chat.id)
@@ -315,13 +411,27 @@ class Bot:
assert chat_data
if chat_data.silenced:
if not self.superuser(user):
- await update.message.reply_text(
- "You do not have permissions to do that."
- )
+ try:
+ await update.message.reply_text(
+ "You do not have permissions to do that."
+ )
+ except (
+ telegram.error.Forbidden,
+ telegram.error.BadRequest,
+ telegram.error.NetworkError,
+ ) as e:
+ logger.error(str(e))
return
self.messenger.update_set_silenced(chat_id, not chat_data.silenced)
allowed = "avoid mentioning" if not chat_data.silenced else "mention"
- await update.message.reply_text(f"I will {allowed} people now.")
+ try:
+ await update.message.reply_text(f"I will {allowed} people now.")
+ except (
+ telegram.error.Forbidden,
+ telegram.error.BadRequest,
+ telegram.error.NetworkError,
+ ) as e:
+ logger.error(str(e))
async def send(
self,