Skip to content

Commit

Permalink
Add ability for gamemodes to override message keys
Browse files Browse the repository at this point in the history
Boreal now uses this to suppress the message telling wolf shamans to use
!kill, since wolf shamans are never able to !kill in that mode.
  • Loading branch information
skizzerz committed Oct 8, 2023
1 parent cc94c57 commit 9e7e51e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ botconfig.*
/messages.json
/gamemodes/*.py
/roles/*.py
!/hooks
/hooks/
!/hooks/__init__.py

# Database files
*.sqlite3
Expand Down
5 changes: 5 additions & 0 deletions src/gamemodes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ def __init__(self, arg=""):
self.ROLE_GUIDE = {}

self.CUSTOM_SETTINGS = CustomSettings()
self.MESSAGE_OVERRIDES = {}

# Support all shamans and totems
# Listeners should add their custom totems with non-zero chances, and custom roles in evt.data["shaman_roles"]
Expand Down Expand Up @@ -267,6 +268,8 @@ def startup(self):
else:
for listener in listeners:
listener.install(event)
for key, override in self.MESSAGE_OVERRIDES.items():
messages.overrides[key] = override

def teardown(self):
for event, listeners in self.EVENTS.items():
Expand All @@ -275,6 +278,8 @@ def teardown(self):
else:
for listener in listeners:
listener.remove(event)
for key in self.MESSAGE_OVERRIDES:
del messages.overrides[key]

def can_vote_bot(self, var):
return False
Expand Down
6 changes: 6 additions & 0 deletions src/gamemodes/boreal.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ def __init__(self, arg=""):
"num_totems": EventListener(self.on_num_totems)
}

self.MESSAGE_OVERRIDES = {
# suppress the "you can !kill" line; wolf shamans get most of their info from shaman_notify
# and wolves can't kill in this mode
"wolf_shaman_notify": None
}

self.TOTEM_CHANCES = {totem: {} for totem in self.DEFAULT_TOTEM_CHANCES}
self.set_default_totem_chances()
for totem, roles in self.TOTEM_CHANCES.items():
Expand Down
14 changes: 10 additions & 4 deletions src/messages/_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
import os
from typing import Optional

from src import config
from src.messages.message import Message
Expand All @@ -16,12 +17,17 @@ def __init__(self, *, override=None):
else:
self.lang = config.Main.get("gameplay.language")
self.cache = {}
self.messages = {}
self.overrides = {}
self._load_messages()

def get(self, key, index=None):
if key not in self.messages:
raise KeyError("Key {0!r} does not exist! Add it to messages.json".format(key))
return Message(key, self.messages[key], index)
def get(self, key, index=None) -> Optional[Message]:
actual_key = self.overrides[key] if key in self.overrides else key
if actual_key is None:
return None
if actual_key not in self.messages:
raise KeyError("Key {0!r} does not exist! Add it to messages.json".format(actual_key))
return Message(actual_key, self.messages[actual_key], index)

__getitem__ = get

Expand Down
1 change: 1 addition & 0 deletions src/roles/helper/shamans.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from src.status import try_misdirection, try_protection, try_exchange, is_dying, add_dying
from src.dispatcher import MessageDispatcher
from src.users import User
from src.locations import move_player_home

#####################################################################################
########### ADDING CUSTOM TOTEMS AND SHAMAN ROLES TO YOUR BOT -- READ THIS ##########
Expand Down

0 comments on commit 9e7e51e

Please sign in to comment.