Python beginner launches Discord Bot

Preface

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 ...

Operating environment

Python 3.8.1 pip 20.1 discord.py 1.3.1

First of all

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.

How to make a Discord server

There is a round "+" mark on the left edge of the screen. スクリーンショット 2020-02-10 14.51.24.png When you press this スクリーンショット 2020-02-10 14.51.58.png 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!

Create a bot account

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. discordbotsample2.png

Click ** Add Bot ** on the right and click ** Yes, do it! **. discordbotsample3.png discordbotsample4.png

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> discordbotsample5

Finally, click ** OAuth2 ** from ** SETTINGS ** on the left side. discordbotsample2.png 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. discordbotsample6.png

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. discordbotsample7.png Click the pull-down that says "Choose a server", select your server and click Authenticate. This will add the bot to the selected server.

Create and launch a program

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 /dogAnd sendWow!You can have a bot that replies. discordbotsample.png

development

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

Python beginner launches Discord Bot
Beginner ABC154 (Python)
Beginner ABC156 (Python)
python beginner memo (9.1)
Python beginner notes
Discord in Python
Launch the Discord Python bot for 24 hours.
Beginner ABC155 (Python)
[Beginner] Python functions
Beginner ABC157 (Python)
PyQ ~ Python Beginner ~
Python beginner memo (2)
Python beginner Zundokokiyoshi
Discord bot with python raspberry pi zero with [Notes]
Discord bot memorandum ① bot creation
Discord Bot with recording function starting with Python: (1) Introduction discord.py
How to operate Discord API with Python (bot registration)
Let's make a Discord Bot.
python super beginner tries scraping
I made a discord bot
Web scraping beginner with python
[Python beginner] Update pip itself
Prohibit multiple launches in python
Atcoder Beginner Contest 152 Kiroku (python)
Discord Bot with recording function starting with Python: (4) Play music files
Discord Bot with recording function starting with Python: (5) Directly operate the Discord API
[Python] Make your own LINE bot
Let's play with Excel with Python [Beginner]
[Python beginner] Divide one list (5 lines).
[DISCORD BOT] Server Manager Command Help
[Beginner] Extract character strings with Python
Python beginner tried 100 language processing knock 2015 (05 ~ 09)
AtCoder Beginner Contest 174 C Problem (Python)
Python beginner tried 100 language processing knock 2015 (00 ~ 04)
A story about a beginner making a VTuber notification bot from scratch in Python
I made a Discord bot in Python that translates when it reacts