Skip to content Skip to sidebar Skip to footer

How To Download Transcript From Discord

How to Gather Message Data Using a Discord Bot From Scratch With Python

If you want to work with text data from your discord servers, a simple bot is the best way to go.

Thiago Rodrigues

Photo by Matheus Ferrero on Unsplash

Introduction

In this day and age, applications that make use of text analysis are everywhere from your typical e-mail spam filter to chatbots capable of making enough sense out of messages that they can even respond to them.

There's a lot of possibilities regarding text processing, but whatever it is that you might want to do, you'll need data to do it. And, while that data can come from pretty much anywhere, if you're after text data that represents how people talk on the daily, you need to look no further than to social media.

Some social media platforms like WhatsApp or Telegram allow you to directly export your chat history for you to use however you'd like. But, for those of us who want to do something involving other platforms, we have to figure our own way to do it. This is why in this post I'll be showing you how to use a Discord bot to scrape your own servers for message history data to use for whatever you'd like.

The Discord Logo

Getting Set Up

First, you will need to create an application in the Discord Developer Portal to have a token for your bot. All you need to do is sign in with your Discord account and, once you're in, click New Application and give it a name. You can also upload a picture and describe it if you wish.

What the General Information tab of your application is like.

With your application created, go into the bot tab in the settings menu and click Add Bot. You can choose your bot's username in Discord and upload its profile picture, which will be the same as the one for the application by default. But the thing we want on this page is your Token, which we will need to run the bot.

The Token is what will be used by Discord to identify your bot.

With the bot created, the last thing you'll need to do before starting to code is to install discord.py, a library used to code Discord bots with Python, and Pandas, which is a data analysis and manipulation library. The installation can be easily done using pip by running one of the following lines:

For Windows:

            py -3 -m pip install -U discord.py            pip install pandas          

For Linux:

            python3 -m pip install -U discord.py            pip3 install pandas          

For anaconda users, Pandas already comes installed.

Starting The Bot

We start by importing the two modules we installed and define the client and guild variables, which are used to refer to the bot itself and the servers, also called guilds, respectively.

To make the bot universal so we don't have to keep getting the unique ID of each server we want to analyze, we will be making it so the bot reads whichever channel we call our command in.

To do so, we use the on_message() asynchronous function from the discord.py library, which runs every time a new message is sent. Then, we check if the message author is the bot itself, and, if not, whether the message starts with whatever string we define as our command call, which in this case I set to "_".

If the message does start with the command call, I like to split it's content and then save the first element into a 'command' variable and the rest into a list of parameters. But since we'll be making the bot read whichever channel we call the command on, that part is optional.

If the 'command' variable is equal to whatever we named our command — in this case, "_scan" — then we run our code.

Reading The Data

There are many variables we could want to track from a server's message history. But, to keep it simple, we'll only be looking at when the messages were sent, who sent them, and the content of the message itself. And, to do that, we create a Data Frame containing one column for each.

To read the data into the data frame, we will make use of the history() method of the TextChannel class in the discord.py library, which we get access to by using the channel attribute of our message. The method allows us to use a for loop to iterate through n messages in the channel history, with n being a parameter of the method itself set, by default, to 100.

Once inside the loop, we can use if statements to avoid messages sent by our bot — in this case, it won't send any messages — and the command calls we used to trigger it. Then we append the message's content, time of creation, and author — which can all be accessed via the message's attributes — to the data frame and use Pandas' to_csv() method to save it locally as a .CSV file.

Lastly, use the client.run() function to set your bot running, and paste the token you got from the developer portal as the parameter.

Conclusion

Text data from social media can give us many insights as to how people behave and this is no exception. Although making a bot to scrape the data isn't as easy as simply exporting it, doing so opens the door to yet another platform with data to be analyzed.

In case you'd like a bot that takes in the channel and number of messages to read as command parameters and has a help command. I have a fancier version of this bot with those features in my GitHub in case you'd like to copy or check the full code.

Either way, now that you know how to get the data, the sky is the limit as to what you can do with it.

Source: https://levelup.gitconnected.com/how-to-gather-message-data-using-a-discord-bot-from-scratch-with-python-2fe239da3bcd

Posted by: erlenebujnowskie0194747.blogspot.com

Post a Comment for "How To Download Transcript From Discord"