forked from mirror/Archipelago
Compare commits
7 Commits
0.6.2-rc2
...
multiserve
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bede173277 | ||
|
|
4a7232c6f3 | ||
|
|
3ad7f55d6b | ||
|
|
342093c510 | ||
|
|
609cb22c91 | ||
|
|
0607051718 | ||
|
|
61fd11b351 |
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user