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.
A framework that makes it really easy to create a discord bot with python (KONAMI)
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