maubot-audio-preventer

git clone git://archive.git.mtrnord.blog/MTRNord/maubot-audio-preventer.git
Log | Files | Refs | README | LICENSE

bot.py (5261B)


      1 from typing import Type
      2 
      3 from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper
      4 from mautrix.types import EventType, GenericEvent, MessageType, TextMessageEventContent
      5 
      6 from maubot import Plugin, MessageEvent
      7 from maubot.handlers import event
      8 
      9 from .db import Database, UserInfo
     10 
     11 
     12 m_voice_event = EventType.find("m.voice",
     13                                t_class=EventType.Class.MESSAGE)
     14 
     15 
     16 class Config(BaseProxyConfig):
     17     def do_update(self, helper: ConfigUpdateHelper) -> None:
     18         helper.copy("text_warning_amount")
     19         helper.copy("kick_warning_amount")
     20         helper.copy("whitelist")
     21 
     22 
     23 class MaubotAudioPreventer(Plugin):
     24     db: Database
     25 
     26     async def start(self) -> None:
     27         self.config.load_and_update()
     28         self.db = Database(self.database)
     29         # self.log.debug("Loaded %s from config example 2", self.config["example_2.value"])
     30 
     31     async def stop(self) -> None:
     32         pass
     33 
     34     @classmethod
     35     def get_config_class(cls) -> Type[BaseProxyConfig]:
     36         return Config
     37 
     38     # async def handle_voice(self, evt: )
     39 
     40     @event.on(EventType.ROOM_MESSAGE)
     41     async def audio_event_handler(self, evt: MessageEvent) -> None:
     42         if evt.content.msgtype != MessageType.AUDIO or evt.sender in self.config['whitelist']:
     43             return
     44         if evt.content.get("m.voice") is None and evt.content.get("org.matrix.msc1767.audio") is None:
     45             return
     46 
     47         await self.client.redact(evt.room_id, evt.event_id, "Voice messages are not allowed")
     48 
     49         warnings = self.db.get_user(evt.sender)
     50         if (warnings is None):
     51             await evt.reply(f"""Please do not send Audio or Voice messages. This is your first warning! You have {self.config['text_warning_amount'] - 1} warnings left before you will get kicked.\n
     52 If you then still not comply you will after {self.config['kick_warning_amount']} kicks get banned from this room. The counter is across all rooms this bot is admin in.""")
     53             self.db.add_user(evt.sender)
     54         else:
     55             warnings_typed: UserInfo = warnings
     56             text_warnings: int = getattr(warnings_typed, 'text_warnings')
     57             kick_warnings: int = getattr(warnings_typed, 'kick_warnings')
     58             if (text_warnings - 1 != self.config['text_warning_amount']):
     59                 await evt.reply(f"Please do not send Audio or Voice messages. This is your first warning! You have {self.config['text_warning_amount'] - text_warnings - 1} left.")
     60                 self.db.increment_text_warnings(evt.sender, text_warnings)
     61             elif (kick_warnings - 1 != self.config['kick_warning_amount']):
     62                 await self.client.kick_user(evt.room_id, evt.sender, f"You exceeded your voice message warnings. This is your first kick! You have {self.config['kick_warning_amount'] - kick_warnings - 1} left.")
     63                 self.db.increment_kick_warnings(evt.sender, text_warnings)
     64             else:
     65                 await self.client.ban_user(evt.room_id, evt.sender, "You exceeded your voice message kick warnings. You are now getting banned for spam!")
     66 
     67     @ event.on(m_voice_event)
     68     async def handle_m_voice_event(self, evt: GenericEvent) -> None:
     69 
     70         await self.client.redact(evt.room_id, evt.event_id, "Voice messages are not allowed")
     71         warnings = self.db.get_user(evt.sender)
     72         if (warnings is None):
     73             content = TextMessageEventContent(
     74                 msgtype=MessageType.NOTICE, content=f"""Please do not send Audio or Voice messages. This is your first warning! You have {self.config['text_warning_amount'] - 1} warnings left before you will get kicked.\n
     75 If you then still not comply you will after {self.config['kick_warning_amount']} kicks get banned from this room. The counter is across all rooms this bot is admin in.""")
     76             content.set_reply(evt.event_id)
     77 
     78             await self.client.send_message_event(evt.room_id, EventType.ROOM_MESSAGE, content)
     79             self.db.add_user(evt.sender)
     80         else:
     81             warnings_typed: UserInfo = warnings
     82             text_warnings: int = getattr(warnings_typed, 'text_warnings')
     83             kick_warnings: int = getattr(warnings_typed, 'kick_warnings')
     84             if (text_warnings - 1 != self.config['text_warning_amount']):
     85                 content = TextMessageEventContent(
     86                     msgtype=MessageType.NOTICE, content=f"Please do not send Audio or Voice messages. This is your first warning! You have {self.config['text_warning_amount'] - text_warnings - 1} left.")
     87                 content.set_reply(evt.event_id)
     88 
     89                 await self.client.send_message_event(evt.room_id, EventType.ROOM_MESSAGE, content)
     90                 self.db.increment_text_warnings(evt.sender, text_warnings)
     91             elif (kick_warnings - 1 != self.config['kick_warning_amount']):
     92                 await self.client.kick_user(evt.room_id, evt.sender, f"You exceeded your voice message warnings. This is your first kick! You have {self.config['kick_warning_amount'] - kick_warnings - 1} left.")
     93                 self.db.increment_kick_warnings(evt.sender, text_warnings)
     94             else:
     95                 await self.client.ban_user(evt.room_id, evt.sender, "You exceeded your voice message kick warnings. You are now getting banned for spam!")