🤖【Node.js】快速教你如何創建 Discord Bot 💬
Discord Bot 是一種在 Discord 社交平臺上運行的軟件機器人。這些機器人可以完成各種任務,例如播放音樂、查找信息、管理服務器等。使用者可以通過 Discord 應用程序與這些機器人進行互動,通過命令行或聊天命令與其進行互動。 Discord Bot 的開發需要一定的程序設計知識,因此不是所有用戶都可以輕松地創建自己的機器人。
文章目錄
- 登入 Discord 開發後台
- 創建應用
- 設置圖片 名稱 描述
- 創建機器人
- 取得 Token (不要給任何人知道)
- 設定 OAuth2 URL
- 導入到 Discord
- 創建文字頻道
- DisocrdDemo.js
- 創建完成
1.登入 Discord 開發後台
Discord Developers
2.創建應用


3.設置圖片 名稱 描述


4.創建機器人



5.取得 Token (不要給任何人知道)


開啟接受訊息權限

9.設定 OAuth2 URL



7.導入到 Discord
把剛剛設定好的 URL 貼到瀏覽器上 選擇伺服器

確認權限

導入完成

未開啟 所以離線狀態

8.創建文字頻道

設定頻道名稱


9.DisocrdDemo.js
安裝 discord.js
npm i
npm i discord.js
DisocrdDemo.js
const {Client, Events, GatewayIntentBits, ActivityType} = require("discord.js")
// 設定 Discord 客戶端
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
})
// 設定 BOT 狀態
client.on(Events.ClientReady, client => {
//串流直播專屬 不用設定status
client.user.setActivity('色色的影片', {
type: ActivityType.Streaming,
url: "https://www.youtube.com/watch?v=tkYr3nY1WX8"
});
client.user.setStatus('online')
console.log(`開啟成功 {client.user.tag}`);
});
// 監聽訊息
client.on(Events.MessageCreate, message => {
try {
// 機器人給的訊息不理
if (message.author.bot) return
// 機器聊天區頻道
if (message.channelId === "頻道的ID") {
message.reply(`你竟然跟我說{message.content}`)
}
} catch (e) {
console.log(e)
}
});
// 開啟機器人
client.login("一開始拿到的 Token")
啟動
node .\DisocrdDemo.js
Discord Bot 活動與狀態
ActivityType.Watching 正在看
ActivityType.Listening 正在聽
ActivityType.Streaming 正在直播
ActivityType.Playing 正在玩
ActivityType.Competing 競爭
online 線上
idle 閒置
dnd 請勿打擾
invisible 隱形
取得頻道 ID

10.創建完成

