Add roles

This commit is contained in:
2024-11-06 19:42:31 +01:00
parent 7adfea42d6
commit 06ba8bec34
4 changed files with 61 additions and 16 deletions

View File

@@ -2,16 +2,19 @@
id = 59900845 id = 59900845
name = "hafmc" name = "hafmc"
[notifications] [notifications]
start_stream = ''' start_stream = '''
HafMC сейчас стримит {title} ({category})! HafMC стримит {title} ({category})!
Присоединяйся: https://twitch.tv/hafmc Присоединяйся: https://twitch.tv/hafmc
{role}
''' '''
change_category = ''' change_category = '''
HafMC начал играть в {category}! HafMC играет в {category} ({title})!
Присоединяйся: https://twitch.tv/hafmc Присоединяйся: https://twitch.tv/hafmc
{role}
''' '''
@@ -25,5 +28,16 @@ notifications_channel_id = 1198296540964475002
channel_id = 1201810638800691210 channel_id = 1201810638800691210
message_id = 1239664178038313012 message_id = 1239664178038313012
[integrations.discord.roles]
"Counter-Strike" = 1198372462476398823
"SIGame" = 1198372523046346833
"Battlefield 1" = 1198377604533735454
"Battlefield V" = 1198377604533735454
"Among Us" = 1198377604533735454
"BattleBit Remastered" = 1198381867343286352
"Lethal Company" = 1198383849785270302
"HELLDIVERS 2" = 1230292196805054488
"Satisfactory" = 1292091826802528308
[integrations.telegram] [integrations.telegram]
notifications_channel_id = -1001939021131 notifications_channel_id = -1001939021131

View File

@@ -21,6 +21,7 @@ class DiscordConfig(BaseModel):
guild_id: int guild_id: int
notifications_channel_id: int notifications_channel_id: int
games_list: GamesListConfig | None = None games_list: GamesListConfig | None = None
roles: dict[str, int] | None = None
class TelegramConfig(BaseModel): class TelegramConfig(BaseModel):
notifications_channel_id: int notifications_channel_id: int

View File

@@ -1,8 +1,10 @@
import logging import logging
from typing import Literal
from httpx import AsyncClient from httpx import AsyncClient
from config import config, StreamerConfig from config import config, StreamerConfig
from services.twitch import State
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -32,11 +34,43 @@ async def notify_discord(msg: str, channel_id: str):
) )
async def notify(msg: str, streamer_config: StreamerConfig): def get_role_id(streamer_config: StreamerConfig, category: str) -> int | None:
discord_integration = streamer_config.integrations.discord
if discord_integration is None:
return None
roles= discord_integration.roles
if roles is None:
return None
return roles.get(category)
async def notify(notification_type: Literal["start"] | Literal["change_category"], streamer_config: StreamerConfig, current_state: State):
if notification_type == "start":
message_template = streamer_config.notifications.start_stream
else:
message_template = streamer_config.notifications.change_category
if message_template is None:
return
integrations = streamer_config.integrations integrations = streamer_config.integrations
if (discord := integrations.discord) is not None: if (discord := integrations.discord) is not None:
if discord.notifications_channel_id is not None: if discord.notifications_channel_id is not None:
role_id = get_role_id(streamer_config, current_state.category)
if role_id is not None:
role = f"<@&{role_id}>"
else:
role = ""
msg = message_template.format(
title=current_state.title,
category=current_state.category,
role=role
)
try: try:
await notify_discord(msg, str(discord.notifications_channel_id)) await notify_discord(msg, str(discord.notifications_channel_id))
except Exception as e: except Exception as e:
@@ -44,6 +78,12 @@ async def notify(msg: str, streamer_config: StreamerConfig):
if (telegram := integrations.telegram) is not None: if (telegram := integrations.telegram) is not None:
if telegram.notifications_channel_id is not None: if telegram.notifications_channel_id is not None:
msg = message_template.format(
title=current_state.title,
category=current_state.category,
role=""
)
try: try:
await notify_telegram(msg, str(telegram.notifications_channel_id)) await notify_telegram(msg, str(telegram.notifications_channel_id))
except Exception as e: except Exception as e:

View File

@@ -95,12 +95,7 @@ class TwitchService:
if streamer.notifications.start_stream is None: if streamer.notifications.start_stream is None:
return return
msg = streamer.notifications.start_stream.format( await notify("start", streamer, current_state)
title=current_state.title,
category=current_state.category
)
await notify(msg, streamer)
async def notify_change_category(self, streamer_id: int): async def notify_change_category(self, streamer_id: int):
current_state = self.state.get(streamer_id) current_state = self.state.get(streamer_id)
@@ -116,12 +111,7 @@ class TwitchService:
if streamer.notifications.change_category is None: if streamer.notifications.change_category is None:
return return
msg = streamer.notifications.change_category.format( await notify("change_category", streamer, current_state)
title=current_state.title,
category=current_state.category
)
await notify(msg, streamer)
async def get_current_stream(self, streamer_id: int, retry_count: int = 5, delay: int = 5): async def get_current_stream(self, streamer_id: int, retry_count: int = 5, delay: int = 5):
remain_retry = retry_count remain_retry = retry_count