Files
bot.py
data_manager.py
duty_view.py
requirements.txt
Code
4 352
bot.py
import discord
from discord.ext import commands
import os
from dotenv import load_dotenv
from data_manager import get_panel_info, save_panel_info
from duty_view import DutyView
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
if not TOKEN:
raise ValueError("DISCORD_TOKEN not found in .env file")
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)
@bot.event
async def on_ready():
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
print(f" Servers: {len(bot.guilds)}")
print(f" Latency: {round(bot.latency * 1000)}ms")
bot.add_view(DutyView())
panel_info = get_panel_info()
if panel_info.get("channel_id") and panel_info.get("message_id"):
channel = bot.get_channel(panel_info["channel_id"])
if channel:
try:
message = await channel.fetch_message(panel_info["message_id"])
embed = DutyView.build_embed()
await message.edit(embed=embed, view=DutyView())
print(f" Updated existing duty panel in #{channel.name}")
except (discord.NotFound, discord.Forbidden):
print(" Previous panel message not found, will create new one on command")
@bot.command(name="duty", aliases=["panel"])
async def duty_panel(ctx: commands.Context):
"""Display the duty management panel with interactive buttons."""
await ctx.message.delete()
panel_info = get_panel_info()
if panel_info.get("channel_id") and panel_info.get("message_id"):
channel = bot.get_channel(panel_info["channel_id"])
if channel:
try:
message = await channel.fetch_message(panel_info["message_id"])
embed = DutyView.build_embed()
await message.edit(embed=embed, view=DutyView())
confirm = await ctx.send(
f"Duty panel updated in {channel.mention}", delete_after=5
)
return
except (discord.NotFound, discord.Forbidden):
pass
embed = DutyView.build_embed()
message = await ctx.send(embed=embed, view=DutyView())
save_panel_info(message.channel.id, message.id)
if __name__ == "__main__":
bot.run(TOKEN)