Files
.gitignore
bot.py
README.md
requirements.txt
Code
4 114
bot.py
import logging
import os
import discord
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("westkust-roleplay")
token = os.getenv("DISCORD_TOKEN")
game_link = os.getenv("ROBLOX_GAME_LINK")
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(
command_prefix="?",
intents=intents,
case_insensitive=True,
help_command=None,
)
@bot.event
async def on_ready() -> None:
if bot.user is None:
return
await bot.change_presence(
activity=discord.Game(name="Westkust Roleplay"),
)
logger.info("Logged in as %s (ID: %s)", bot.user, bot.user.id)
@bot.command(name="game")
@commands.cooldown(rate=1, per=5.0, type=commands.BucketType.user)
async def game(ctx: commands.Context) -> None:
"""Send the Westkust Roleplay Roblox game link."""
if not game_link:
await ctx.send("The Roblox game link has not been configured yet.")
return
await ctx.send(f"Play **Westkust Roleplay** on Roblox:\n{game_link}")
@bot.command(name="help")
async def help_command(ctx: commands.Context) -> None:
"""Show the bot's available commands."""
await ctx.send("**Westkust Roleplay commands**\n`?game` — Get the Roblox game link")
@bot.event
async def on_command_error(
ctx: commands.Context,
error: commands.CommandError,
) -> None:
if isinstance(error, commands.CommandOnCooldown):
await ctx.send(
f"Please wait {error.retry_after:.1f} seconds before using that command again."
)
return
if isinstance(error, commands.CommandNotFound):
return
logger.error("Command error in %s", ctx.command, exc_info=error)
await ctx.send("Something went wrong while running that command.")
if not token:
raise RuntimeError("DISCORD_TOKEN is missing from the environment.")
bot.run(token)