Skip to content

Latest commit

 

History

History
148 lines (87 loc) · 4.27 KB

File metadata and controls

148 lines (87 loc) · 4.27 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), run the following script, and enter your bot token when prompted:

# /// script
# dependencies = ["discord-py"]
# ///
import asyncio

import discord
from discord.ext import commands


class Bot(commands.Bot):
    async def on_ready(self):
        print(f"Bot is running as user {self.user.name!r}.")
        print("Post $ping in any channel on your server to test the bot.")
        print("Press Ctrl+C to stop the bot.")


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


async def main(token: str) -> None:
    intents = discord.Intents(messages=True, message_content=True)
    async with Bot(command_prefix="$", intents=intents) as bot:
        await bot.add_cog(Ping())
        await bot.start(token)


if __name__ == "__main__":
    bot_token = input("Please enter your bot token: ").strip()
    print("Bot is starting, please wait...")
    asyncio.run(main(bot_token))

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.