RcGcDw/extensions/hooks/buttons.py

97 lines
5.2 KiB
Python
Raw Permalink Normal View History

2023-05-06 16:25:29 +00:00
# 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 <http://www.gnu.org/licenses/>.
2023-09-16 18:23:03 +00:00
from typing import Optional
2023-05-06 16:25:29 +00:00
2023-09-16 18:23:03 +00:00
from src.api.context import Context
from src.discord.message import DiscordMessage, DiscordMessageMetadata
2023-05-06 16:25:29 +00:00
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
2023-05-07 22:10:43 +00:00
# You can use https://www.wikibot.de/interactions to create a Discord webhook to be used for RcGcDw that supports buttons.
2023-05-06 16:25:29 +00:00
# {
# "hooks": {
# "buttons": {
# "block": "Block user",
# "blocknotalk": "Block user (disable talk)",
2023-05-06 16:25:29 +00:00
# "delete": "Delete",
# "filerevert": "Revert",
# "move": "Move back",
# "rollback": "Rollback",
2024-07-05 21:37:51 +00:00
# "thank": "Thank user",
2023-05-06 16:25:29 +00:00
# "undo": "Undo"
# }
# }
# }
action_buttons = settings.get("hooks", {}).get("buttons", {})
2023-09-16 18:23:03 +00:00
def add_button(message: DiscordMessage, custom_id: str, label, style=2, emoji: Optional[dict] = None):
2023-05-06 16:25:29 +00:00
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": []})
2023-09-16 18:23:03 +00:00
message.webhook_object["components"][-1]["components"].append(
{"type": 2, "custom_id": custom_id, "style": style, "label": label, "emoji": emoji})
2023-05-06 16:25:29 +00:00
@post_hook
2023-09-16 18:23:03 +00:00
def buttons_hook(message: DiscordMessage, metadata: DiscordMessageMetadata, context: Context, change: dict):
2023-05-06 16:25:29 +00:00
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 context.event != "suppressed":
if "blocknotalk" in action_buttons:
add_button(message,
BUTTON_PREFIX + " blocknotalk " + ("#" + str(change["userid"]) if change["userid"] else change["user"]),
action_buttons["blocknotalk"], 4, {"id": None, "name": "🚧"})
if "block" in action_buttons:
add_button(message,
BUTTON_PREFIX + " block " + ("#" + str(change["userid"]) if change["userid"] else change["user"]),
action_buttons["block"], 4, {"id": None, "name": "🚧"})
2023-05-06 16:25:29 +00:00
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"]),
2023-09-16 18:23:03 +00:00
action_buttons["delete"], 4, {"id": None, "name": "🗑️"})
2024-09-26 02:57:45 +00:00
if "filerevert" in action_buttons and context.event in ("upload/overwrite", "upload/revert") and context.image_data:
found_cur = False
for revision in context.image_data.get("imageinfo", []):
if found_cur:
add_button(message, BUTTON_PREFIX + " file " + str(change["pageid"]) + " " + revision["archivename"].split("!")[0],
action_buttons["filerevert"], 2, {"id": None, "name": "🔂"})
break
if revision["timestamp"] == change["logparams"]["img_timestamp"]: # find the correct revision corresponding for this log entry
found_cur = True
2023-05-06 16:25:29 +00:00
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": "🔂"})
2024-07-05 21:37:51 +00:00
if "rollback" in action_buttons and context.event == "edit":
2023-09-16 18:23:03 +00:00
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": "🔁"})
2024-07-05 21:37:51 +00:00
if "undo" in action_buttons and context.event == "edit":
2023-05-06 16:25:29 +00:00
add_button(message, BUTTON_PREFIX + " undo " + str(change["pageid"]) + " " + str(change["revid"]),
action_buttons["undo"], 2, {"id": None, "name": "🔂"})
2024-07-05 21:43:34 +00:00
if "thank" in action_buttons and context.event != "suppressed":
2024-07-05 21:37:51 +00:00
if change["type"] == "log":
add_button(message, BUTTON_PREFIX + " thank log " + str(change["logid"]),
action_buttons["thank"], 3, {"id": None, "name": "👍"})
else:
add_button(message, BUTTON_PREFIX + " thank rev " + str(change["revid"]),
action_buttons["thank"], 3, {"id": None, "name": "👍"})