samyyc.dev
LOC::Macau, China

..UTC+8

samyyc.dev

-- BPM

Sun Jun 04 2023

Making a Discord Bot with Python

#84

/

samyyc

/

237 words

/

Updated 1279 days ago

/

[中文]

Making a Discord Bot with Python

Prerequisites

  • Python 3.10+
  • Some magic gizmos for getting on Discord

Setting up the Bot

To make a Discord bot, you first need to register a Discord app:

Applying for a Bot token

Log in to Discord Developer Portal — My Applications

Create an app by following these steps:

6AEB531F-7B93-4ED1-BC0A-294607279B0D.png

BB9D9F9F-E63C-487D-8A28-4CD6C29CEF1A.png

Once you click Create, the app is created. There should be some way to see the bot token directly at this point, but for some reason it wasn't showing up for me, so go to the app page, click the Bot option on the left, and click Reset Token to reset the bot token.

After you click it, the bot token is shown once and then never again, so keep it somewhere safe.

01B749F8-A24A-4F1C-B76C-93ECCB37AB57.png7326D193-D8EF-414B-B516-32C3DA5ADE8F.pngIf these three aren't enabled, you'll run into problems when registering slash commands.

Granting permissions

Next you need to create a Discord server (skip this if you already have one), then invite the Bot in.

B9DD0C9E-28AA-4118-87EC-25669942C237.png72F176D9-B1BC-48CA-A7C8-D619130226BE.pngOnce the Bot is in, the Discord-side prep is done.

Writing the code

I'll use the Python discord-py-interactions library here.

The library's Github repo: Github

Tutorial & API docs: Guide

First install the library (requires Python 3.10+):

pip install -U discord-py-interactions

Code:

import interactions
bot.start(TOKEN)

And that's the simplest possible Discord bot skeleton set up. I'll keep adding features going forward.