5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

discord.pyでコマンドを組み込む方法

Last updated at Posted at 2021-10-29

discord.pyでcommandを使う

disocrdでbotのコマンドを組み込む方法として簡単なのは組み込まれているon_message()を使っている人も多いかと思います.しかしon_message()を用いている場合,条件分岐でやや工夫が必要になることがあり,可読性が下がると思います.そこでdiscordpyが用意している拡張を利用してこの記事ではコマンドを組み込んでいこうと思います.

もともとのon_message()を利用した場合

import discord

TOKEN = ''
client = discord.Client()

@client.event
async def on_message(message):
    if message.author.bot:
        return
    if message.content == '!good':
        await message.channel.send('evening')

client.run(TOKEN)

discordの拡張を利用した場合

import discord
from discord.ext import commands

TOKEN = ''
bot = commands.Bot(command_prefix='!')

@bot.command()
async def good(ctx):
    await ctx.send('evening')

bot.run(TOKEN)

このようにすることで簡潔に書くことができます.

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?