Try   HackMD

Discord.py 機器人從0到1超詳細教學

本教學相關教學綜合網上諸多教學及本人實際操作,歡迎參考及補充!

創建新的機器人

  1. 首先到 Discord Developers,登入你的 Discord 帳號後!
  2. 點擊「New Application」,輸入完你想要的名稱後就按下「Create」,之後都還可以做更改這點不用擔心.w.
  3. 左上點擊後會出現側邊欄,點擊Bot,按下「Add Bot」就可以創造一隻機器人咯!
  4. 前往「OAuth2」, 在「SCOPES」中點選 bot,下方連接就是機器人邀請連結,就可以將機器人邀請進去你自己的群咯!

安裝 Python

  1. 前往官網下載(https://www.python.org/downloads/)
  2. 安裝

安裝 pip 及 Discord.py

第一種方法,可以參考(https://hackmd.io/@yizhewang/B1zdXG4br) 將 Python 加到系統路徑
第二種方式,可以開啟 CMD 直接用 cd 指令到 Python 所在資料夾

可以用

python --version

來檢查是否可以正確執行。

最後,安裝 discord.py

python -m pip install -U discord.py

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

完成截圖

2022.12.9 更新 intents 性質性質

Discord.py 在定義 discord.Client 時需要額外帶入 intents 參數

#client 是我們與 Discord 連結的橋樑,intents 是我們要求的權限 client = discord.Client(intents=discord.Intents.default())

或是想帶入個別權可以使用以下寫法:

client = discord.Client(intents=discord.Intents(messages=True, guilds=True))

通常我會習慣用 default + message_content 的權限,讓機器人有講話的權利!

#client 是我們與 Discord 連結的橋樑,intents 是我們要求的權限 intents = discord.Intents.default() intents.message_content = True client = discord.Client(intents=intents)

intents 可以用目前已經有的三種模板

其他 intents 可以從這邊查到,通常用 default 即可:https://discordpy.readthedocs.io/en/stable/api.html#intents
記得也要到 Discord Develop 頁面去修正

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

開始啟動機器人,讓他可以上線

#導入 Discord.py import discord #client 是我們與 Discord 連結的橋樑,intents 是我們要求的權限 intents = discord.Intents.default() intents.message_content = True client = discord.Client(intents=intents) #調用 event 函式庫 @client.event #當機器人完成啟動時 async def on_ready(): print('目前登入身份:', client.user) @client.event #當有訊息時 async def on_message(message): #排除自己的訊息,避免陷入無限循環 if message.author == client.user: return #如果包含 ping,機器人回傳 pong if message.content == 'ping': await message.channel.send('pong') client.run('你的機器人 TOKEN') #TOKEN 在剛剛 Discord Developer 那邊「BOT」頁面裡面

event 函式庫 API 查詢:https://discordpy.readthedocs.io/en/latest/api.html#event-reference
discord.py 官方API:https://discordpy.readthedocs.io/en/latest/api.html

讓他說你說的話

#導入Discord.py import discord #client 是我們與 Discord 連結的橋樑,intents 是我們要求的權限 intents = discord.Intents.default() intents.message_content = True client = discord.Client(intents=intents) #調用event函式庫 @client.event #當機器人完成啟動時 async def on_ready(): print('目前登入身份:',client.user) @client.event #當有訊息時 async def on_message(message): #排除自己的訊息,避免陷入無限循環 if message.author == client.user: return #如果以「說」開頭 if message.content.startswith('說'): #分割訊息成兩份 tmp = message.content.split(" ",2) #如果分割後串列長度只有1 if len(tmp) == 1: await message.channel.send("你要我說什麼啦?") else: await message.channel.send(tmp[1]) client.run('你的機器人TOKEN') #TOKEN在剛剛Discord Developer那邊「BOT」頁面裡面

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

設定機器人狀態

#導入Discord.py import discord #client 是我們與 Discord 連結的橋樑,intents 是我們要求的權限 intents = discord.Intents.default() intents.message_content = True client = discord.Client(intents=intents) #調用event函式庫 @client.event #當機器人完成啟動時 async def on_ready(): print('目前登入身份:',client.user) game = discord.Game('努力學習py中') #discord.Status.<狀態>,可以是online,offline,idle,dnd,invisible await client.change_presence(status=discord.Status.idle, activity=game) @client.event #當有訊息時 async def on_message(message): #排除自己的訊息,避免陷入無限循環 if message.author == client.user: return #如果以「說」開頭 if message.content.startswith('說'): #分割訊息成兩份 tmp = message.content.split(" ",2) #如果分割後串列長度只有1 if len(tmp) == 1: await message.channel.send("你要我說什麼啦?") else: await message.channel.send(tmp[1]) if message.content.startswith('更改狀態'): #切兩刀訊息 tmp = message.content.split(" ",2) #如果分割後串列長度只有1 if len(tmp) == 1: await message.channel.send("你要改成什麼啦?") else: game = discord.Game(tmp[1]) #discord.Status.<狀態>,可以是online,offline,idle,dnd,invisible await client.change_presence(status=discord.Status.idle, activity=game) client.run('你的機器人TOKEN') #TOKEN在剛剛Discord Developer那邊「BOT」頁面裡面


輸入
更改狀態 啊啊啊啊好難啊

開始跟機器人互動吧

#導入Discord.py import discord import time #client 是我們與 Discord 連結的橋樑,intents 是我們要求的權限 intents = discord.Intents.default() intents.message_content = True client = discord.Client(intents=intents) #調用event函式庫 @client.event #當機器人完成啟動時 async def on_ready(): print('目前登入身份:',client.user) game = discord.Game('努力學習py中') #discord.Status.<狀態>,可以是online,offline,idle,dnd,invisible await client.change_presence(status=discord.Status.idle, activity=game) @client.event #當有訊息時 async def on_message(message): if message.content.startswith('跟我打聲招呼吧'): channel = message.channel #機器人叫你先跟他說你好 await channel.send('那你先跟我說你好') #檢查函式,確認使用者是否在相同頻道打上「你好」 def checkmessage(m): return m.content == '你好' and m.channel == channel #獲取傳訊息的資訊(message是類型,也可以用reaction_add等等動作) msg = await client.wait_for('message', check=checkmessage) await channel.send('嗨, {.author}!'.format(msg)) client.run('你的機器人TOKEN') #TOKEN在剛剛Discord Developer那邊「BOT」頁面裡面

我就是要嗆你

#導入Discord.py import discord #為了asyncio.sleep() import asyncio #client 是我們與 Discord 連結的橋樑,intents 是我們要求的權限 intents = discord.Intents.default() intents.message_content = True client = discord.Client(intents=intents) @client.event #當有訊息時 async def on_message(message): if message.content == '我好帥喔': #發送訊息,並將本次訊息資料存入tmpmsg,方便之後刪除 tmpmsg = await message.channel.send('你確定你帥嗎?') #停頓3秒 await asyncio.sleep(3) #刪除訊息 await tmpmsg.delete() client.run('你的機器人TOKEN') #TOKEN在剛剛Discord Developer那邊「BOT」頁面裡面

這樣下來我只要輸入「我好帥喔」,機器人就會回嗆我「你確定你帥嗎?」,但三秒後會自動刪除

我就是要嗆你-延伸版

#導入Discord.py import discord import time #client 是我們與 Discord 連結的橋樑,intents 是我們要求的權限 intents = discord.Intents.default() intents.message_content = True client = discord.Client(intents=intents) @client.event #當有訊息時 async def on_message(message): if message.content == '我好帥喔': #刪除傳送者的訊息 await message.delete() #然後回傳訊息 await message.channel.send('不好意思,不要騙人啦') client.run('你的機器人TOKEN') #TOKEN在剛剛Discord Developer那邊「BOT」頁面裡面

這樣下來我只要輸入「我好帥喔」,機器人就會直接刪除我的訊息,並且說「不好意思,不要騙人啦」

我人在哪?

#導入Discord.py import discord import time #client 是我們與 Discord 連結的橋樑,intents 是我們要求的權限 intents = discord.Intents.default() intents.message_content = True client = discord.Client(intents=intents) @client.event #當有訊息時 async def on_message(message): if message.content == '群組': #獲取當前所在群組(極限150個,預設為100個),並用flatten將它全部移到guilds這個list裡面 guilds = await client.fetch_guilds(limit=150).flatten() #遍尋 guilds for i in guilds: #由於我們只要 guilds 的name 就好,當然也可以獲取 id~ await message.channel.send(i.name) client.run('你的機器人 TOKEN') #TOKEN 在剛剛 Discord Developer 那邊「BOT」頁面裡面

輸入「群組」後,機器人就會打出目前所在的群組

作者聯絡資訊

個人網站:https://kangjw.me
Email:hi@kangjw.me