What Do Repair Bots Do In Deep Town
How to Gather Message Information Using a Discord Bot From Scratch With Python
If yous want to work with text information from your discord servers, a simple bot is the best way to go.
Introduction
In this day and historic period, applications that make use of text analysis are everywhere from your typical east-mail service spam filter to chatbots capable of making enough sense out of messages that they can even respond to them.
At that place's a lot of possibilities regarding text processing, but whatsoever it is that you might desire to do, you'll need data to do it. And, while that data can come from pretty much anywhere, if you lot're subsequently text data that represents how people talk on the daily, yous need to look no further than to social media.
Some social media platforms like WhatsApp or Telegram permit you to directly export your conversation history for you to use however you'd like. But, for those of us who desire to do something involving other platforms, nosotros have to figure our ain style to practice it. This is why in this postal service I'll be showing you how to use a Discord bot to scrape your own servers for message history data to use for any y'all'd similar.
Getting Set up Upward
First, you will need to create an application in the Discord Developer Portal to accept a token for your bot. All you lot need to do is sign in with your Discord business relationship and, one time you're in, click New Application and give it a name. You tin besides upload a picture and depict information technology if you wish.
With your application created, go into the bot tab in the settings menu and click Add Bot. Y'all can choose your bot's username in Discord and upload its profile pic, which volition exist the same as the 1 for the application past default. But the thing nosotros want on this page is your Token, which we will need to run the bot.
With the bot created, the last affair yous'll demand 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 past running one of the following lines:
For Windows:
py -3 -thou 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 beginning 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, likewise chosen guilds, respectively.
To make the bot universal then we don't have to keep getting the unique ID of each server we want to clarify, we will exist making it and then the bot reads whichever channel nosotros call our command in.
To practise and then, we use the on_message() asynchronous function from the discord.py library, which runs every time a new message is sent. So, we check if the message author is the bot itself, and, if non, whether the bulletin starts with whatever string we define as our command phone call, which in this case I gear up to "_".
If the message does get-go with the command call, I like to split it'due south content so salve the showtime element into a 'command' variable and the rest into a list of parameters. But since nosotros'll be making the bot read whichever aqueduct nosotros call the command on, that part is optional.
If the 'command' variable is equal to whatever we named our control — in this case, "_scan" — then we run our code.
Reading The Information
At that place are many variables we could desire to track from a server'due south message history. But, to continue it uncomplicated, 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 ane column for each.
To read the data into the information frame, nosotros will make apply of the history() method of the TextChannel class in the discord.py library, which nosotros get access to by using the channel attribute of our message. The method allows the states to utilise 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, information technology won't send any messages — and the control calls we used to trigger information technology. And so we suspend the message's content, time of creation, and writer — which can all be accessed via the message'southward attributes — to the information frame and use Pandas' to_csv() method to save information technology locally equally a .CSV file.
Lastly, use the client.run() function to ready your bot running, and paste the token you got from the developer portal as the parameter.
Conclusion
Text data from social media tin can requite us many insights equally to how people deport and this is no exception. Although making a bot to scrape the data isn't as like shooting fish in a barrel 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 help command. I have a fancier version of this bot with those features in my GitHub in example you'd similar to copy or check the full lawmaking.
Either fashion, now that y'all know how to get the data, the heaven is the limit as to what you tin do with it.
Source: https://levelup.gitconnected.com/how-to-gather-message-data-using-a-discord-bot-from-scratch-with-python-2fe239da3bcd
Posted by: correathippid.blogspot.com
0 Response to "What Do Repair Bots Do In Deep Town"
Post a Comment