Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement blacklist #160

Merged
merged 11 commits into from
Feb 25, 2024
2 changes: 2 additions & 0 deletions src/spotted/config/yaml/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ post:
delete_anonymous_comments: true
reject_after_autoreply: true
autoreplies_per_page: 6
blacklist_message: []
Helias marked this conversation as resolved.
Show resolved Hide resolved

token: ""
bot_tag: "@bot_tag"
1 change: 1 addition & 0 deletions src/spotted/config/yaml/settings.yaml.types
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ post:
delete_anonymous_comments: bool
reject_after_autoreply: bool
autoreplies_per_page: int
blacklist_message: list[str]
Helias marked this conversation as resolved.
Show resolved Hide resolved
token: str
bot_tag: str
1 change: 1 addition & 0 deletions src/spotted/data/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"report_wait_mins",
"replace_anonymous_comments",
"delete_anonymous_comments",
"blacklist_message",
]
SettingsKeysType = Literal[SettingsKeys, SettingsPostKeys, SettingsDebugKeys]
AutorepliesKeysType = Literal["autoreplies"]
Expand Down
11 changes: 11 additions & 0 deletions src/spotted/handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Modules that handle the events the bot recognizes and reacts to"""

import ast
from datetime import time
from warnings import filterwarnings

Expand All @@ -16,6 +17,7 @@

from spotted.data.config import Config
from spotted.debug import error_handler, log_message
from spotted.handlers.spam_comment import spam_comment_msg

from .anonym_comment import anonymous_comment_msg
from .approve import approve_no_callback, approve_status_callback, approve_yes_callback
Expand Down Expand Up @@ -126,6 +128,7 @@ def add_handlers(app: Application):

if Config.post_get("comments"):
app.add_handler(MessageHandler(community_filter & filters.IS_AUTOMATIC_FORWARD, forwarded_post_msg))

if Config.post_get("delete_anonymous_comments"):
app.add_handler(
MessageHandler(
Expand All @@ -134,6 +137,14 @@ def add_handlers(app: Application):
)
)

if Config.post_get("blacklist_message") and len(ast.literal_eval(Config.post_get("blacklist_message"))) > 0:
Helias marked this conversation as resolved.
Show resolved Hide resolved
app.add_handler(
MessageHandler(
community_filter,
spam_comment_msg,
)
)

app.add_handler(MessageHandler(community_filter & filters.REPLY, follow_spot_comment))


Expand Down
25 changes: 25 additions & 0 deletions src/spotted/handlers/spam_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Anonym Comment on a post in the comment group"""

from telegram import Update
from telegram.ext import CallbackContext

from spotted.data import Config
from spotted.utils import EventInfo

import ast


async def spam_comment_msg(update: Update, context: CallbackContext):
"""Handles a spam comment on a post in the comment group.
Deletes the original post.

Args:
update: update event
context: context passed by the handler
"""
info = EventInfo.from_message(update, context)
blacklist_messages = ast.literal_eval((Config.post_get("blacklist_message")))
Helias marked this conversation as resolved.
Show resolved Hide resolved
for message in blacklist_messages:
if message in info.message.text:
await info.message.delete()
return
Loading