[PYTHON] [Note] Discord Bot ① Create Bot-Reply to specific content

Create a bot in the Discord Developer Portal

Log in to Discord

Go to the Discord Developer Portal and log in with your Discord account. image.png

You will be skipped to the Application tab. image.png

Creating a New Application

Click New Application on the upper right to create a new bot on Discord side. image.png

You will be taken to the basic setting screen of the created Bot. image.png

Click the Bot tab on the left side of the screen Click the Add bot button at the top right of the screen, then click yes. image.png

Click Copy to copy it to Notepad or something.

Click the OAth2 tab on the left side of the screen Check the bot in the middle of the SCOPES frame. image.png

Scroll down and under Bot Permissions, select the permissions you want to give your bot. You can use Administrator (administrator authority) to install it on your own test server and play with it. image.png

After granting authority, copy the link that appears below the SCOPE frame to access it. Once accessed, select the location where you want to add the BOT and press Yes. Also, if you have administrator privileges, a confirmation message will appear asking if you want to grant administrator privileges. image.png

Write bot content in Python

environment

Windows10 x64 Python3.7.7 x64 discord.py 1.2.5

Introducing Python

Download the corresponding version of python from Downloads in Python Official. image.png Download the one that suits the internal environment of Files. This time it is Win x64, so Windows x86-64 executable installer

image.png

The installation screen is OK for the time being

Introducing an editor

This time, I used Visual Studio Code. Use the one that matches the version from Download.

Put discord.py in PIP

Execute the following command on the command line

pip install discord.py


move

Save the following code in DiscordBot.py and execute it (async / await version)

# -*- coding: utf-8 -*-

#Library import
import discord
import asyncio

TOKEN = 'Paste the copied token here'
client = discord.Client()

#Define an event handler to be executed when the bot starts
@client.event
async def on_ready():
    print('Bot Launched')

#Define an event handler to be executed when a message is sent
@client.event
async def on_message(message):
    if message.author.bot:
        pass
    elif message.content.startswith('Hello!'):
        send_message = f'{message.author.mention}San, Hello!'
        await message.channel.send(send_message)

#Run bot
client.run(TOKEN)
#Processes written below this will not be executed until the bot is stopped

Execute the following command on the command line at the location of the file

python DiscordBot.py



 State of operation
 ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/446312/27e64e8f-a444-d77d-1b68-8543e692303d.png)





Recommended Posts

[Note] Discord Bot ① Create Bot-Reply to specific content
Create a bot to retweet coronavirus information
Steps to create a Twitter bot with python
[Note] How to create a Ruby development environment
I tried to operate Linux with Discord Bot
[Note] How to create a Mac development environment
How to operate Discord API with Python (bot registration)