[PYTHON] Discord bot memorandum ① bot creation

Introduction

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

What is Discord?

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

How to create a Discord bot

Created from discord developer

Click New Application on the Applications page cut1.PNG

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! cut2.PNG

The created bot is displayed You can also change the icon and name

** Make a copy of the TOKEN name ** cut3.PNG

Set OAuth2 as follows (I tried turning on all TEXT systems for the time being) cut4.PNG cut5.PNG

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.

cut6.PNG

Module, directory structure, etc.

module

The modules are as follows Please install if necessary

pip install discord   #Required when using discord

Directory structure

The directory structure is as follows

- dis_python
  - img
    - good.png
  -dis_test.py

I made various things

Official

discord.py There are many things written on it. It's fun to thoroughly investigate what you want to do

program

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

A little commentary

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.

Output result

Command line cut7.PNG

Play with bot cut8.PNG cut9.PNG

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

Discord bot memorandum ① bot creation
Let's make a Discord Bot.
Python beginner launches Discord Bot
Parrot return LINE BOT creation
I made a discord bot
[DISCORD BOT] Server Manager Command Help
Mastodon Bot Creation Memo: Part 4 Summary
Launch the Discord Python bot for 24 hours.
TFRecord file creation memorandum for object detection