diff --git a/extensions/hooks/usertalk.py b/extensions/hooks/usertalk.py new file mode 100644 index 0000000..de156bf --- /dev/null +++ b/extensions/hooks/usertalk.py @@ -0,0 +1,37 @@ +# This file is part of Recent changes Goat compatible Discord webhook (RcGcDw). +# +# RcGcDw is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# RcGcDw is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with RcGcDw. If not, see . + +from src.api.hook import post_hook +from src.configloader import settings + +# { +# "hooks": { +# "usertalk": { +# "USERNAME": "USERID" +# } +# } +# } +discord_users = settings.get("hooks", {}).get("usertalk", {}) + +@post_hook +def example_post_hook(message, metadata, context, change): + if discord_users and change["ns"] in [2, 3, 202] and not "/" in change["title"]: + username = change["title"].split(':', 1)[1] + if discord_users.get(username, "") and username != change["user"]: + message.webhook_object["content"] = (content or "") + " <@{}>".format(discord_users[username]) + if message.webhook_object["allowed_mentions"].get("users", []): + message.webhook_object["allowed_mentions"]["users"].append(discord_users[username]) + else: + message.webhook_object["allowed_mentions"]["users"] = [discord_users[username]] diff --git a/src/discord/message.py b/src/discord/message.py index ceab2a5..57a572f 100644 --- a/src/discord/message.py +++ b/src/discord/message.py @@ -34,6 +34,7 @@ class DiscordMessage: content = settings["event_appearance"][event_type]["emoji"] + " " + content self.webhook_object["content"] = content + self.message_type = message_type self.event_type = event_type def __setitem__(self, key, value): @@ -63,6 +64,8 @@ class DiscordMessage: self.__setup_embed() def finish_embed(self): + if self.message_type != "embed": + return if self.embed["color"] is None: if settings["event_appearance"].get(self.event_type, {"color": None})["color"] is None: self.embed["color"] = random.randrange(1, 16777215) @@ -110,4 +113,4 @@ class DiscordMessageMetadata: self.new_data = new_data def dump_ids(self) -> (int, int, int): - return self.page_id, self.rev_id, self.log_id \ No newline at end of file + return self.page_id, self.rev_id, self.log_id diff --git a/src/misc.py b/src/misc.py index 11944cf..2e0093c 100644 --- a/src/misc.py +++ b/src/misc.py @@ -21,6 +21,7 @@ from urllib.parse import urlparse, urlunparse import requests from src.configloader import settings +from src.api.util import sanitize_to_markdown from src.discord.message import DiscordMessage, DiscordMessageMetadata from src.discord.queue import messagequeue, send_to_discord from src.exceptions import MediaWikiError @@ -344,13 +345,13 @@ class LinkParser(HTMLParser): def handle_data(self, data): if self.recent_href: - self.new_string = self.new_string + "[{}](<{}>)".format(data.replace("//", "/\\/"), self.recent_href) + self.new_string = self.new_string + "[{}](<{}>)".format(sanitize_to_markdown(data), self.recent_href) self.recent_href = "" else: - self.new_string = self.new_string + data.replace("//", "/\\/") + self.new_string = self.new_string + sanitize_to_markdown(data) def handle_comment(self, data): - self.new_string = self.new_string + data.replace("//", "/\\/") + self.new_string = self.new_string + sanitize_to_markdown(data) def handle_endtag(self, tag): misc_logger.debug(self.new_string) diff --git a/src/rcgcdw.py b/src/rcgcdw.py index 987d383..26001f2 100644 --- a/src/rcgcdw.py +++ b/src/rcgcdw.py @@ -30,7 +30,7 @@ from src.api.context import Context from src.api.hooks import formatter_hooks, pre_hooks, post_hooks from src.configloader import settings from src.misc import add_to_dict, datafile, WIKI_API_PATH, LinkParser, run_hooks -from src.api.util import create_article_path, default_message, sanitize_to_markdown +from src.api.util import create_article_path, default_message from src.discord.queue import send_to_discord from src.discord.message import DiscordMessage, DiscordMessageMetadata from src.exceptions import MWError, ServerError, MediaWikiError, BadRequest, ClientError @@ -210,7 +210,6 @@ def rc_processor(change, changed_categories): if "commenthidden" not in change: LinkParser.feed(change.get("parsedcomment", "")) parsed_comment = LinkParser.new_string - parsed_comment = sanitize_to_markdown(parsed_comment) else: parsed_comment = _("~~hidden~~") if not parsed_comment and context.message_type == "embed" and settings["appearance"].get("embed", {}).get("show_no_description_provided", True): @@ -257,6 +256,7 @@ def rc_processor(change, changed_categories): for revid in logparams.get("ids", []): delete_messages(dict(revid=revid)) run_hooks(post_hooks, discord_message, metadata, context, change) + discord_message.finish_embed() send_to_discord(discord_message, metadata) @@ -270,6 +270,7 @@ def abuselog_processing(entry): discord_message: Optional[DiscordMessage] = default_message(action, formatter_hooks)(context, entry) metadata = DiscordMessageMetadata("POST") run_hooks(post_hooks, discord_message, metadata, context, entry) + discord_message.finish_embed() send_to_discord(discord_message, metadata)