|
| 1 | +# Discord Server and Bot Setup |
| 2 | + |
| 3 | +Follow this guide to |
| 4 | + |
| 5 | +* Create a Discord Server |
| 6 | +* Create a Discord Bot |
| 7 | +* Connect the Bot to the Server |
| 8 | + |
| 9 | +NOTE: These steps require a Discord account with two-factor authentication enabled. |
| 10 | + |
| 11 | +## Create the Server |
| 12 | + |
| 13 | +In the left sidebar of the Discord Window, click the button "Add a Server": |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +Select "Create My Own": |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +Skip further questions: |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +Configure the server name and icon (can be updated later), then click "Create": |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +Congratulations, you now have your own Discord server. |
| 30 | + |
| 31 | +## Create a Bot |
| 32 | + |
| 33 | +Go to https://discord.com/developers/applications/ and log in. |
| 34 | + |
| 35 | +In the top-right corner, click "New Application". |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +Configure the application name (can be updated later), agree to the terms of service, and click "Create". |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +In the left sidebar, go to "General Information" and configure the bot's icon and description (optional, can be updated later). |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +Scroll down and configure the Terms of Service and Privacy Policy (optional, can be updated later). |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +In the left sidebar, go to "Installation". |
| 52 | +Remove the option "User install" and set the Install Link to "None". |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +In the left sidebar, go to "Bot", then scroll down to "Privileged Gateway Intents". |
| 57 | +Activate "Server Member Intent" and "Message Content Intent". |
| 58 | +Click "Save Changes". |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +In the left sidebar, go to "OAuth2". |
| 63 | +Select the scope "bot" and the permission "Administrator". |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +Scroll down, configure the integration type "Guild Install", and copy the URL. |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +Open the copied URL in a web browser. You will be prompted to open the Discord app. |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +Proceed to add the bot to your server. |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +Confirm the assigned permissions. |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +Congratulations, you created a bot and added it to a server. |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +## Use the Bot |
| 88 | + |
| 89 | +To use the bot from Python, you need an authorization token. |
| 90 | + |
| 91 | +Go back to https://discord.com/developers/applications/. |
| 92 | +In the left sidebar, go to "Bot", and click "Reset Token". |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +Confirm that step. This might require a two-factor authentication confirmation. |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +Copy your new token and store it somewhere safe. |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +To confirm the installation, install the Python package [discord.py](https://pypi.org/project/discord.py/) |
| 105 | +(e.g. with `pip install discord.py`), and run the following script (add your bot token at `BOT_TOKEN = "..."`): |
| 106 | + |
| 107 | +```python |
| 108 | +import asyncio |
| 109 | + |
| 110 | +import discord |
| 111 | +from discord.ext import commands |
| 112 | + |
| 113 | +BOT_TOKEN = "..." |
| 114 | + |
| 115 | + |
| 116 | +class Bot(commands.Bot): |
| 117 | + async def on_ready(self): |
| 118 | + print("ready", self.user.name, self.user.id) |
| 119 | + |
| 120 | + |
| 121 | +class Ping(commands.Cog): |
| 122 | + @commands.hybrid_command(name="ping") |
| 123 | + async def ping(self, context): |
| 124 | + await context.send("Pong!") |
| 125 | + |
| 126 | + |
| 127 | +async def main(): |
| 128 | + prefix = commands.when_mentioned_or("$") |
| 129 | + |
| 130 | + intents = discord.Intents(messages=True, message_content=True) |
| 131 | + async with Bot(command_prefix=prefix, intents=intents) as bot: |
| 132 | + await bot.add_cog(Ping()) |
| 133 | + await bot.start(BOT_TOKEN) |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + asyncio.run(main()) |
| 138 | +``` |
| 139 | + |
| 140 | +In Discord, go to your server and write the message `$ping` in a text channel. |
| 141 | +The bot should respond with `Pong!`. |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +You can now stop the Python script by pressing Ctrl+C. |
0 commit comments