This commit is contained in:
夜坂雅 2023-02-15 08:17:30 +08:00
parent 12e35aca98
commit 629a253210
4 changed files with 40 additions and 22 deletions

View File

@ -16,6 +16,7 @@ from nio import (
from nyx_bot.archcn_utils import send_archlinuxcn_pkg, update_archlinuxcn_pkg
from nyx_bot.chat_functions import (
bulk_update_messages,
send_exception,
send_multiquote_image,
send_quote_image,
send_text_to_room,
@ -68,6 +69,16 @@ class Command:
async def process(self):
"""Process the command"""
try:
await self._process()
except Exception as inst:
# Clear any previous typing event
await self.client.room_typing(self.room.room_id, False)
await send_exception(
self.client, inst, self.room.room_id, self.event.event_id
)
async def _process(self):
if self.command == "quote":
await self._quote()
elif self.command == "archlinuxcn":

View File

@ -1,3 +1,4 @@
import asyncio
import logging
import time
@ -12,7 +13,6 @@ from nio import (
)
from nyx_bot.bot_commands import Command
from nyx_bot.chat_functions import send_exception
from nyx_bot.config import Config
from nyx_bot.message_responses import Message
from nyx_bot.storage import MatrixMessage, MembershipUpdates
@ -26,6 +26,7 @@ from nyx_bot.utils import (
)
logger = logging.getLogger(__name__)
callbacks_ = set()
class Callbacks:
@ -96,14 +97,9 @@ class Callbacks:
message = Message(
self.client, self.config, msg, room, event, reply_to, self.room_features
)
try:
await message.process()
except Exception as inst:
# Clear any previous typing event
await self.client.room_typing(room.room_id, False)
await send_exception(self.client, inst, room.room_id, event.event_id)
finally:
return
task = asyncio.create_task(message.process())
callbacks_.add(task)
task.add_done_callback(callbacks_.discard)
# Treat it as a command only if it has a prefix
if has_command_prefix:
@ -120,12 +116,9 @@ class Callbacks:
self.replace_map,
self.command_prefix,
)
try:
await command.process()
except Exception as inst:
# Clear any previous typing event
await self.client.room_typing(room.room_id, False)
await send_exception(self.client, inst, room.room_id, event.event_id)
task = asyncio.create_task(command.process())
callbacks_.add(task)
task.add_done_callback(callbacks_.discard)
async def unknown(self, room: MatrixRoom, event: UnknownEvent) -> None:
"""Callback for when an event with a type that is unknown to matrix-nio is received.

View File

@ -4,7 +4,11 @@ from zlib import crc32
from nio import AsyncClient, MatrixRoom, RoomMessageText
from nyx_bot.chat_functions import gen_result_randomdraw, send_text_to_room
from nyx_bot.chat_functions import (
gen_result_randomdraw,
send_exception,
send_text_to_room,
)
from nyx_bot.config import Config
from nyx_bot.jerryxiao import send_jerryxiao
from nyx_bot.trpg_dicer import get_trpg_dice_result
@ -47,6 +51,16 @@ class Message:
async def process(self) -> None:
"""Process and possibly respond to the message"""
try:
await self._process()
except Exception as inst:
# Clear any previous typing event
await self.client.room_typing(self.room.room_id, False)
await send_exception(
self.client, inst, self.room.room_id, self.event.event_id
)
async def _process(self) -> None:
if re.match("^(!!|\\\\|/|¡¡)", self.message_content):
await self._jerryxiao()
elif self.message_content.startswith("@@"):

View File

@ -44,8 +44,8 @@ def make_image(freqs, bytesio):
image = (
WordCloud(
font_path=FONT,
width=1600,
height=800,
width=800,
height=400,
)
.generate_from_frequencies(freqs)
.to_image()
@ -176,14 +176,14 @@ def gather_messages(
)
if fwd_match is not None:
string = fwd_match.group(1)
print(strip_urls(strip_tags(string)), file=stringio)
print(strip_tags(string), file=stringio)
elif msg_item.body is not None:
# XXX: Special case for Arch Linux CN
if msg_item.sender == "@matterbridge:nichi.co":
data = re.sub(r"^\[.*\] ", "", msg_item.body)
print(strip_urls(data.strip()), file=stringio)
print(data.strip(), file=stringio)
else:
print(strip_urls(msg_item.body), file=stringio)
print(msg_item.body, file=stringio)
else:
continue
@ -191,4 +191,4 @@ def gather_messages(
users.add(msg_item.sender)
ret = stringio.getvalue()
return (ret, count, users)
return (strip_urls(ret), count, users)