برودكاست

Posted by user_j3cag2 in discord-bots.

import discord from discord import app commands from discord.ext import commands import os import json import asyncio TOKEN = os.environ["TOKEN"] OWNER ID = int(os.environ["OWNER ID"]) intents = discord.Intents.default() bot = commands.Bot(command prefix="!", intents=intents) FILE = "subscribers.json" def load subscribers(): if os.path.exists(FILE): with open(FILE, "r") as f: return set(json.load(f)) return set() subscribers = load subscribers() def save subscribers(): with open(FILE, "w") as f: json.dump(list(subscribers), f) @bot.event async def on ready(): await bot.tree.sync() print(f"Logged in as {bot.user}") @bot.tree.command( name="subscribe", description="اشترك باستقبال إعلانات السيرفر" ) async def subscribe(interaction: discord.Interaction): subscribers.add(interaction.user.id) save subscribers() await interaction.response.send message( "تم اشتراكك باستقبال الإعلانات الخاصة.", ephemeral=True ) @bot.tree.command( name="unsubscribe", description="إلغاء استقبال الإعلانات" ) async def unsubscribe(interaction: discord.Interaction): subscribers.discard(interaction.user.id) save subscribers() await interaction.response.send message( "تم إلغاء اشتراكك بنجاح.", ephemeral=True ) @bot.tree.command( name="bc", description="إرسال برودكاست للمشتركين" ) @app commands.describe(message="نص الإعلان") async def bc(interaction: discord.Interaction, message: str): if interaction.user.id != OWNER ID: await interaction.response.send message( "هذا الأمر خاص بمالك البوت.", ephemeral=True ) return await interaction.response.send message( "بدأ إرسال الإعلان للمشتركين.", ephemeral=True ) sent = 0 failed = 0 for user id in list(subscribers): try: user = await bot.fetch user(user id) await user.send(message) sent += 1 await asyncio.sleep(2) except discord.HTTPException: failed += 1 except Exception: failed += 1 await interaction.followup.send( f"تم الإرسال: {sent}\n" f"تعذر الإرسال: {failed}", ephemeral=True ) bot.run(TOKEN)

Wispbyte Community Forum