Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0125-preview· OpenAIAi2 WildChat-4.8M (AI2)

Write a script of a simple Discord bot using discord.py

  1. User

    Write a script of a simple Discord bot using discord.py

  2. Assistant

    Creating a Discord bot using discord.py involves several steps, including setup on the Discord Developer Portal, installing discord.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

    1. Python 3.5.3 or higher should be installed on your system.
    2. Install discord.py library 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.py file, import the discord library and asyncio, if necessary:

    import discord
    from discord.ext import commands
    

    Step 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 !hello in 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.py file and run it with Python:

    python bot.py
    

    You should see a message printed to the console that the bot has connected to Discord, and it will respond with "Hello!" whenever someone types !hello in 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.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.