Get rid of magic strings in triggers code.

This commit is contained in:
Jennifer Taylor
2021-08-18 19:14:02 -04:00
parent d60fb8de17
commit 6efd07d02f
9 changed files with 176 additions and 132 deletions
+24 -10
View File
@@ -1,8 +1,9 @@
from bemani.common.constants import GameConstants
from bemani.data.types import Song
from typing import Any, Dict
from discord_webhook import DiscordWebhook, DiscordEmbed # type: ignore
from datetime import datetime
from discord_webhook import DiscordWebhook, DiscordEmbed # type: ignore
from typing import Any, Dict
from bemani.common.constants import GameConstants, BroadcastConstants
from bemani.data.types import Song
class Triggers:
@@ -12,26 +13,39 @@ class Triggers:
def __init__(self, config: Dict[str, Any]):
self.config = config
def broadcast_score(self, data: Dict[str, str], game: str, song: Song) -> None:
def __gameconst_to_series(self, game: str) -> str:
return {
GameConstants.BISHI_BASHI: 'Bishi Bashi',
GameConstants.DANCE_EVOLUTION: 'Dance Evolution',
GameConstants.DDR: 'Dance Dance Revolution',
GameConstants.IIDX: 'Beatmania IIDX',
GameConstants.JUBEAT: 'Jubeat',
GameConstants.MUSECA: 'MÚSECA',
GameConstants.POPN_MUSIC: 'Pop\'n Music',
GameConstants.REFLEC_BEAT: 'Reflec Beat',
GameConstants.SDVX: 'Sound Voltex',
}.get(game, 'Unknown')
def broadcast_score(self, data: Dict[BroadcastConstants, str], game: str, song: Song) -> None:
# For now we only support discord
if self.config.get('webhooks', {}).get('discord', {}).get(game, None) is not None:
self.broadcast_score_discord(data, game, song)
def broadcast_score_discord(self, data: Dict[str, str], game: str, song: Song) -> None:
def broadcast_score_discord(self, data: Dict[BroadcastConstants, str], game: str, song: Song) -> None:
if game == GameConstants.IIDX:
now = datetime.now()
webhook = DiscordWebhook(url=self.config['webhooks']['discord'][game])
scoreembed = DiscordEmbed(title=f'New {game} Score!', color='fbba08')
scoreembed = DiscordEmbed(title=f'New {self.__gameconst_to_series(game)} Score!', color='fbba08')
scoreembed.set_footer(text=(now.strftime('Score was recorded on %m/%d/%y at %H:%M:%S')))
# lets give it an author
song_url = f"{self.config['server']['uri']}/{game}/topscores/{song.id}" if self.config['server']['uri'] is not None else None
scoreembed.set_author(name=self.config['name'], url=song_url)
for item in data:
for item, value in data.items():
inline = True
if item in ['DJ Name', 'Song', 'Artist', 'Play Stats']:
if item in {BroadcastConstants.DJ_NAME, BroadcastConstants.SONG_NAME, BroadcastConstants.ARTIST_NAME, BroadcastConstants.PLAY_STATS_HEADER}:
inline = False
scoreembed.add_embed_field(name=item, value=data[item], inline=inline)
scoreembed.add_embed_field(name=item.value, value=value, inline=inline)
webhook.add_embed(scoreembed)
webhook.execute()