Files
bot.py
requirements.txt
Code
2 83
bot.py
import logging
import math
import os
import discord
from discord import app_commands
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("sparx_bot")
bot = commands.Bot(command_prefix="!", intents=discord.Intents.default())
@bot.event
async def on_ready():
try:
await bot.tree.sync()
except discord.HTTPException:
logger.exception("Could not sync slash commands")
logger.info("Logged in as %s", bot.user)
print("KODARI_READY", flush=True)
@bot.tree.command(name="wheel", description="Calculate distance and rotations for two wheels")
@app_commands.describe(
big_diameter="Diameter of the big wheel in metres",
small_diameter="Diameter of the small wheel in metres",
big_rotations="Number of rotations of the big wheel",
)
@app_commands.checks.cooldown(3, 10.0, key=lambda interaction: interaction.user.id)
async def wheel(
interaction: discord.Interaction,
big_diameter: app_commands.Range[float, 0.001, 1000000],
small_diameter: app_commands.Range[float, 0.001, 1000000],
big_rotations: app_commands.Range[float, 0, 1000000],
):
distance = math.pi * big_diameter * big_rotations
rotations = big_diameter * big_rotations / small_diameter
embed = discord.Embed(title="Wheel calculation", colour=discord.Colour.blue())
embed.add_field(name="Distance", value=f"{distance:.1f} m", inline=False)
embed.add_field(name="Complete small-wheel rotations", value=str(math.floor(rotations)), inline=False)
embed.add_field(
name="Working",
value=(f"Distance = π × {big_diameter:g} × {big_rotations:g}\n"
f"Small-wheel rotations = {big_diameter:g} × {big_rotations:g} ÷ {small_diameter:g} = {rotations:g}"),
inline=False,
)
await interaction.response.send_message(embed=embed)
@bot.tree.command(name="sparx", description="Show the current Sparx automation status")
async def sparx(interaction: discord.Interaction):
await interaction.response.send_message(
"Automatic Sparx login and homework completion are not implemented yet. "
"Never send your password or verification codes to this bot. "
"Use /wheel to calculate wheel questions in the meantime.",
ephemeral=True,
)
@bot.tree.error
async def on_app_command_error(interaction: discord.Interaction, error: app_commands.AppCommandError):
if isinstance(error, app_commands.CommandOnCooldown):
message = f"Please wait {error.retry_after:.1f} seconds before trying again."
else:
logger.error("Slash command failed", exc_info=(type(error), error, error.__traceback__))
message = "Something went wrong while running that command."
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 or token == "your_discord_bot_token_here":
raise RuntimeError("Set DISCORD_TOKEN in your .env file before starting the bot.")
bot.run(token)
