[PYTHON] Let's make a Discord Bot.

I made a Discord Bot in Python on a whim, so I'll introduce it in the procedure. computer_jinkou_chinou.png

Creating an environment

Download discord.py

Install discord.py with pip in the Python development environment.

python3 -m pip install -U discord.py

Register your bot with DEVELOPER PORTAL

Go to DEVELOPER PORTAL and register your bot. Click New Application </ b> and decide on a name for your bot. image.png Select Bot from the menu and click Add Bot </ b> for Build-A-Bot. image.png When this screen appears, change the name and icon of the bot to whatever you like. image.png

Get Token

Click Click to Reveal Token </ b> and copy the Token. (Tokens should not be taught to others (hijacked) </ b>)

Join the bot to the server.

You can't talk unless you put the bot on the server. To put the bot on the server, select the checkbox Bot </ b> from OAuth2 in the menu and access the link that appears below to join the bot. (I think it's a good idea to set permissions according to the intended use of the bot.) image.png

Write a bot program

Create a Python file and open it.

Import Discord.py installed with pip.

import discord #Discord.Import py

TOKEN = "YOUR TOKEN" #Write the Token here.
client = discord.Client()

Write a process to send a message when the bot is online and ready.

@client.event
async def on_ready(): #When the bot is ready
   print("Bot logged in")
   ChannelID = int(0000000000000) #Write the channel ID to send
   channel = client.get_channel(ChannelID)
   await channel.send("I'm here!")

Write the code to reply with a specific string.

@client.event
async def on_message(message)
   if message.author.bot: #If it is a bot, it will not respond
        return
   if message.content == "/hello": #/When hello is sent
        print("/hello has been executed.")
        await message.channel.send("Hello!")

Write the code to start the bot.

client.run(TOKEN)

Besides on_ready () and on_message () ..

In addition to on_ready () and on_message (), there are the following event handlers.

  • Be sure to write `` `@ client.eventandasync def```.
@client.event
async def on_member_join(member): #When a new user joins
  #processing
@client.event
async def discord.on_voice_state_update(member, before, after): #When entering and exiting Men's voice channel
  #processing
@client.event
async def on_reaction_add(reaction, user): #When a reaction is added
  #processing

Code description example

Discord_Bot.py


import discord #Discord.Import py

TOKEN = "YOUR TOKEN" #Write the Token here.
client = discord.Client()

@client.event
async def on_ready(): #When the bot is ready
   print("Bot logged in")
   print("----------")
   ChannelID = int(0000000000000) #Write the channel ID to send
   channel = client.get_channel(ChannelID)
   await channel.send("I'm here!")

@client.event
async def on_message(message)
   if message.author.bot: #If it is a bot, it will not respond
        return
   if message.content == "/hello": #/When hello is sent
        print("/hello has been executed.")
        await message.channel.send("Hello!")
@client.event
async def on_member_join(member): #When a new user joins
        print("New user participation")
        await message.channel.send("Welcome!")
@client.event
async def discord.on_voice_state_update(member, before, after): #When entering and exiting Men's voice channel
        print("Voice channel update")
        await message.channel.send("There is movement in the voice channel...")
@client.event
async def on_reaction_add(reaction, user): #When a reaction is added
        print("reaction")
        await message.channel.send("It reacted!")

client.run(TOKEN)

Try moving the bot

Save it with Ctrl + S and run it.

$ python Discord_Bot.py
Bot logged in
----------

If you see this display, you are successful. After that, I think you should add your favorite functions.

Summary

Discord Bot has a convenient library called discord.py, so you can easily create it. Please try it out (^ ▽ ^) /

Quote

-discord.py document

Recommended Posts