1
1

とりあえず動けばいい人向けDiscordBotの作成手順(python)

Posted at

とりあえず難しいのいいから動けばいいや的な人向けのDiscord Bot作成手順

1.Discord Botの作成

image.png

New Applicationを選択

image.png
このBotのTOKENを取得

image.png
Botという項目のPrivileged Gateway Intentsをすべて選択する

image.png

OAuth2という項目に移動
OAuth2 URL Generatorの
・SCOPESを「bot」
・BOT PERMISSIONSを「Adminster」
にチェックを入れる

そのまま下にスクロールするとリンクがあるのでコピーして、検索する

以下のような画面が出る
image.png
ここで導入したいサーバーを選択する
これでBotの導入は完了。

2.Botをコードで動かす

サンプルコード

sample.py
import discord


TOKEN = "Your_DiscordBot_TAKEN"
channel_ID = "Channel_ID"#チャンネルを1つ指定する場合


####ここら辺はおまじないと思ってもいい

intents = discord.Intents.all()
#↑上記でPrivileged Gateway Intentsをすべて選択したのはこう書くのが楽だと考えてたから

intents.messages = True

client = discord.Client(intents=intents)


####

@client.event
async def on_ready():
    print('ログインしました')
      

@client.event
async def on_message(message):

    if message.author.bot:#Botのには反応しない
        return

    if message.channel.id != int(channel_ID):#指定のチャンネルのみに反応
        return    
    
    #なにかメッセージが来た場合に「こんちわ」返す
    await message.channel.send("こんちわ")

client.run(TOKEN)  

3.どんなふうになるか

image.png

指定のチャンネルでなにかテキストを打つと「こんちわ」と応答されます。
別のチャンネルでは何も反応しません。

おまけ

指定の語が来た場合返す

sample.py
if message.content == 'はろー':
        await message.channel.send("ちっすちっす")
1
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
1
1