# 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 . import gettext from typing import Callable, Optional from src.api.context import Context from src.discord.message import DiscordMessage, DiscordMessageMetadata from src.discord.queue import send_to_discord from src.api.util import compact_summary, compact_author, clean_link, sanitize_to_url, sanitize_to_markdown from src.api.hook import pre_hook from src.configloader import settings # { # "hooks": { # "talk_notify": { # "default": ["https://discord.com/api/webhooks/111111111111111111/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"], # "lang": "en", # "namespace": { # "10001": ["https://discord.com/api/webhooks/111111111111111111/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"], # "10003": ["https://discord.com/api/webhooks/111111111111111111/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"], # "10005": ["https://discord.com/api/webhooks/111111111111111111/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"], # "10007": ["https://discord.com/api/webhooks/111111111111111111/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"] # }, # "pages": { # "Minecraft Wiki talk:Community videos": [ # "https://discord.com/api/webhooks/111111111111111111/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", # "https://discord.com/api/webhooks/111111111111111111/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" # ] # }, # "extra_pages": { # "exact": ["Minecraft Wiki:Admin noticeboard"], # "partial": ["Minecraft Wiki:Forum/"] # } # } # } # } talk_notify = settings.get("hooks", {}).get("talk_notify", {}) @pre_hook def talk_notify_hook(context: Context, change: dict): if not talk_notify.get("default", []) or context.event not in ("edit", "new"): return if change.get("minor", False) or change["ns"] == 3: return ignore = change["ns"] % 2 == 0 if ignore and talk_notify.get("extra_pages", {}): extra = talk_notify.get("extra_pages", {}) if extra.get("exact", []): ignore = ignore and change["title"] not in extra.get("exact", []) if extra.get("partial", []): ignore = ignore and not any(page in change["title"] for page in extra.get("partial", [])) if ignore: return parsed_comment = compact_summary(context) author, author_url = compact_author(context, change) edit_link = clean_link("{wiki}index.php?title={article}&curid={pageid}&diff={diff}&oldid={oldrev}".format( wiki=context.client.WIKI_SCRIPT_PATH, pageid=change["pageid"], diff=change["revid"], oldrev=change["old_revid"], article=sanitize_to_url(change["title"]))) edit_size = change["newlen"] - change["oldlen"] sign = "" if edit_size > 0: sign = "+" bold = "" if abs(edit_size) > 500: bold = "**" translator: Optional[Callable] = None if settings["lang"] != talk_notify.get("lang", settings["lang"]): translator = gettext.translation('formatters', localedir='locale', languages=[talk_notify.get("lang", settings["lang"])]).gettext else: translator = context.gettext if context.event == "edit": content = translator( "[{author}]({author_url}) edited [{article}]({edit_link}){comment} {bold}({sign}{edit_size}){bold}").format( author=author, author_url=author_url, article=sanitize_to_markdown(change["title"]).replace("\\:", ":"), edit_link=edit_link, comment=parsed_comment, edit_size=edit_size, sign=sign, bold=bold) else: content = translator( "[{author}]({author_url}) created [{article}]({edit_link}){comment} {bold}({sign}{edit_size}){bold}").format( author=author, author_url=author_url, article=sanitize_to_markdown(change["title"]).replace("\\:", ":"), edit_link=edit_link, comment=parsed_comment, edit_size=edit_size, sign=sign, bold=bold) webhook_list = talk_notify.get("default", []) if str(change["ns"]) in talk_notify.get("namespace", {}): webhook_list = talk_notify.get("namespace", {}).get(str(change["ns"]), webhook_list) if change["title"] in talk_notify.get("pages", {}): webhook_list = talk_notify.get("pages", {}).get(change["title"], webhook_list) for webhook in webhook_list: msg = DiscordMessage("compact", context.event, webhook, content=content) send_to_discord(msg, DiscordMessageMetadata("POST"))