Skip to content

Files

145 lines (82 loc) · 4.05 KB

discord-server-bot-setup.md

File metadata and controls

145 lines (82 loc) · 4.05 KB

Discord Server and Bot Setup

Follow this guide to

  • Create a Discord Server
  • Create a Discord Bot
  • Connect the Bot to the Server

NOTE: These steps require a Discord account with two-factor authentication enabled.

Create the Server

In the left sidebar of the Discord Window, click the button "Add a Server":

Discord: Add a Server

Select "Create My Own":

Discord: Create My Own

Skip further questions:

Discord: Skip question

Configure the server name and icon (can be updated later), then click "Create":

Discord: Server name and icon

Congratulations, you now have your own Discord server.

Create a Bot

Go to https://discord.com/developers/applications/ and log in.

In the top-right corner, click "New Application".

Bot: New Application

Configure the application name (can be updated later), agree to the terms of service, and click "Create".

Bot: Create Application

In the left sidebar, go to "General Information" and configure the bot's icon and description (optional, can be updated later).

Bot: Icon and Description

Scroll down and configure the Terms of Service and Privacy Policy (optional, can be updated later).

Bot: ToS and Privacy Policy

In the left sidebar, go to "Installation". Remove the option "User install" and set the Install Link to "None".

Bot: Install configuration

In the left sidebar, go to "Bot", then scroll down to "Privileged Gateway Intents". Activate "Server Member Intent" and "Message Content Intent". Click "Save Changes".

Bot: Configure Intents

In the left sidebar, go to "OAuth2". Select the scope "bot" and the permission "Administrator".

Bot: Scope and Permissions

Scroll down, configure the integration type "Guild Install", and copy the URL.

Bot: Integration URL

Open the copied URL in a web browser. You will be prompted to open the Discord app.

Bot: Open in Discord

Proceed to add the bot to your server.

Bot: Add to Server

Confirm the assigned permissions.

Bot: Confirm Permissions

Congratulations, you created a bot and added it to a server.

Bot: Post-Installation Screen

Use the Bot

To use the bot from Python, you need an authorization token.

Go back to https://discord.com/developers/applications/. In the left sidebar, go to "Bot", and click "Reset Token".

Bot: Reset Token

Confirm that step. This might require a two-factor authentication confirmation.

Bot: Confirm Token Reset

Copy your new token and store it somewhere safe.

Bot: Copy Token

To confirm the installation, install the Python package discord.py (e.g. with pip install discord.py), and run the following script (add your bot token at BOT_TOKEN = "..."):

import asyncio

import discord
from discord.ext import commands

BOT_TOKEN = "..."


class Bot(commands.Bot):
    async def on_ready(self):
        print("ready", self.user.name, self.user.id)


class Ping(commands.Cog):
    @commands.hybrid_command(name="ping")
    async def ping(self, context):
        await context.send("Pong!")


async def main():
    prefix = commands.when_mentioned_or("$")

    intents = discord.Intents(messages=True, message_content=True)
    async with Bot(command_prefix=prefix, intents=intents) as bot:
        await bot.add_cog(Ping())
        await bot.start(BOT_TOKEN)


if __name__ == "__main__":
    asyncio.run(main())

In Discord, go to your server and write the message $ping in a text channel. The bot should respond with Pong!.

Bot: Ping Pong

You can now stop the Python script by pressing Ctrl+C.