Discord .py How to Read Multiple Messages

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

If you desire to piece of work with text data from your discord servers, a elementary bot is the best style to go.

Thiago Rodrigues

Photo by Matheus Ferrero on Unsplash

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

There'south a lot of possibilities regarding text processing, but whatever it is that you might want to do, you'll need information to do it. And, while that information tin 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 direct export your chat history for you to use however you'd like. But, for those of u.s.a. who desire to practice something involving other platforms, we have to figure our own way to do information technology. This is why in this mail 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

Starting time, you will need to create an application in the Discord Programmer Portal to take a token for your bot. All y'all demand to do is sign in with your Discord business relationship and, one time you're in, click New Application and give it a name. Yous can also upload a film and draw it if y'all wish.

What the Full general Data tab of your application is like.

With your awarding created, go into the bot tab in the settings menu and click Add Bot. Yous can choose your bot's username in Discord and upload its profile moving picture, which will be the aforementioned every bit the one for the awarding by default. But the thing we want on this folio is your Token, which we will need to run the bot.

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

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

For Windows:

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

For Linux:

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

For anaconda users, Pandas already comes installed.

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

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

To exercise so, we use the on_message() asynchronous part from the discord.py library, which runs every time a new message is sent. Then, nosotros bank 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 example I fix to "_".

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

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

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

To read the data into the data frame, we will brand use of the history() method of the TextChannel course in the discord.py library, which we get admission to past using the channel aspect of our bulletin. The method allows us to use a for loop to iterate through n messages in the channel history, with north beingness a parameter of the method itself gear up, past default, to 100.

Once inside the loop, we can apply if statements to avoid messages sent by our bot — in this example, it won't send any messages — and the command calls we used to trigger information technology. Then nosotros suspend the message'southward content, fourth dimension of creation, and author — which tin all be accessed via the message's attributes — to the information frame and use Pandas' to_csv() method to relieve information technology locally equally a .CSV file.

Lastly, use the customer.run() function to set your bot running, and paste the token you got from the programmer portal every bit the parameter.

Text data from social media can give united states of america many insights as to how people bear and this is no exception. Although making a bot to scrape the data isn't every bit easy as simply exporting it, doing so opens the door to yet another platform with data to exist analyzed.

In case you'd similar a bot that takes in the aqueduct and number of messages to read as command parameters and has a aid command. I have a fancier version of this bot with those features in my GitHub in case y'all'd similar to re-create or check the full code.

Either way, now that you know how to get the data, the sky is the limit as to what y'all tin can practice with information technology.

jimenezsurney.blogspot.com

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

0 Response to "Discord .py How to Read Multiple Messages"

แสดงความคิดเห็น

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel