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.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
import time
|
||||||
|
|
||||||
from twitchAPI.chat import ChatMessage
|
from twitchAPI.chat import ChatMessage
|
||||||
|
|
||||||
@@ -8,6 +9,8 @@ from protondb import searhProtonDb
|
|||||||
from twitchbot import _user_has_twitch_permission
|
from twitchbot import _user_has_twitch_permission
|
||||||
from webapp import webapp
|
from webapp import webapp
|
||||||
|
|
||||||
|
_last_used: float = 0.0
|
||||||
|
|
||||||
TIER_ICONS = {
|
TIER_ICONS = {
|
||||||
'platinum': '✅ Platinum',
|
'platinum': '✅ Platinum',
|
||||||
'gold': '🥇 Gold',
|
'gold': '🥇 Gold',
|
||||||
@@ -49,14 +52,24 @@ def _format_game_response(game: dict) -> str:
|
|||||||
|
|
||||||
|
|
||||||
async def protondb_command(msg: ChatMessage):
|
async def protondb_command(msg: ChatMessage):
|
||||||
|
global _last_used
|
||||||
with webapp.app_context():
|
with webapp.app_context():
|
||||||
if not ConfigurationHelper().getValue('proton_db_twitch_enable'):
|
if not ConfigurationHelper().getValue('proton_db_twitch_enable'):
|
||||||
return
|
return
|
||||||
permission = ConfigurationHelper().getValue('proton_db_twitch_permission') or 'viewer'
|
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):
|
if not _user_has_twitch_permission(msg, permission):
|
||||||
return
|
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
|
text = msg.text
|
||||||
for prefix in ('!protondb', '!pdb'):
|
for prefix in ('!protondb', '!pdb'):
|
||||||
if text.lower().startswith(prefix):
|
if text.lower().startswith(prefix):
|
||||||
|
|||||||
@@ -110,18 +110,32 @@
|
|||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div class="grid grid-cols-1 sm:grid-cols-2 gap-6">
|
||||||
<label for="proton_db_twitch_permission" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
|
<div>
|
||||||
Permission minimale pour <code class="px-1 py-0.5 bg-gray-200 dark:bg-gray-600 rounded text-xs">!pdb</code> sur Twitch
|
<label for="proton_db_twitch_permission" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
|
||||||
</label>
|
Permission minimale pour <code class="px-1 py-0.5 bg-gray-200 dark:bg-gray-600 rounded text-xs">!pdb</code> sur Twitch
|
||||||
<select name="proton_db_twitch_permission" id="proton_db_twitch_permission"
|
</label>
|
||||||
class="w-full sm:w-48 px-4 py-2 rounded-lg border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:ring-2 focus:ring-purple-500 focus:border-transparent transition-all">
|
<select name="proton_db_twitch_permission" id="proton_db_twitch_permission"
|
||||||
{% set current_perm = configuration.getValue('proton_db_twitch_permission') or 'viewer' %}
|
class="w-full px-4 py-2 rounded-lg border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:ring-2 focus:ring-purple-500 focus:border-transparent transition-all">
|
||||||
<option value="viewer" {% if current_perm == 'viewer' %}selected{% endif %}>👁️ Viewer (tout le monde)</option>
|
{% set current_perm = configuration.getValue('proton_db_twitch_permission') or 'viewer' %}
|
||||||
<option value="sub" {% if current_perm == 'sub' %}selected{% endif %}>⭐ Abonné (Sub)</option>
|
<option value="viewer" {% if current_perm == 'viewer' %}selected{% endif %}>👁️ Viewer (tout le monde)</option>
|
||||||
<option value="vip" {% if current_perm == 'vip' %}selected{% endif %}>💎 VIP</option>
|
<option value="sub" {% if current_perm == 'sub' %}selected{% endif %}>⭐ Abonné (Sub)</option>
|
||||||
<option value="moderator" {% if current_perm == 'moderator' %}selected{% endif %}>🛡️ Modérateur</option>
|
<option value="vip" {% if current_perm == 'vip' %}selected{% endif %}>💎 VIP</option>
|
||||||
</select>
|
<option value="moderator" {% if current_perm == 'moderator' %}selected{% endif %}>🛡️ Modérateur</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label for="proton_db_twitch_cooldown" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
|
||||||
|
Cooldown entre deux <code class="px-1 py-0.5 bg-gray-200 dark:bg-gray-600 rounded text-xs">!pdb</code> (secondes)
|
||||||
|
</label>
|
||||||
|
<input type="number" name="proton_db_twitch_cooldown" id="proton_db_twitch_cooldown"
|
||||||
|
min="0" max="3600" step="1"
|
||||||
|
value="{{ configuration.getValue('proton_db_twitch_cooldown') or 0 }}"
|
||||||
|
class="w-full px-4 py-2 rounded-lg border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:ring-2 focus:ring-purple-500 focus:border-transparent transition-all"
|
||||||
|
placeholder="0"/>
|
||||||
|
<p class="mt-1 text-xs text-gray-500 dark:text-gray-400">0 = pas de cooldown</p>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
|
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||||
|
|||||||
Reference in New Issue
Block a user