Files
bot.py
requirements.txt
Code
2 201
bot.py
import logging
import os
from datetime import datetime, timezone
from typing import Optional
import discord
from discord import app_commands
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
)
logger = logging.getLogger("kodari_bot")
intents = discord.Intents.default()
bot = commands.Bot(command_prefix="!", intents=intents)
@bot.event
async def on_ready():
try:
synced = await bot.tree.sync()
logger.info("Synced %s application command(s)", len(synced))
except discord.HTTPException:
logger.exception("Failed to sync application commands")
logger.info("Logged in as %s", bot.user)
print("KODARI_READY", flush=True)
@bot.tree.command(name="pager", description="Send a pager alert")
@app_commands.describe(
specialty="The specialty or team being paged",
room="The room or location",
urgency="The urgency level",
notes="Additional notes shown in Discord",
notify_role="Role to mention with the pager alert",
)
async def pager(
interaction: discord.Interaction,
specialty: str,
room: str,
urgency: str,
notes: Optional[str] = None,
notify_role: Optional[discord.Role] = None,
):
embed = discord.Embed(
title="📟 Pager Alert",
colour=discord.Colour.red(),
timestamp=datetime.now(timezone.utc),
)
embed.add_field(name="Specialty", value=specialty, inline=True)
embed.add_field(name="Room", value=room, inline=True)
embed.add_field(name="Urgency", value=urgency, inline=True)
if notes:
embed.add_field(name="Notes (Discord only)", value=notes, inline=False)
requester = interaction.user.display_name
embed.set_footer(text=f"Requested by {requester}")
content = notify_role.mention if notify_role else None
allowed_mentions = discord.AllowedMentions(roles=[notify_role] if notify_role else [])
await interaction.response.send_message(
content=content,
embed=embed,
allowed_mentions=allowed_mentions,
)
class SessionGroup(app_commands.Group, name="session", description="Post a session announcement"):
@app_commands.command(name="server-poll", description="Post a staff availability poll")
@app_commands.describe(host="Host to mention in the poll")
async def server_poll(
self,
interaction: discord.Interaction,
host: Optional[discord.Member] = None,
):
poll_embed = discord.Embed(
description=(
"We are preparing for a potential upcoming session and are currently checking staff availability. "
"If you’re interested in participating, please react below with your current status:\n\n"
"🍾 — ***Fully available and ready to join***\n\n"
"🧪 — ***Partially available, can join later***\n\n"
"🥂 — ***Unavailable at this time***\n\n"
"Your responses will assist leadership in determining if we have sufficient staffing to proceed. "
"A minimum of **2 available reactions** is required to begin.\n\n"
f"**Host:** {host.mention if host else ''}\n\n"
"**Once availability is confirmed, the Server Start-Up (SSU) will begin. "
"Thank you for your cooperation and continued dedication.**"
),
colour=discord.Colour.blurple(),
)
await interaction.response.send_message(
content="***Greetings @everyone ,***",
embed=poll_embed,
allowed_mentions=discord.AllowedMentions(
everyone=True,
users=[host] if host else [],
),
)
poll_post = await interaction.original_response()
try:
for emoji in ("🍾", "🧪", "🥂"):
await poll_post.add_reaction(emoji)
except discord.HTTPException:
logger.exception("Posted server poll but failed to add its reactions")
await interaction.followup.send(
"The server poll was posted, but I couldn't add its reactions. "
"Please check that the bot has permission to add reactions.",
ephemeral=True,
)
@app_commands.command(name="start", description="Announce a session start")
@app_commands.describe(
title="Session announcement title",
details="What members should know about the session",
start_time="When the session starts, for example: Friday at 7 PM",
notify_role="Role to mention in the announcement",
)
async def start(
self,
interaction: discord.Interaction,
title: str,
details: str,
start_time: Optional[str] = None,
notify_role: Optional[discord.Role] = None,
):
await self.send_announcement(interaction, "Session Start", title, details, start_time, notify_role)
@app_commands.command(name="end", description="Announce a session end")
@app_commands.describe(
title="Session announcement title",
details="What members should know about the session",
start_time="Optional time related to the announcement",
notify_role="Role to mention in the announcement",
)
async def end(
self,
interaction: discord.Interaction,
title: str,
details: str,
start_time: Optional[str] = None,
notify_role: Optional[discord.Role] = None,
):
await self.send_announcement(interaction, "Session End", title, details, start_time, notify_role)
async def send_announcement(
self,
interaction: discord.Interaction,
session_type: str,
title: str,
details: str,
start_time: Optional[str],
notify_role: Optional[discord.Role],
):
embed = discord.Embed(
title=title,
description=details,
colour=discord.Colour.blurple(),
timestamp=datetime.now(timezone.utc),
)
embed.add_field(name="Type", value=session_type, inline=False)
if start_time:
embed.add_field(name="Starts", value=start_time, inline=False)
embed.set_footer(text=f"Session announced by {interaction.user.display_name}")
content = notify_role.mention if notify_role else None
allowed_mentions = discord.AllowedMentions(roles=[notify_role] if notify_role else [])
await interaction.response.send_message(
content=content,
embed=embed,
allowed_mentions=allowed_mentions,
)
bot.tree.add_command(SessionGroup())
@bot.tree.error
async def on_app_command_error(
interaction: discord.Interaction,
error: app_commands.AppCommandError,
):
logger.error("Application command failed", exc_info=error)
message = "Something went wrong while running that command. Please try again."
if interaction.response.is_done():
await interaction.followup.send(message, ephemeral=True)
else:
await interaction.response.send_message(message, ephemeral=True)
token = os.getenv("DISCORD_TOKEN")
if not token:
raise RuntimeError("DISCORD_TOKEN is missing. Add it to your .env file.")
bot.run(token)