Write a script of a simple Discord bot using discord.py
- User
Write a script of a simple Discord bot using discord.py
- Assistant
Creating a Discord bot using
discord.pyinvolves several steps, including setup on the Discord Developer Portal, installingdiscord.py, and writing the bot script. Below, I'll guide you through creating a simple bot script that responds to a specific command. Please ensure you've already created a bot and obtained your bot token from the Discord Developer Portal before proceeding.Pre-requisites
- Python 3.5.3 or higher should be installed on your system.
- Install
discord.pylibrary using pip:python3 -m pip install -U discord.py
Step 1: Create Your Bot Script
Create a new Python file, for example,
bot.py, and open it with your favorite text editor or IDE.Step 2: Import Discord.py
At the top of your
bot.pyfile, import the discord library and asyncio, if necessary:import discord from discord.ext import commandsStep 3: Set Up Bot Command Prefix
Define the command prefix (the character that precedes commands). Here, we'll use '!' as the prefix:
bot = commands.Bot(command_prefix='!')Step 4: Create a Simple Command
Now, let's define a simple command that makes the bot respond with "Hello!" when someone types
!helloin a server channel where the bot is present:@bot.command(name='hello') async def send_hello(ctx): await ctx.send("Hello!")Step 5: Add Events
You can also respond to events, such as when the bot is ready and operational. Let's add that:
@bot.event async def on_ready(): print(f'{bot.user.name} has connected to Discord!')Step 6: Run Your Bot
At the bottom of your script, add the line to run your bot with your bot's token (replace
'your_token_here'with the actual token you got from the Bot page in the Discord Developer Portal):bot.run('your_token_here')Full Script:
Here’s the complete bot script for reference:
import discord from discord.ext import commands bot = commands.Bot(command_prefix='!') @bot.command(name='hello') async def send_hello(ctx): await ctx.send("Hello!") @bot.event async def on_ready(): print(f'{bot.user.name} has connected to Discord!') bot.run('your_token_here')Important Notes:
- Replace
'your_token_here'with the actual token provided by Discord. - Make sure to invite your bot to a server where you have permissions to add bots. You can generate an invite link for your bot from the Discord Developer Portal.
- Always keep your bot token private and never share it or commit it to public repositories.
Running Your Bot
In your terminal or command prompt, navigate to the folder containing your
bot.pyfile and run it with Python:python bot.pyYou should see a message printed to the console that the bot has connected to Discord, and it will respond with "Hello!" whenever someone types
!helloin a text channel it has access to.That’s it! You’ve just created a simple Discord bot with
discord.py. From here, you can explore further and add more complex commands and event responses to your bot.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.