Files
.gitignore
bot.py
requirements.txt
Code
3 182
bot.py
import discord
from discord.ext import commands
import os
from dotenv import load_dotenv
from datetime import timedelta
load_dotenv()
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
bot = commands.Bot(command_prefix='.', intents=intents)
@bot.event
async def on_ready():
print(f'Bot is ready! Logged in as {bot.user}')
print(f'Bot ID: {bot.user.id}')
print('------')
@bot.command(name='announce')
@commands.has_permissions(administrator=True)
async def announce(ctx, channel: discord.TextChannel, *, message):
embed = discord.Embed(
title="๐ข Announcement",
description=message,
color=discord.Color.blue()
)
embed.set_footer(text=f"Announced by {ctx.author.name}", icon_url=ctx.author.avatar.url if ctx.author.avatar else None)
await channel.send(embed=embed)
await ctx.send(f"โ
Announcement sent to {channel.mention}")
@bot.command(name='poll')
@commands.has_permissions(manage_messages=True)
async def poll(ctx, question, *options):
if len(options) > 10:
await ctx.send("โ You can only have up to 10 options!")
return
if len(options) < 2:
await ctx.send("โ You need at least 2 options for a poll!")
return
emoji_numbers = ['1๏ธโฃ', '2๏ธโฃ', '3๏ธโฃ', '4๏ธโฃ', '5๏ธโฃ', '6๏ธโฃ', '7๏ธโฃ', '8๏ธโฃ', '9๏ธโฃ', '๐']
description = []
for i, option in enumerate(options):
description.append(f"{emoji_numbers[i]} {option}")
embed = discord.Embed(
title=f"๐ {question}",
description="\n\n".join(description),
color=discord.Color.green()
)
embed.set_footer(text=f"Poll by {ctx.author.name}", icon_url=ctx.author.avatar.url if ctx.author.avatar else None)
poll_message = await ctx.send(embed=embed)
for i in range(len(options)):
await poll_message.add_reaction(emoji_numbers[i])
await ctx.message.delete()
@bot.command(name='timeout')
@commands.has_permissions(moderate_members=True)
async def timeout(ctx, member: discord.Member, duration: int, unit: str = "m", *, reason: str = "No reason provided"):
if member.top_role >= ctx.author.top_role and ctx.author != ctx.guild.owner:
await ctx.send("โ You cannot timeout someone with a role equal to or higher than yours!")
return
if member == ctx.author:
await ctx.send("โ You cannot timeout yourself!")
return
time_units = {
's': 1,
'm': 60,
'h': 3600,
'd': 86400
}
if unit.lower() not in time_units:
await ctx.send("โ Invalid time unit! Use: s (seconds), m (minutes), h (hours), d (days)")
return
seconds = duration * time_units[unit.lower()]
if seconds > 2419200:
await ctx.send("โ Timeout duration cannot exceed 28 days!")
return
try:
await member.timeout(timedelta(seconds=seconds), reason=reason)
embed = discord.Embed(
title="โฑ๏ธ Member Timed Out",
color=discord.Color.orange()
)
embed.add_field(name="Member", value=f"{member.mention} ({member})", inline=False)
embed.add_field(name="Duration", value=f"{duration}{unit}", inline=True)
embed.add_field(name="Moderator", value=ctx.author.mention, inline=True)
embed.add_field(name="Reason", value=reason, inline=False)
await ctx.send(embed=embed)
except discord.Forbidden:
await ctx.send("โ I don't have permission to timeout this member!")
except Exception as e:
await ctx.send(f"โ An error occurred: {str(e)}")
@bot.command(name='kick')
@commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, reason: str = "No reason provided"):
if member.top_role >= ctx.author.top_role and ctx.author != ctx.guild.owner:
await ctx.send("โ You cannot kick someone with a role equal to or higher than yours!")
return
if member == ctx.author:
await ctx.send("โ You cannot kick yourself!")
return
try:
await member.kick(reason=reason)
embed = discord.Embed(
title="๐ข Member Kicked",
color=discord.Color.orange()
)
embed.add_field(name="Member", value=f"{member} ({member.id})", inline=False)
embed.add_field(name="Moderator", value=ctx.author.mention, inline=True)
embed.add_field(name="Reason", value=reason, inline=False)
await ctx.send(embed=embed)
except discord.Forbidden:
await ctx.send("โ I don't have permission to kick this member!")
except Exception as e:
await ctx.send(f"โ An error occurred: {str(e)}")
@bot.command(name='ban')
@commands.has_permissions(ban_members=True)
async def ban(ctx, member: discord.Member, *, reason: str = "No reason provided"):
if member.top_role >= ctx.author.top_role and ctx.author != ctx.guild.owner:
await ctx.send("โ You cannot ban someone with a role equal to or higher than yours!")
return
if member == ctx.author:
await ctx.send("โ You cannot ban yourself!")
return
try:
await member.ban(reason=reason)
embed = discord.Embed(
title="๐จ Member Banned",
color=discord.Color.red()
)
embed.add_field(name="Member", value=f"{member} ({member.id})", inline=False)
embed.add_field(name="Moderator", value=ctx.author.mention, inline=True)
embed.add_field(name="Reason", value=reason, inline=False)
await ctx.send(embed=embed)
except discord.Forbidden:
await ctx.send("โ I don't have permission to ban this member!")
except Exception as e:
await ctx.send(f"โ An error occurred: {str(e)}")
@announce.error
@poll.error
@timeout.error
@kick.error
@ban.error
async def command_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("โ You don't have permission to use this command!")
elif isinstance(error, commands.MissingRequiredArgument):
await ctx.send(f"โ Missing required argument: {error.param.name}")
elif isinstance(error, commands.BadArgument):
await ctx.send("โ Invalid argument provided!")
else:
await ctx.send(f"โ An error occurred: {str(error)}")
bot.run(os.getenv('DISCORD_TOKEN'))