This commit is contained in:
Frisk 2020-05-09 00:16:14 +02:00
parent 8770fcea47
commit b0698bddde
No known key found for this signature in database
GPG key ID: 213F7C15068AF8AC
2 changed files with 18 additions and 14 deletions

16
misc.py
View file

@ -302,6 +302,14 @@ def create_article_path(article: str) -> str:
return WIKI_ARTICLE_PATH.replace("$1", article) return WIKI_ARTICLE_PATH.replace("$1", article)
def send_simple(msgtype, message, name, avatar):
discord_msg = DiscordMessage("compact", msgtype, content=message)
discord_msg.set_avatar(avatar)
discord_msg.set_name(name)
messagequeue.resend_msgs()
send_to_discord(discord_msg)
def send_to_discord_webhook(data): def send_to_discord_webhook(data):
header = settings["header"] header = settings["header"]
header['Content-Type'] = 'application/json' header['Content-Type'] = 'application/json'
@ -387,4 +395,10 @@ class DiscordMessage():
def add_field(self, name, value, inline=False): def add_field(self, name, value, inline=False):
if "fields" not in self.embed: if "fields" not in self.embed:
self.embed["fields"] = [] self.embed["fields"] = []
self.embed["fields"].append(dict(name=name, value=value, inline=inline)) self.embed["fields"].append(dict(name=name, value=value, inline=inline))
def set_avatar(self, url):
self.webhook_object["avatar_url"] = url
def set_name(self, name):
self.webhook_object["username"] = name

View file

@ -30,7 +30,7 @@ from urllib.parse import quote_plus
from configloader import settings from configloader import settings
from misc import link_formatter, ContentParser, safe_read, add_to_dict, datafile, \ from misc import link_formatter, ContentParser, safe_read, add_to_dict, datafile, \
WIKI_API_PATH, WIKI_SCRIPT_PATH, WIKI_JUST_DOMAIN, create_article_path, messagequeue, send_to_discord_webhook, \ WIKI_API_PATH, WIKI_SCRIPT_PATH, WIKI_JUST_DOMAIN, create_article_path, messagequeue, send_to_discord_webhook, \
send_to_discord, DiscordMessage send_to_discord, DiscordMessage, send_simple
from session import session from session import session
if settings["fandom_discussions"]["enabled"]: if settings["fandom_discussions"]["enabled"]:
@ -114,16 +114,6 @@ LinkParser = LinkParser()
class MWError(Exception): class MWError(Exception):
pass pass
def send(message, name, avatar):
dictionary_creator = {"content": message}
if name:
dictionary_creator["username"] = name
if avatar:
dictionary_creator["avatar_url"] = avatar
send_to_discord(dictionary_creator)
def profile_field_name(name, embed): def profile_field_name(name, embed):
try: try:
return profile_fields[name] return profile_fields[name]
@ -1175,7 +1165,7 @@ class Recent_Changes_Class(object):
self.streak += 1 self.streak += 1
if self.streak > 8: if self.streak > 8:
self.streak = -1 self.streak = -1
send(_("Connection to {wiki} seems to be stable now.").format(wiki=settings["wikiname"]), send_simple("down_detector", _("Connection to {wiki} seems to be stable now.").format(wiki=settings["wikiname"]),
_("Connection status"), settings["avatars"]["connection_restored"]) _("Connection status"), settings["avatars"]["connection_restored"])
# In the first for loop we analize the categorize events and figure if we will need more changes to fetch # In the first for loop we analize the categorize events and figure if we will need more changes to fetch
# in order to cover all of the edits # in order to cover all of the edits
@ -1290,7 +1280,7 @@ class Recent_Changes_Class(object):
else: else:
if ( if (
time.time() - self.last_downtime) > 1800 and self.check_connection(): # check if last downtime happened within 30 minutes, if yes, don't send a message time.time() - self.last_downtime) > 1800 and self.check_connection(): # check if last downtime happened within 30 minutes, if yes, don't send a message
send(_("{wiki} seems to be down or unreachable.").format(wiki=settings["wikiname"]), send_simple("down_detector", _("{wiki} seems to be down or unreachable.").format(wiki=settings["wikiname"]),
_("Connection status"), settings["avatars"]["connection_failed"]) _("Connection status"), settings["avatars"]["connection_failed"])
self.last_downtime = time.time() self.last_downtime = time.time()
self.streak = 0 self.streak = 0