Go to the Discord Developer Portal and log in with your Discord account.
You will be skipped to the Application tab.
Click New Application on the upper right to create a new bot on Discord side.
You will be taken to the basic setting screen of the created Bot.
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.
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.
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.
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.
Windows10 x64 Python3.7.7 x64 discord.py 1.2.5
Download the corresponding version of python from Downloads in Python Official. Download the one that suits the internal environment of Files. This time it is Win x64, so Windows x86-64 executable installer
The installation screen is OK for the time being
This time, I used Visual Studio Code. Use the one that matches the version from Download.
Execute the following command on the command line
pip install discord.py
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