|
| 1 | +import discord |
| 2 | +from discord import app_commands |
| 3 | +from discord.ext import commands |
| 4 | + |
| 5 | +from database.mongo import get_counting_state, update_counting_state |
| 6 | +from features.config import ( |
| 7 | + ADMIN_ROLE_ID, |
| 8 | + COUNTING_CHANNEL_ID, |
| 9 | + FOUNDER_ROLE_ID, |
| 10 | + MODERATOR_ROLE_ID, |
| 11 | +) |
| 12 | + |
| 13 | +COUNTING_MOD_ROLES = {MODERATOR_ROLE_ID, ADMIN_ROLE_ID, FOUNDER_ROLE_ID} |
| 14 | + |
| 15 | + |
| 16 | +class Counting(commands.Cog): |
| 17 | + def __init__(self, bot): |
| 18 | + self.bot = bot |
| 19 | + |
| 20 | + @commands.Cog.listener() |
| 21 | + async def on_message(self, message: discord.Message): |
| 22 | + if message.author.bot: |
| 23 | + return |
| 24 | + if not message.guild: |
| 25 | + return |
| 26 | + if message.channel.id != COUNTING_CHANNEL_ID: |
| 27 | + return |
| 28 | + |
| 29 | + state = await get_counting_state() |
| 30 | + current_count = state["current_count"] |
| 31 | + last_user_id = state["last_user_id"] |
| 32 | + expected = current_count + 1 |
| 33 | + |
| 34 | + if message.author.id == last_user_id: |
| 35 | + await message.delete() |
| 36 | + await message.channel.send( |
| 37 | + f"{message.author.mention} You can't count twice in a row!", |
| 38 | + delete_after=5, |
| 39 | + ) |
| 40 | + return |
| 41 | + |
| 42 | + try: |
| 43 | + number = int(message.content.strip()) |
| 44 | + except ValueError: |
| 45 | + await message.delete() |
| 46 | + await message.channel.send( |
| 47 | + f"{message.author.mention} That's not a number! Next number is **{expected}**.", |
| 48 | + delete_after=5, |
| 49 | + ) |
| 50 | + return |
| 51 | + |
| 52 | + if number != expected: |
| 53 | + await message.delete() |
| 54 | + await message.channel.send( |
| 55 | + f"{message.author.mention} Wrong number! Next number is **{expected}**.", |
| 56 | + delete_after=5, |
| 57 | + ) |
| 58 | + return |
| 59 | + |
| 60 | + await update_counting_state(number, message.author.id) |
| 61 | + |
| 62 | + @app_commands.command( |
| 63 | + name="set-count", description="Set the current count (staff only)." |
| 64 | + ) |
| 65 | + @app_commands.describe(count="The number to set the count to") |
| 66 | + async def set_count(self, interaction: discord.Interaction, count: int): |
| 67 | + if not isinstance(interaction.user, discord.Member) or not any( |
| 68 | + role.id in COUNTING_MOD_ROLES for role in interaction.user.roles |
| 69 | + ): |
| 70 | + await interaction.response.send_message( |
| 71 | + "❌ You don't have permission to use this command.", ephemeral=True |
| 72 | + ) |
| 73 | + return |
| 74 | + |
| 75 | + if interaction.channel_id != COUNTING_CHANNEL_ID: |
| 76 | + await interaction.response.send_message( |
| 77 | + f"❌ This command can only be used in <#{COUNTING_CHANNEL_ID}>.", |
| 78 | + ephemeral=True, |
| 79 | + ) |
| 80 | + return |
| 81 | + |
| 82 | + await update_counting_state(count, None) |
| 83 | + await interaction.response.send_message( |
| 84 | + f"✅ Count set to **{count}**.", ephemeral=True |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +async def setup(bot): |
| 89 | + await bot.add_cog(Counting(bot)) |
0 commit comments