From 58da12ac9e2380d396a2253a67c8266e164a12ef Mon Sep 17 00:00:00 2001 From: Frisk Date: Fri, 22 May 2020 17:30:58 +0200 Subject: [PATCH] Fixed #116 --- discussions.py | 4 ++-- misc.py | 7 ++++--- rcgcdw.py | 17 +++++++---------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/discussions.py b/discussions.py index 8d0d682..acf84f4 100644 --- a/discussions.py +++ b/discussions.py @@ -44,7 +44,7 @@ fetch_url = "https://services.fandom.com/discussion/{wikiid}/posts?sortDirection def embed_formatter(post, post_type): """Embed formatter for Fandom discussions.""" - embed = DiscordMessage("embed", "discussion") + embed = DiscordMessage("embed", "discussion", settings["fandom_discussions"]["webhookURL"]) embed.set_author(post["createdBy"]["name"], "{wikiurl}f/u/{creatorId}".format( wikiurl=settings["fandom_discussions"]["wiki_url"], creatorId=post["creatorId"]), icon_url=post["createdBy"]["avatarUrl"]) if post_type == "TEXT": @@ -100,7 +100,7 @@ def compact_formatter(post, post_type): "[{author}](<{url}f/u/{creatorId}>) created a poll [{title}](<{url}f/p/{threadId}>) in {forumName}").format( author=post["createdBy"]["name"], url=settings["fandom_discussions"]["wiki_url"], creatorId=post["creatorId"], title=post["title"], threadId=post["threadId"], forumName=post["forumName"]) - send_to_discord(DiscordMessage("compact", "discussion", content=message)) + send_to_discord(DiscordMessage("compact", "discussion", settings["fandom_discussions"]["webhookURL"], content=message)) def fetch_discussions(): diff --git a/misc.py b/misc.py index 179b6f6..8667357 100644 --- a/misc.py +++ b/misc.py @@ -303,7 +303,7 @@ def create_article_path(article: str) -> str: def send_simple(msgtype, message, name, avatar): - discord_msg = DiscordMessage("compact", msgtype, content=message) + discord_msg = DiscordMessage("compact", msgtype, settings["webhookURL"], content=message) discord_msg.set_avatar(avatar) discord_msg.set_name(name) messagequeue.resend_msgs() @@ -314,7 +314,7 @@ def send_to_discord_webhook(data): header = settings["header"] header['Content-Type'] = 'application/json' try: - result = requests.post(settings["webhookURL"], data=repr(data), + result = requests.post(data.webhook_url, data=repr(data), headers=header, timeout=10) except requests.exceptions.Timeout: misc_logger.warning("Timeouted while sending data to the webhook.") @@ -342,8 +342,9 @@ def send_to_discord(data): class DiscordMessage(): """A class defining a typical Discord JSON representation of webhook payload.""" - def __init__(self, message_type: str, event_type: str, content=None): + def __init__(self, message_type: str, event_type: str, webhook_url: str, content=None): self.webhook_object = dict(allowed_mentions={"parse": []}, avatar_url=settings["avatars"].get(message_type, "")) + self.webhook_url = webhook_url if message_type == "embed": self.__setup_embed() diff --git a/rcgcdw.py b/rcgcdw.py index 54f7c7b..ba84852 100644 --- a/rcgcdw.py +++ b/rcgcdw.py @@ -426,11 +426,11 @@ def compact_formatter(action, change, parsed_comment, categories): else: logger.warning("No entry for {event} with params: {params}".format(event=action, params=change)) return - send_to_discord(DiscordMessage("compact", action, content=content)) + send_to_discord(DiscordMessage("compact", action, settings["webhookURL"], content=content)) def embed_formatter(action, change, parsed_comment, categories): - embed = DiscordMessage("embed", action) + embed = DiscordMessage("embed", action, settings["webhookURL"]) if parsed_comment is None: parsed_comment = _("No description provided") if action != "suppressed": @@ -983,15 +983,16 @@ def day_overview(): changed_bytes = 0 new_articles = 0 active_articles = [] + embed = DiscordMessage("embed", "daily_overview", settings["webhookURL"]) + embed["title"] = _("Daily overview") + embed["url"] = create_article_path("Special:Statistics") + embed.set_author(settings["wikiname"], create_article_path(""), + icon_url=settings["appearance"]["embed"]["daily_overview"]["icon"]) if not result[0]: if not settings["send_empty_overview"]: return # no changes in this day else: - embed = DiscordMessage("embed", "daily_overview") - embed["title"] = _("Daily overview") - embed["url"] = create_article_path("Special:Statistics") embed["description"] = _("No activity") - embed.set_author(settings["wikiname"], create_article_path(""), icon_url=settings["appearance"]["embed"]["daily_overview"]["icon"]) else: for item in result[0]: if "actionhidden" in item or "suppressed" in item or "userhidden" in item: @@ -1012,10 +1013,6 @@ def day_overview(): admin = admin + 1 if item["logtype"] in ["delete", "merge", "block", "protect", "import", "rights", "abusefilter", "interwiki", "managetags"] else admin overall = round(new_articles + edits * 0.1 + files * 0.3 + admin * 0.1 + math.fabs(changed_bytes * 0.001), 2) - embed = DiscordMessage("embed", "daily_overview") - embed["title"] = _("Daily overview") - embed["url"] = create_article_path("Special:Statistics") - embed.set_author(settings["wikiname"], create_article_path(""), icon_url=settings["appearance"]["embed"]["daily_overview"]["icon"]) if activity: active_users = [] for user, numberu in Counter(activity).most_common(3): # find most active users