Compare commits

...

7 Commits

Author SHA1 Message Date
Fabian Dill
bede173277 Merge branch 'main' into multiserver_discord_webhook 2025-05-14 20:00:32 +02:00
Fabian Dill
4a7232c6f3 Merge branch 'main' into multiserver_discord_webhook 2024-10-18 00:35:06 +02:00
Fabian Dill
3ad7f55d6b Merge branch 'main' into multiserver_discord_webhook 2024-02-20 01:19:38 +01:00
Fabian Dill
342093c510 Merge branch 'main' into multiserver_discord_webhook 2024-01-05 21:30:29 +01:00
Fabian Dill
609cb22c91 Update MultiServer.py 2024-01-04 11:22:00 +01:00
Fabian Dill
0607051718 remove async 2024-01-03 22:14:35 +01:00
Fabian Dill
61fd11b351 MultiServer: add discord webhook support 2024-01-03 22:13:53 +01:00
2 changed files with 70 additions and 1 deletions

View File

@@ -2368,7 +2368,6 @@ class ServerCommandProcessor(CommonCommandProcessor):
known_options = (f"{option}: {option_type}" for option, option_type in self.ctx.simple_options.items())
self.output(f"Unrecognized option '{option_name}', known: {', '.join(known_options)}")
return False
if value_type == bool:
def value_type(input_text: str):
return input_text.lower() not in {"off", "0", "false", "none", "null", "no"}
@@ -2402,6 +2401,75 @@ class ServerCommandProcessor(CommonCommandProcessor):
f"approximately totaling {Utils.format_SI_prefix(total, power=1024)}B")
self.output("\n".join(texts))
def _cmd_discord_webhook(self, webhook_url: str):
"""Needs to be supplied with a Discord WebHook url as parameter,
which will then relay the server log to a discord channel."""
import discord_webhook
initial_response = discord_webhook.DiscordWebhook(webhook_url, wait=True,
content="Beginning Discord Logging").execute()
if initial_response.ok:
import queue
response_queue = queue.SimpleQueue()
class Emitter(threading.Thread):
def run(self):
record: typing.Optional[logging.LogRecord] = None
while True:
time.sleep(1)
# check for leftover record from last iteration
message = record.msg if record else ""
while 1:
try:
record = response_queue.get_nowait()
except queue.Empty:
break
else:
if record is None:
return # shutdown
if len(record.msg) > 1999:
continue # content size limit
if len(message) + len(record.msg) > 2000:
break # reached content size limit in total
else:
message += "\n" + record.msg
record = None
if message:
try:
response = discord_webhook.DiscordWebhook(
webhook_url, rate_limit_retry=True, content=message.strip()).execute()
if response.status_code not in (200, 204):
shutdown()
logging.info(f"Disabled Discord WebHook due to error code {response.status_code}.")
return
# just in case to prevent an error-loop logging itself
except Exception as e:
shutdown()
logging.error("Disabled Discord WebHook due to error.")
logging.exception(e)
return
emitter = Emitter()
emitter.daemon = True
emitter.start()
class DiscordLogger(logging.Handler):
"""Logs to Discord WebHook"""
def emit(self, record: logging.LogRecord):
response_queue.put(record)
handler = DiscordLogger()
def shutdown():
response_queue.put(None)
logging.getLogger().removeHandler(handler)
logging.getLogger().addHandler(handler)
self.output("Discord Link established.")
else:
self.output("Discord Link could not be established. Check your webhook url.")
async def console(ctx: Context):
import sys

View File

@@ -11,6 +11,7 @@ certifi>=2025.4.26
cython>=3.0.12
cymem>=2.0.11
orjson>=3.10.15
discord-webhook>=1.3.0
typing_extensions>=4.12.2
pyshortcuts>=1.9.1
kivymd @ git+https://github.com/kivymd/KivyMD@5ff9d0d