A memorandum that created the Discord bot
For some reason, I wanted to create a Discord bot, so a memorandum of various actions from the bot creation I wanted to try something other than slackbot
The item description is as follows
Discord is voice call / VoIP free software. (From Wikipedia)
I've used it like Slack several times before
Since there is a bot in Slack, I thought that Discord was also ... and I looked it up, but it was also in discord, so I tried to make it
Created from discord developer
Click New Application on the Applications page
You will be asked for the bot name, so give it your favorite bot name
Open the bot page and click the Add Bot button Click Yes, do it!
The created bot is displayed You can also change the icon and name
** Make a copy of the TOKEN name **
Set OAuth2 as follows (I tried turning on all TEXT systems for the time being)
When you connect to the URL on the OAuth2 settings screen, you will be asked where to invite the bot to the Discord room. Once the identity is complete, the bot will enter the room.
The modules are as follows Please install if necessary
pip install discord #Required when using discord
The directory structure is as follows
- dis_python
- img
- good.png
-dis_test.py
discord.py There are many things written on it. It's fun to thoroughly investigate what you want to do
dis_test.py
import discord
TOKEN = "Paste Bot TOKEN"
client = discord.Client()
GOOD_IMG = "./img/good.PNG"
###Event handler list#################################################
#Just change after async def to change the executed event
#Execute when message is received: on_message(message)
#Run when bot starts: on_ready(message)
#Execute when adding reaction: on_reaction_add(reaction, user)
#Execute when new member joins: on_member_join(member)
#Execute for voice channel entry / exit: on_voice_state_update(member, before, after)
###################################################################
#bot startup processing
@client.event
async def on_ready():
channel = client.get_channel(Paste the channel ID)
await channel.send("Hi! how are you?")
#When receiving a message
@client.event
async def on_message(message):
#Ignore if sender is a bot
if message.author.bot:
return
if message.content.startswith("Hello"):
await message.channel.send("Hello!")
if len(message.attachments) > 0:
await message.channel.send("Did you send any files? ?? ??")
@client.event
async def on_reaction_add(reaction, user):
await reaction.message.channel.send('{}Did you press? ??'.format(reaction.emoji), file=discord.File(GOOD_IMG))
client.run(TOKEN)
I will summarize the detailed explanation next time ...
Use the TOKEN and channel ID you are using The channel ID is "Numbers 2" at the end of the URL
: //discordapp.com/channels/ Numbers 1 / Numbers 2
The event handler list is a collection of things that you might use. Especially on_message () is often used.
Command line
Play with bot
I tried to summarize it for the time being
As for the document, the content is deep, so I would like to turn it next time. Especially, file operation with on_message was unexpectedly troublesome, so I made a memorandum about that (˘ω˘)
Recommended Posts