| import discord |
| from discord.ext import commands |
| from discord import app_commands |
| from datetime import datetime, timedelta |
| import asyncio |
| from database import Database |
|
|
| CYBER_BLUE = discord.Color.from_rgb(52, 152, 219) |
| CYBER_GOLD = discord.Color.from_rgb(241, 196, 15) |
|
|
| POLL_EMOJIS_MULTI = ["1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣", "9️⃣", "🔟"] |
| POLL_EMOJIS_YESNO = ["👍", "👎"] |
|
|
|
|
| class Poll(commands.Cog): |
| def __init__(self, bot): |
| self.bot = bot |
|
|
| @commands.hybrid_command(name="anket", description="Bir anket oluşturur.") |
| @app_commands.describe( |
| soru="Anket sorusu", |
| secenekler="Seçenekler | ile ayrılmış (en fazla 10). Boş = Evet/Hayır anketi", |
| sure_dakika="Anket süresi dakika cinsinden (boş = süresiz)" |
| ) |
| async def anket( |
| self, ctx: commands.Context, |
| soru: str, |
| secenekler: str = "", |
| sure_dakika: int = 0 |
| ): |
| if ctx.interaction: |
| await ctx.defer() |
|
|
| if secenekler: |
| opts = [s.strip() for s in secenekler.split(" | ") if s.strip()] |
| if len(opts) < 2: |
| await ctx.send("❌ En az 2 seçenek gereklidir. (Örn: A | B | C)", ephemeral=True) |
| return |
| if len(opts) > 10: |
| opts = opts[:10] |
|
|
| multi_emojis = POLL_EMOJIS_MULTI[:len(opts)] |
|
|
| embed = discord.Embed( |
| title="📊 Anket", |
| description=f"**{soru}**", |
| color=CYBER_BLUE, |
| timestamp=datetime.now() |
| ) |
| embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.display_avatar.url) |
| for i, opt in enumerate(opts): |
| embed.add_field(name=f"{multi_emojis[i]} Seçenek {i+1}", value=opt, inline=False) |
|
|
| if sure_dakika > 0: |
| bitis = datetime.now() + timedelta(minutes=sure_dakika) |
| embed.set_footer(text=f"Anket süresi: <t:{int(bitis.timestamp())}:R> | Başlatan: {ctx.author}") |
| else: |
| embed.set_footer(text=f"Başlatan: {ctx.author}") |
| else: |
| embed = discord.Embed( |
| title="📊 Anket", |
| description=f"**{soru}**", |
| color=CYBER_BLUE, |
| timestamp=datetime.now() |
| ) |
| embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.display_avatar.url) |
| embed.set_footer(text=f"👍 Evet | 👎 Hayır | Başlatan: {ctx.author}") |
|
|
| poll_msg = await ctx.send(embed=embed) |
|
|
| if secenekler: |
| for emoji in multi_emojis: |
| await poll_msg.add_reaction(emoji) |
| else: |
| await poll_msg.add_reaction("👍") |
| await poll_msg.add_reaction("👎") |
|
|
| if sure_dakika > 0: |
| await asyncio.sleep(sure_dakika * 60) |
| try: |
| final_msg = await ctx.channel.fetch_message(poll_msg.id) |
| results_desc = f"**Anket Süresi Doldu!**\n\n{soru}\n\n" |
| if secenekler: |
| for i, opt in enumerate(opts): |
| reaction_count = 0 |
| for r in final_msg.reactions: |
| if str(r.emoji) == multi_emojis[i]: |
| reaction_count = r.count - 1 |
| results_desc += f"{multi_emojis[i]} **{opt}**: `{reaction_count} oy`\n" |
| else: |
| yes_count = 0 |
| no_count = 0 |
| for r in final_msg.reactions: |
| if str(r.emoji) == "👍": |
| yes_count = r.count - 1 |
| elif str(r.emoji) == "👎": |
| no_count = r.count - 1 |
| results_desc += f"👍 **Evet**: `{yes_count} oy`\n👎 **Hayır**: `{no_count} oy`\n" |
|
|
| result_embed = discord.Embed( |
| title="📊 Anket Sonuçları", |
| description=results_desc, |
| color=CYBER_GOLD, |
| timestamp=datetime.now() |
| ) |
| await ctx.channel.send(embed=result_embed) |
| except: |
| pass |
|
|
|
|
| async def setup(bot): |
| await bot.add_cog(Poll(bot)) |
|
|