If you want to make a discord bot with python, let's use a framework

Introduction

This is the article on the 20th day of Inatatsu Adventar.

If you want to make a Discord bot, it's easier to use the Bot Commands Framework.

what is that

A framework that makes it really easy to create a discord bot with python (KONAMI)

Slowly

Import etc.

from discord.ext import commands
import traceback

INITIAL_EXTENSION = 'Kogu no Pass'

INITIAL_EXTENSION = 'cogs.hoge'

If so, you can register with this.

main

if __name__ == '__main__':
    bot = MyBot(command_prefix='!')
    bot.run('Please enter the token') 

command_prefix determines the identifier for recognizing the command. Here, "!" Is set as the identifier.

MyBot class

class MyBot(commands.Bot):

    #MyBot constructor.
    def __init__(self, command_prefix):
        super().__init__(command_prefix)
        try:
            self.load_extension(INITIAL_EXTENSION)
        except Exception:
            traceback.print_exc()

    #Event called when the bot is ready
    async def on_ready(self):
        print('--------in preparation-------')
        print(self.user.name)
        print(self.user.id)
        print('-------------------------')

Read the cog and display the error if an error occurs

hoge.py


from discord.ext import commands #Import of Bot Commands Framework
import discord # discord.Import py

class Hoge(commands.Cog):
    def __init__(self,bot):
        self.bot = bot
        self.players = None

    @commands.command(aliases=['h'])
    async def hello(self, ctx):
        await ctx.send(f'Hello! {ctx.author.name}Mr.!')

def setup(bot):
    bot.add_cog(Hoge(bot))

Now, when you do ! Hoge, it will return likeHello! Inatatsu-san! And I give ʻaliases = ['h']to the argument of@ commands.command (). Now you can create Hello! Inatatsu-san! Even with! H`.

You can also create subcommands, so use this when creating a discordbot with python.

Recommended Posts

If you want to make a discord bot with python, let's use a framework
Let's make a web framework with Python! (1)
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I want to make a game with Python
Let's make a Discord Bot.
[Super easy] Let's make a LINE BOT with Python.
If you want to make a TODO application (distributed) now using only Python
Let's make a graph with python! !!
Solution when you want to use cv_bridge with python3 (virtualenv)
If you want to become a data scientist, start with Kaggle
If you know Python, you can make a web application with Django
Let's make a shiritori game with Python
Don't write Python if you want to speed it up with Python
A note on what you did to use Flycheck with Python
I want to use a wildcard that I want to shell with Python remove
[Introduction to Python] Let's use foreach with Python
Let's make a voice slowly with Python
If you want to assign csv export to a variable in python
Gist repository to use when you want to try a little with ansible
If you want to include awsebcli with CircleCI, specify the python version
[Python] If you want to draw a scatter plot of multiple clusters
If you want to count words in Python, it's convenient to use Counter.
I want to use MATLAB feval with python
Make a Twitter trend bot with heroku + Python
If you want to create a Word Cloud.
Try to make a "cryptanalysis" cipher with Python
I want to use Temporary Directory with Python2
Steps to create a Twitter bot with python
Let's replace UWSC with Python (5) Let's make a Robot
Try to make a dihedral group with Python
I want to write to a file with Python
Two document generation tools that you definitely want to use if you write python
Try to make a command standby tool with python
I want to write in Python! (2) Let's write a test
If you want to use Cython, also include python-dev
[Let's play with Python] Make a household account book
When you want to filter with Django REST framework
I want to work with a robot in python.
[For play] Let's make Yubaba a LINE Bot (Python)
Site notes to help you use NetworkX with Python
How to operate Discord API with Python (bot registration)
I want to run a quantum computer with Python
[Python] I want to add a static directory with Flask [I want to use something other than static]
[Python] I want to use only index when looping a list with a for statement
Create a discord bot that notifies unilaterally with python (use only requests and json)
A convenient function memo to use when you want to enter the debugger if an error occurs when running a Python script.
Let's make a websocket client with Python. (Access token authentication)
How to make a Cisco Webex Teams BOT with Flask
[Django] A memorandum when you want to communicate asynchronously [Python3]
[Python] If you suddenly want to create an inquiry form
The first API to make with python Djnago REST framework
Create a Mastodon bot with a function to automatically reply with Python
I want to make a blog editor with django admin
Experiment to make a self-catering PDF for Kindle with Python
[python] A note when trying to use numpy with Cython
I want to make a click macro with pyautogui (desire)
Use aggdraw when you want to draw beautifully with pillow
I want to make a click macro with pyautogui (outlook)
[Python] I want to use the -h option with argparse