69 lines
2.3 KiB
Python
69 lines
2.3 KiB
Python
|
|
import discord
|
||
|
|
from discord.ext import commands
|
||
|
|
from discord import app_commands
|
||
|
|
import json
|
||
|
|
import os
|
||
|
|
|
||
|
|
COUNTING_FILE = "counting_data.json"
|
||
|
|
counting_channels = {}
|
||
|
|
|
||
|
|
def load_counting_channels():
|
||
|
|
global counting_channels
|
||
|
|
if os.path.isfile(COUNTING_FILE):
|
||
|
|
with open(COUNTING_FILE, "r", encoding="utf-8") as f:
|
||
|
|
data = json.load(f)
|
||
|
|
counting_channels = {int(k): v for k, v in data.items()}
|
||
|
|
else:
|
||
|
|
counting_channels = {}
|
||
|
|
|
||
|
|
def save_counting_channels():
|
||
|
|
with open(COUNTING_FILE, "w", encoding="utf-8") as f:
|
||
|
|
json.dump({str(k): v for k, v in counting_channels.items()}, f, ensure_ascii=False, indent=2)
|
||
|
|
|
||
|
|
class Counting(commands.Cog):
|
||
|
|
def __init__(self, bot):
|
||
|
|
self.bot = bot
|
||
|
|
load_counting_channels()
|
||
|
|
|
||
|
|
async def cog_load(self):
|
||
|
|
# Synchronise les slash commands à l'ajout du cog
|
||
|
|
await self.bot.tree.sync()
|
||
|
|
print("Slash commands synchronisées pour Counting.")
|
||
|
|
|
||
|
|
@app_commands.command(name="start_counting", description="Démarre un channel de counting.")
|
||
|
|
async def start_counting(self, interaction: discord.Interaction):
|
||
|
|
counting_channels[interaction.channel.id] = {"current": 0, "last_user": None}
|
||
|
|
save_counting_channels()
|
||
|
|
await interaction.response.send_message("Le counting commence ! Envoyez 1 pour débuter.")
|
||
|
|
|
||
|
|
@commands.Cog.listener()
|
||
|
|
async def on_message(self, message):
|
||
|
|
if message.author.bot:
|
||
|
|
return
|
||
|
|
|
||
|
|
channel_id = message.channel.id
|
||
|
|
if channel_id not in counting_channels:
|
||
|
|
return
|
||
|
|
|
||
|
|
state = counting_channels[channel_id]
|
||
|
|
try:
|
||
|
|
number = int(message.content.strip())
|
||
|
|
except ValueError:
|
||
|
|
return
|
||
|
|
|
||
|
|
if number == state["current"] + 1 and message.author.id != state["last_user"]:
|
||
|
|
state["current"] = number
|
||
|
|
state["last_user"] = message.author.id
|
||
|
|
save_counting_channels()
|
||
|
|
await message.add_reaction("✅")
|
||
|
|
else:
|
||
|
|
await message.add_reaction("❌")
|
||
|
|
await message.channel.send(
|
||
|
|
f"Erreur ! <@{message.author.id}> a cassé la chaîne à {number}. On repart à 1 !"
|
||
|
|
)
|
||
|
|
counting_channels[channel_id] = {"current": 0, "last_user": None}
|
||
|
|
save_counting_channels()
|
||
|
|
|
||
|
|
async def setup(bot):
|
||
|
|
await bot.add_cog(Counting(bot))
|