diff --git a/extensions/hooks/buttons.py b/extensions/hooks/buttons.py new file mode 100644 index 0000000..196f979 --- /dev/null +++ b/extensions/hooks/buttons.py @@ -0,0 +1,70 @@ +# 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 + +# The webhook used for RcGcDw need to be controlled by a Discord application running https://github.com/Markus-Rost/rcgcdw-buttons +# { +# "hooks": { +# "buttons": { +# "block": "Block user", +# "delete": "Delete", +# "filerevert": "Revert", +# "move": "Move back", +# "rollback": "Rollback", +# "undo": "Undo" +# } +# } +# } +action_buttons = settings.get("hooks", {}).get("buttons", {}) + +def add_button(message, custom_id, label, style=2, emoji=None): + if len(custom_id) > 100 or not len(label): + return + if "components" not in message.webhook_object: + message.webhook_object["components"] = [{"type": 1, "components": []}] + if len(message.webhook_object["components"][-1]["components"]) >= 5: + message.webhook_object["components"].append({"type": 1, "components": []}) + message.webhook_object["components"][-1]["components"].append({"type": 2, "custom_id": custom_id, "style": style, "label": label, "emoji": emoji}) + + +@post_hook +def buttons_hook(message, metadata, context, change): + if not len(action_buttons) or context.feed_type == "discussion": + return + BUTTON_PREFIX = context.client.WIKI_SCRIPT_PATH[len(context.client.WIKI_JUST_DOMAIN):] + if "block" in action_buttons and context.event != "suppressed": + add_button(message, BUTTON_PREFIX + " block " + ( "#" + str(change["userid"]) if change["userid"] else change["user"] ), + action_buttons["block"], 4, {"id": None, "name": "🚫"}) + if context.feed_type != "recentchanges": + return + if "delete" in action_buttons and context.event in ("new", "upload/upload"): + add_button(message, BUTTON_PREFIX + " delete " + str(change["pageid"]), + action_buttons["delete"], 4, {"id": None, "name": "🗑️"}) + # if "filerevert" in action_buttons and context.event in ("upload/overwrite", "upload/revert"): + # add_button(message, BUTTON_PREFIX + " file " + str(change["pageid"]) + " " + revision["archivename"].split("!")[0], + # action_buttons["filerevert"], 2, {"id": None, "name": "🔂"}) + if "move" in action_buttons and context.event in ("move/move", "move/move_redir"): + add_button(message, BUTTON_PREFIX + " move " + str(change["pageid"]) + " " + change["title"], + action_buttons["move"], 2, {"id": None, "name": "🔂"}) + if context.event != "edit": + return + if "rollback" in action_buttons: + add_button(message, BUTTON_PREFIX + " rollback " + str(change["pageid"]) + " " + ( "#" + str(change["userid"]) if change["userid"] else change["user"] ), + action_buttons["rollback"], 1, {"id": None, "name": "🔁"}) + if "undo" in action_buttons: + add_button(message, BUTTON_PREFIX + " undo " + str(change["pageid"]) + " " + str(change["revid"]), + action_buttons["undo"], 2, {"id": None, "name": "🔂"}) diff --git a/src/rcgcdw.py b/src/rcgcdw.py index 98e8855..39d76b1 100644 --- a/src/rcgcdw.py +++ b/src/rcgcdw.py @@ -89,7 +89,7 @@ def day_overview_request() -> list: continuearg: Optional[str] = None while passes < 10: params = OrderedDict(dict(action="query", format="json", list="recentchanges", rcend=timestamp, - rcprop="title|timestamp|sizes|loginfo|user", rcshow="!bot", rclimit="max", + rcprop="title|timestamp|sizes|loginfo|user|userid", rcshow="!bot", rclimit="max", rctype="edit|new|log", rccontinue=continuearg)) request = wiki.retried_api_request(params) result += request['query']['recentchanges'] diff --git a/src/wiki.py b/src/wiki.py index a8892f1..e5674c6 100644 --- a/src/wiki.py +++ b/src/wiki.py @@ -139,7 +139,7 @@ class Wiki(object): params = OrderedDict(action="query", format="json") params["list"] = "recentchanges|abuselog" if settings.get("show_abuselog", False) else "recentchanges" params["rcshow"] = "" if settings.get("show_bots", False) else "!bot" - params["rcprop"] = "title|redirect|timestamp|ids|loginfo|parsedcomment|sizes|flags|tags|user" + params["rcprop"] = "title|redirect|timestamp|ids|loginfo|parsedcomment|sizes|flags|tags|user|userid" params["rclimit"] = amount params["rctype"] = "edit|new|log|external|categorize" if settings.get("show_added_categories", True) else "edit|new|log|external" if settings.get("show_abuselog", False):