This is the first post of N high school student. This article is an article in which the author of this article, who had no knowledge of Python, studied the basic knowledge of Python and developed a simple Discord Bot. The content explained here is almost the same as the site I referred to. Please note. Since I'm still studying, there are many things I don't understand about this command, so I'd like you to overlook it ...
Python 3.8.1 pip 20.1 discord.py 1.3.1
If you have not installed Discord, please install it from the link below. (The browser version is fine) Discord installation
Let's prepare a Python development environment. You can't do anything without it. Please refer to here to prepare the development environment. Prepare a Python development environment!
Now, the development environment is ready. You can understand the feeling of "I want to make a bot right away!", But let's study the basics first. If you understand the basics, development will be much easier. ** "I don't care about the basics, so I want to move it for the time being!" ** It's okay for people to skip it. I have read through the contents of the introductory part of this site. (It took about a month and a half ...) Introduction to Python Learning Course
First, let's create a bot account and register it on the Discord server. If you haven't created a Discord server yet, create a Discord server first.
There is a round "+" mark on the left edge of the screen. When you press this A screen like this will appear, so click "Create Server". Then give it a name for the server. You can leave the server area below as Japan. This completes the Discord server creation! It's easy!
You can create a bot account on the Discord Developer Portal (https://discordapp.com/developers/applications/).
When you reach the site, first click ** New Applications ** in the upper right corner of ** Applications **. Then name your bot with ** NAME ** and click ** Create **.
Then click ** Bot ** from ** SETTINGS ** on the left side of the screen.
Click ** Add Bot ** on the right and click ** Yes, do it! **.
Then, there is a column called ** TOKEN ** near the center of the screen, so click ** Click to Reveal Token ** there and check ** Token **. (I will use this Token later)
** * Caution! Never tell this Token to anyone ** </ font>
Finally, click ** OAuth2 ** from ** SETTINGS ** on the left side. As you slide down, you'll see a lot of checkboxes under ** OAuth2 URL Generator **. Check only ** bot ** from this checkbox and copy the URL displayed below.
Paste the copied URL into Google's search field and search. When you go to that URL, you will see a screen like the one in the image below. Click the pull-down that says "Choose a server", select your server and click Authenticate. This will add the bot to the selected server.
Finally, we will use Python to move the bot. First, install discord.py.
discord.py installation
$ pip install discord.py
Then save the code below as `` `discordbot.py```. There is no specific source code editor, so you can use the one you have already installed. By the way, the recommended editor is Atom. Let's save the save destination in "Documents" (Documents in English) so that it is easy to understand this time.
discordbot.py
#Installed discord.Load py
import discord
#Required for connection(Seems to be)Create object
client = discord.Client()
#Processing that operates at startup
@client.event
async def on_ready():
#When you start it, you will be notified that you have logged in to the terminal
print('You are now logged')
#Processing that operates when receiving a message
@client.event
async def on_message(message):
#I don't want to react if the sender is a bot
if client.user != message.author:
# /Processing to reply "Wan!" When saying dog
if message.content == '/dog':
await message.channel.send('Wow!')
#Replace the "TOKEN" part with your own bot access token
client.run("TOKEN")
Launch the bot (Copy the sentence below line by line and type it into the terminal)
Launch the bot
$ cd Documents
$ python3 discordbot.py
On the text channel of the Discord server that the bot is participating in
/dog
And sendWow!
You can have a bot that replies.
If you can do so far, all you have to do is add the functions you want to create. After that, I made it possible to get the current time and display the weather forecast using a technology called web scraping. Also, I think that the bot cannot be used as it is unless the terminal is attached. So I used a hosting service called Heroku to make it available 24 hours a day. I don't think that the implementation around here can be taught correctly if it is my own level of understanding, so please refer to the articles of other people and try it.
Recommended Posts