LoginSignup
0
1

Discord.pyのインストール方法

Discord.pyを使うにはまずPythonとpipがインストールされている必要があります。その上でpip install discord.pyコマンドでインストールできます。

基本的なBotの作成方法

Botのトークンを取得する方法
Discord Developersポータルから新しいアプリケーションを作成し、Botを追加します。するとTokenが発行されるのでこれをコピーしておきます。

Botを起動するための基本的なコード

import discord

client = discord.Client()

@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))

client.run('TOKEN')

このようなコードでBotを起動できます。

イベントハンドラーの設定方法

@client.event
async def on_message(message):
if message.content.startswith('$hello'):
await message.channel.send('Hello!')

のようにデコレーターを使ってイベントハンドラーを設定します。

Botの機能を追加する方法

コマンドの追加方法

@client.command()
async def hello(ctx):
await ctx.send('Hello!')

のようにコマンドデコレーターを使ってコマンドを追加できます。

リアクション追加の方法

@client.event
async def on_message(message):
if message.content.startswith('$thumb'):
await message.add_reaction('👍')

のようにadd_reactionメソッドを使ってリアクションを追加できます。

チャンネルの管理方法
チャンネルの作成、削除などを行うにはdiscord.TextChannelクラスの各種メソッドを使います。

Botをホストする方法

ローカルでのホスティング
Pythonスクリプトを実行したままにしておくことでローカルでホスティングできます。

クラウドサービスを使ったホスティング
HerokuやAWSなどのクラウドサービスを使えば24時間安定してホスティングできます。

Discord.pyのドキュメントや参考になるサイト・コミュニティの紹介

公式ドキュメント:
https://discordpy.readthedocs.io/en/latest/

Discord.py公式サーバー:
https://discord.gg/dpy

Qiita Discord.pyタグ:
https://qiita.com/tags/discord.py

0
1
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
0
1