mirror of
https://github.com/kurbezz/discord-bot.git
synced 2025-12-06 07:05:36 +01:00
Add roles
This commit is contained in:
@@ -2,16 +2,19 @@
|
||||
id = 59900845
|
||||
name = "hafmc"
|
||||
|
||||
|
||||
[notifications]
|
||||
start_stream = '''
|
||||
HafMC сейчас стримит {title} ({category})!
|
||||
HafMC стримит {title} ({category})!
|
||||
Присоединяйся: https://twitch.tv/hafmc
|
||||
|
||||
{role}
|
||||
'''
|
||||
|
||||
change_category = '''
|
||||
HafMC начал играть в {category}!
|
||||
HafMC играет в {category} ({title})!
|
||||
Присоединяйся: https://twitch.tv/hafmc
|
||||
|
||||
{role}
|
||||
'''
|
||||
|
||||
|
||||
@@ -25,5 +28,16 @@ notifications_channel_id = 1198296540964475002
|
||||
channel_id = 1201810638800691210
|
||||
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]
|
||||
notifications_channel_id = -1001939021131
|
||||
|
||||
@@ -21,6 +21,7 @@ class DiscordConfig(BaseModel):
|
||||
guild_id: int
|
||||
notifications_channel_id: int
|
||||
games_list: GamesListConfig | None = None
|
||||
roles: dict[str, int] | None = None
|
||||
|
||||
class TelegramConfig(BaseModel):
|
||||
notifications_channel_id: int
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import logging
|
||||
from typing import Literal
|
||||
|
||||
from httpx import AsyncClient
|
||||
|
||||
from config import config, StreamerConfig
|
||||
from services.twitch import State
|
||||
|
||||
|
||||
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
|
||||
|
||||
if (discord := integrations.discord) 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:
|
||||
await notify_discord(msg, str(discord.notifications_channel_id))
|
||||
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.notifications_channel_id is not None:
|
||||
msg = message_template.format(
|
||||
title=current_state.title,
|
||||
category=current_state.category,
|
||||
role=""
|
||||
)
|
||||
|
||||
try:
|
||||
await notify_telegram(msg, str(telegram.notifications_channel_id))
|
||||
except Exception as e:
|
||||
|
||||
@@ -95,12 +95,7 @@ class TwitchService:
|
||||
if streamer.notifications.start_stream is None:
|
||||
return
|
||||
|
||||
msg = streamer.notifications.start_stream.format(
|
||||
title=current_state.title,
|
||||
category=current_state.category
|
||||
)
|
||||
|
||||
await notify(msg, streamer)
|
||||
await notify("start", streamer, current_state)
|
||||
|
||||
async def notify_change_category(self, streamer_id: int):
|
||||
current_state = self.state.get(streamer_id)
|
||||
@@ -116,12 +111,7 @@ class TwitchService:
|
||||
if streamer.notifications.change_category is None:
|
||||
return
|
||||
|
||||
msg = streamer.notifications.change_category.format(
|
||||
title=current_state.title,
|
||||
category=current_state.category
|
||||
)
|
||||
|
||||
await notify(msg, streamer)
|
||||
await notify("change_category", streamer, current_state)
|
||||
|
||||
async def get_current_stream(self, streamer_id: int, retry_count: int = 5, delay: int = 5):
|
||||
remain_retry = retry_count
|
||||
|
||||
Reference in New Issue
Block a user