This is a template for a Discord bot written in Python using the Nextcord library. It provides a basic structure for your bot project, as well as examples of commands/events/tasks, utilities and more.
- Python 3.8 or higher (Note: using the latest version of Python may cause this installation error, so the easiest way to avoid this is not to use it.)
- A bot account and token
- Basic knowledge of Python and Discord
- Click on the "Use this template" button at the top of the GitHub page to create a new repository based on this template.
- Clone your new repository locally:
git clone https://github.com/yourusername/your-bot-repo.git
- Navigate to the cloned repository:
cd your-bot-repo
- Create a virtual environment (recommended):
- On Windows:
python -m venv venv
- On Linux/macOS:
python3 -m venv venv
- On Windows:
- Activate the virtual environment:
- On Windows:
venv\Scripts\activate
- On Linux/macOS:
source venv/bin/activate
- On Windows:
- Install dependencies:
pip install -r requirements.txt
- Rename
.env.example
to.env
and add your bot's token. - Add your test server's ID to the config.py file.
- Run the bot using
python3 main.py
.
The bot handles commands, events, and tasks using cogs. This allows you to easily organize your code into separate files and folders. The cogs directory can be defined by setting the COGS_DIR
variable in the config.py
file.
The bot can automatically reload commands, events, and tasks when they are modified. This is useful for testing changes without having to restart the bot.
This feature can be enabled/disabled by setting the AUTO_RELOAD
variable in the config.py
file.
ConfirmButtons
: A view that displays a confirmation message with two buttons.PageButtons
: A view that displays a paginated message with buttons to navigate between pages.
The bot has a built-in Logger
class that can be used to log messages to the console and a log file.
To create a new cog, create a new file in the cogs directory, create a class that inherits from nextcord.ext.commands.Cog
and a setup
function that adds the cog to the bot. Then, add your commands, events, and tasks to the cog. Read the Nextcord cogs documentation or take a look at the Nextcord examples for more information. Here are some examples of cogs:
from config import *
from bot import Bot
from nextcord import Interaction, slash_command
from nextcord.ext.commands import Cog
class ExampleCommand(Cog):
def __init__(self, bot: Bot):
self.bot = bot
@slash_command(description="Example command", guild_ids=GUILD_IDS)
async def example_command(self, interaction: Interaction, ...): # Replace ... with your command parameters
pass # Your code here
def setup(bot: Bot):
bot.add_cog(ExampleCommand(bot))
from config import *
from bot import Bot
from nextcord.ext.commands import Cog
from nextcord.ext import commands
class ExampleEvent(Cog):
def __init__(self, bot: Bot):
self.bot = bot
@commands.Cog.listener("event_name") # Replace event_name with the event name
async def example_event(self, ...): # Replace ... with the event parameters
pass # Your code here
def setup(bot: Bot):
bot.add_cog(ExampleEvent(bot))
Note: You can find a list of events here.
from config import *
from bot import Bot
from nextcord.ext.commands import Cog
from nextcord.ext import tasks
class ExampleTask(Cog):
def __init__(self, bot: Bot):
self.bot = bot
@tasks.loop(seconds=30) # Put the interval here using seconds, minutes or hours
async def example_task(self):
await self.bot.wait_until_ready() # This prevents the task from running before the bot is ready
# Your code here
@example_task.error
async def on_error(self, exception: Exception):
await self.bot.handle_task_error(exception, "example_task") # This will log the error and print it to the console
def setup(bot: Bot):
bot.add_cog(ExampleTask(bot))
Contributions are welcome! If you have any suggestions or find any bugs, please open an issue or submit a pull request.
This project is licensed under the MIT License. You are free to use, modify, and distribute this code as long as you give credit to the original author and include the license. See the LICENSE file for details.