Files
bot.py
README.md
requirements.txt
Code
3 128
bot.py
import discord
from discord.ext import commands
from discord import app_commands
import os
from dotenv import load_dotenv
import asyncio
load_dotenv()
intents = discord.Intents.default()
intents.members = True
intents.guilds = True
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event
async def on_ready():
print(f'Bot is ready! Logged in as {bot.user}')
try:
synced = await bot.tree.sync()
print(f'Synced {len(synced)} command(s)')
except Exception as e:
print(f'Error syncing commands: {e}')
@bot.tree.command(name="announcedm", description="Send a DM announcement to all server members")
@app_commands.describe(message="The announcement message to send to all members")
@app_commands.default_permissions(administrator=True)
async def announcedm(interaction: discord.Interaction, message: str):
await interaction.response.defer(ephemeral=True)
guild = interaction.guild
if not guild:
await interaction.followup.send("This command can only be used in a server!", ephemeral=True)
return
members = [member for member in guild.members if not member.bot]
total_members = len(members)
success_count = 0
failed_count = 0
await interaction.followup.send(f"Starting to send announcements to {total_members} members...", ephemeral=True)
for member in members:
try:
dm_message = f"{member.mention}\n\n{message}"
await member.send(dm_message)
success_count += 1
await asyncio.sleep(1)
except discord.Forbidden:
failed_count += 1
except Exception as e:
print(f"Error sending DM to {member.name}: {e}")
failed_count += 1
summary = f"✅ Announcement sent!\n\n"
summary += f"Successfully delivered: {success_count}/{total_members}\n"
summary += f"Failed (DMs disabled): {failed_count}/{total_members}"
await interaction.followup.send(summary, ephemeral=True)
@bot.tree.command(name="announceuser", description="Send a DM announcement to a specific user")
@app_commands.describe(user="The user to send the announcement to", message="The announcement message")
@app_commands.default_permissions(administrator=True)
async def announceuser(interaction: discord.Interaction, user: discord.Member, message: str):
await interaction.response.defer(ephemeral=True)
if user.bot:
await interaction.followup.send("Cannot send announcements to bots!", ephemeral=True)
return
try:
dm_message = f"{user.mention}\n\n{message}"
await user.send(dm_message)
await interaction.followup.send(f"✅ Announcement sent to {user.mention} successfully!", ephemeral=True)
except discord.Forbidden:
await interaction.followup.send(f"❌ Failed to send DM to {user.mention}. They have DMs disabled.", ephemeral=True)
except Exception as e:
await interaction.followup.send(f"❌ Error sending DM: {str(e)}", ephemeral=True)
bot.run(os.getenv('DISCORD_TOKEN'))