Skip to content

Commit

Permalink
Refactor set_broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
cemathey committed May 21, 2024
1 parent 82adedc commit 71eb083
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion rcon/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ def set_vip_slots_num(self, num):
return self._str_request(f"setnumvipslots {num}", log_info=True)

@_escape_params
def set_broadcast(self, message):
def set_broadcast(self, message: str):
return self._str_request(
f'broadcast "{message}"', log_info=True, can_fail=False
)
Expand Down
21 changes: 9 additions & 12 deletions rcon/rcon.py
Original file line number Diff line number Diff line change
Expand Up @@ -983,27 +983,24 @@ def get_broadcast_message(self) -> str | bytes | None:
return msg.decode()
return msg

def set_broadcast(self, message, save=True) -> str:
def set_broadcast(self, message: str) -> str:
"""Set the in game broadcast message and return the previous message if set"""
from rcon.broadcast import format_message

prev: bytes | None = None
try:
formatted = format_message(self, message)
except Exception:
logger.exception("Unable to format message")
formatted = message

prev: bytes | None = None
try:
red = get_redis_client()
if save:
prev = red.set("BROADCAST_MESSAGE", message, get=True) # type: ignore
else:
prev = red.get("BROADCAST_MESSAGE") # type: ignore
prev = red.set("BROADCAST_MESSAGE", message, get=True) # type: ignore
red.expire("BROADCAST_MESSAGE", 60 * 30)
except Exception:
logger.exception("Can't save message in redis: %s", message)

try:
formatted = format_message(self, message)
except Exception:
logger.exception("Unable to format message")
formatted = message

super().set_broadcast(formatted)
return prev.decode() if prev else ""

Expand Down

0 comments on commit 71eb083

Please sign in to comment.