Files
Mow910 fc274b47ae Implement cooldown feature for ProtonDB command in TwitchBot and update configuration options in webapp
- Added a cooldown mechanism to the ProtonDB command to prevent spamming.
- Introduced a new configuration option for setting the cooldown duration in seconds.
- Updated the ProtonDB template to include the new cooldown setting for Twitch users.
2026-03-24 00:21:02 +01:00

105 lines
2.7 KiB
Python

import asyncio
import logging
import time
from twitchAPI.chat import ChatMessage
from database.helpers import ConfigurationHelper
from protondb import searhProtonDb
from twitchbot import _user_has_twitch_permission
from webapp import webapp
_last_used: float = 0.0
TIER_ICONS = {
'platinum': '✅ Platinum',
'gold': '🥇 Gold',
'silver': '🥈 Silver',
'bronze': '🥉 Bronze',
'borked': '❌ Borked',
'native': '🐧 Native',
}
AC_ICONS = {
'supported': '✅',
'running': '⚠️',
'broken': '❌',
'denied': '🚫',
'planned': '📅',
}
def _format_game_response(game: dict) -> str:
name = game.get('name', '?')
tier = (game.get('tier') or '').lower()
tier_label = TIER_ICONS.get(tier, tier.capitalize() if tier else '?')
g_id = game.get('id', '')
parts = [f"[{name}] {tier_label}"]
ac_status = (game.get('anticheat_status') or '').lower()
if ac_status:
ac_icon = AC_ICONS.get(ac_status, '❔')
acs = game.get('anticheats') or []
ac_list = ', '.join(str(ac) for ac in acs if ac)
ac_part = f"Anti-cheat: {ac_icon} {ac_status.capitalize()}"
if ac_list:
ac_part += f" ({ac_list})"
parts.append(ac_part)
parts.append(f"protondb.com/app/{g_id}")
return ' | '.join(parts)
async def protondb_command(msg: ChatMessage):
global _last_used
with webapp.app_context():
if not ConfigurationHelper().getValue('proton_db_twitch_enable'):
return
permission = ConfigurationHelper().getValue('proton_db_twitch_permission') or 'viewer'
cooldown = int(ConfigurationHelper().getValue('proton_db_twitch_cooldown') or 0)
if not _user_has_twitch_permission(msg, permission):
return
if cooldown > 0:
elapsed = time.time() - _last_used
if elapsed < cooldown:
remaining = int(cooldown - elapsed)
await msg.reply(f"@{msg.user.name} La commande !pdb est en cooldown, réessaie dans {remaining}s.")
return
_last_used = time.time()
text = msg.text
for prefix in ('!protondb', '!pdb'):
if text.lower().startswith(prefix):
text = text[len(prefix):]
break
name = text.strip()
if not name:
await msg.reply(f"@{msg.user.name} Utilisation : !pdb <nom du jeu> Exemple : !pdb Elden Ring")
return
def _search():
with webapp.app_context():
return searhProtonDb(name)
try:
loop = asyncio.get_event_loop()
games = await loop.run_in_executor(None, _search)
except Exception as e:
logging.error(f'Erreur ProtonDB Twitch pour "{name}": {e}')
await msg.reply(f"@{msg.user.name} Erreur lors de la recherche ProtonDB.")
return
if not games:
await msg.reply(f"@{msg.user.name} Aucun jeu trouvé pour \"{name}\" sur Steam.")
return
for game in games[:3]:
response = _format_game_response(game)
if len(response) > 500:
response = response[:497] + '...'
await msg.reply(response)