File size: 2,512 Bytes
6f39b38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
/poll <question> | <option1> | <option2> | ... [| key=value ...]

Create a poll in the group. Only admins/owner can use this command.
Supports all Pyrogram send_poll options via key=value pairs after the options.
Example:
/poll What's your favorite color? | Red | Blue | Green | is_anonymous=False | allows_multiple_answers=True
"""

from pyrogram import filters
from pyrogram.types import Message
from DragMusic import app

async def is_admin(client, chat_id, user_id):
    member = await client.get_chat_member(chat_id, user_id)
    return member.status in ("administrator", "creator")

@app.on_message(filters.command("poll", prefixes=["/", "!"]) & filters.group)
async def poll_command(client, message: Message):
    if not await is_admin(client, message.chat.id, message.from_user.id):
        return await message.reply_text("Only group admins and the owner can use this command.")
    if len(message.command) == 1:
        return await message.reply_text(
            "Usage:\n/poll <question> | <option1> | <option2> | ... [| key=value ...]\n"
            "Example:\n/poll What's your favorite color? | Red | Blue | Green | is_anonymous=False | allows_multiple_answers=True"
        )
    # Join the rest of the message and split by |
    parts = message.text.split(None, 1)[1].split("|")
    parts = [p.strip() for p in parts if p.strip()]
    if len(parts) < 3:
        return await message.reply_text("You must provide a question and at least two options.")
    question = parts[0]
    options = []
    kwargs = {}
    for part in parts[1:]:
        if "=" in part:
            key, value = part.split("=", 1)
            key = key.strip()
            value = value.strip()
            # Convert value to correct type
            if value.lower() == "true":
                value = True
            elif value.lower() == "false":
                value = False
            elif value.isdigit():
                value = int(value)
            kwargs[key] = value
        else:
            options.append(part)
    if len(options) < 2:
        return await message.reply_text("You must provide at least two options.")
    try:
        poll_msg = await app.send_poll(
            chat_id=message.chat.id,
            question=question,
            options=options,
            **kwargs
        )
        await message.reply_text(f"Poll created: [Jump to poll]({poll_msg.link})", disable_web_page_preview=True)
    except Exception as e:
        await message.reply_text(f"Failed to create poll: {e}")