Read Discord chats with Python

In this tutorial, you will learn how to create a simple Discord app and a Bot that reads incoming chats from a server using Python.

Read Discord chats with Python

Discord bots are applications that allow users to communicate with Discord services in an automatic manner.

They can be programmed to perform basic activities like playing music or printing a welcome message, or they can be designed to execute various functions based on pre-programmed commands.

Discord bots may also be used to moderate some administration-level activities, which is useful for managing and moderating big servers.

There are also numerous bots accessible, which you may find at the bot store.

In this tutorial, we will learn how to read incoming discord chats using Python.

Objectives

The goal of this tutorial is to show you the basics of creating a Discord bot that reads the steam of chats sent on the server using Python.

Prerequisites

In order to create a Discord bot in Python, you’ll need a discord account and a server, too.

You will also need some understanding of Python.

In Python, we will use the python.py library that will allow us to interact with the Discord API.

Create the Discord bot

Creating a Discord bot is a quick, basic process that can be completed using the developer site or any code editor.

A bot account must be created before the code can be added.

Log in to the Discord developer site at https://discord.com/developers/applications and select 'New Application.'

Then, enter the name of the app you’d like to create.

Name this application and then select bot from the list on the left.

Now, create a bot by clicking “Add bot”

You will land on this page, and click on “Reset token” to get the token that we will use later on in Python.

Once reset, you should see your token displayed. Copy it and store it somewhere for later.

A Discord bot might have to access data that Discord considers “sensitive”.

To provide your bot access to privileged intents, go to the Developer Portal's bots area and activate 'Privileged Gateway Intents.'

This is only viable for unconfirmed bots on less than 100 servers.

Privilege intentions must be sought for confirmed bots.

Now it’s time to invite the bot to the server where it will be used.

For that head over to Oauth2 > URL Generator.

On this page, you will be able to select which permissions you’d like to give your bot.

First, in the Scopes section, check the bot option (1), and in the Permissions section, select the Administrator option (2).

Note that in real life, you probably wouldn’t want to give a bot administrator rights unless you know exactly what you are doing. For simplicity in this tutorial, we’re choosing admin.

Now copy the link generated (3) below to invite the bot to your server.

When you open the link, you’ll have to choose the server on which you want to add the bot, and hit Continue.

You will also be prompted to confirm the permissions that you are granting to the bot, and hit Authorize.

And voilà! Our friendly bot is now on our server. Now let’s get it to read the messages sent to the server.

Create the Discord bot in Python

Begin by creating a python project where you’d like to create your bot.

We will use the package discord.py, which fully implements Discord's API. To install discord.py, use the pip command.

pip install discord.py

Let’s create a python file called bot.py.

We can now begin making the connection with the API. The code below demonstrates how to instantiate the library:

import discord

API_TOKEN = '<MY_API_TOKEN>'

client = discord.Client()
guild = discord.Guild

@client.event
async def on_ready():
    print('Hello {0.user} !'.format(client))
    await client.change_presence(activity=discord.Game('_scan help'))

if __name__ == "__main__":
    client.run(API_TOKEN)

This code is needed to establish the first connection with Discord. We begin by importing the necessary library discord.

This code outputs:

> Hello friendly bot#0540 !

Now let’s read the feed of messages sent to the server, for that we will need to listen to every incoming message sent to the server.

@client.event
async def on_message(message):
    message_content = message.content
    message_author = message.author
    print(f'New message -> {message_author} said: {message_content}')

This piece of code outputs:

> New message -> <user> said: Hello, friendly bot!

Full code

All pieced together, our code looks like this:

import discord

API_TOKEN = '<MY_API_TOKEN>'

client = discord.Client()
guild = discord.Guild


@client.event
async def on_ready():
    print('Hello {0.user} !'.format(client))
    await client.change_presence(activity=discord.Game('👀'))


@client.event
async def on_message(message):
    message_content = message.content
    message_author = message.author
    print(f'New message -> {message_author} said: {message_content}')


if __name__ == "__main__":
    client.run(API_TOKEN)

Conclusion

As you can see, it is fairly straightforward to create a discord bot and read the stream of chats coming into a Discord server.

Now, the possibilities for text analysis are endless. By leveraging APIs like Tinq.ai, it is very easy to implement content moderation or even a full analysis of Discord server communications.