| # bot.py
|
| # The main file for this bot. Imports required modules, sets everything up, defines commands and gets the bot running.
|
|
|
|
|
| # https://discordpy.readthedocs.io/
|
| import discord
|
| from discord import app_commands
|
| from discord.ext import commands
|
|
|
| # https://pypi.org/project/python-dotenv/
|
| from dotenv import dotenv_values
|
|
|
|
|
| ### Setup ###
|
|
|
| # Load bot tokens and other config from a file named `.env` in the bot's main directory (see README.md):
|
| config = dotenv_values(".env")
|
| # config = {"TOKEN": "<discord bot token>"}
|
|
|
| intents = discord.Intents.default()
|
| intents.message_content = True
|
|
|
| # `when_mentioned` means the only commands that will work with the bot are slash commands:
|
| bot = commands.Bot(command_prefix=commands.when_mentioned, intents=intents)
|
|
|
| @bot.event
|
| async def on_ready() -> None:
|
| print(f"Logged into Discord as {bot.user}")
|
|
|
|
|
| ### Commands ###
|
|
|
| # Remember to use `ephemeral=True` for messages that only the user of the command should see.
|
| # There's no sense in clogging up the channel with unnecessary messages.
|
|
|
| @bot.hybrid_command(description="Shows the bot's latency (ping)")
|
| async def ping(ctx):
|
| await ctx.send(f"This bot's ping is {round(bot.latency * 1000)} ms.", ephemeral=True)
|
|
|
|
|
| # Used for ready() and notready() (temporary):
|
| user_ready = False
|
|
|
|
|
| @bot.hybrid_command(description="/ready n if you'll be ready for a game for n hours")
|
| async def ready(ctx: commands.Context, hours: int):
|
| global user_ready
|
| if 12 > hours > 0:
|
| await ctx.send(f"ā
\nš® You'll be marked **ready for a game** for the next **{str(hours)} hours**.", ephemeral=True)
|
| else:
|
| await ctx.send(f"š«\nYou must enter a value from **1** to **12** for **hours**.", ephemeral=True)
|
| user_ready = True
|
|
|
|
|
| @bot.hybrid_command(description="Undo marking yourself as ready for a game")
|
| async def notready(ctx: commands.Context):
|
| global user_ready
|
| if user_ready:
|
| await ctx.send(f"ā
\nYou're **no longer** marked as ready for a game.", ephemeral=True)
|
| user_ready = False
|
| else:
|
| await ctx.send(f"š«\nYou weren't marked as ready anyway!", ephemeral=True)
|
|
|
|
|
| @bot.hybrid_command(description="Explain how the bot's commands work")
|
| async def helpme(ctx: commands.Context):
|
| await ctx.send(f"""Here's some help:
|
|
|
| `/ready n` marks you as ready for a game for *n* hours with a special role.
|
| `/notready` removes the ready for a game role.
|
| `/helpme` shows this explanation.""", ephemeral=True)
|
|
|
|
|
| # Sync the above commands with Discord:
|
|
|
| async def setup_hook() -> None:
|
| await bot.tree.sync()
|
| print("Command tree should be synced with Discord.")
|
| bot.setup_hook = setup_hook
|
|
|
|
|
| ### Start the bot ###
|
|
|
| bot.run(config["TOKEN"])
|