# 使用Github開源建立自己的Discord Bot ## 需求 * Node.js 16+ * VS code(或其他可以編碼的軟體) * Lavalink Server ## 使用的開源項目 https://github.com/SudhanPlayz/Discord-MusicBot ![](https://images-ext-1.discordapp.net/external/JF_4vSA7HmgvgFZI1GtmPr8LJHgQ22oozUpH0y6gdik/https/opengraph.githubassets.com/d320261391b0391c430296f8e2e70d12d8902b167999462d9a77805f94e03fdd/SudhanPlayz/Discord-MusicBot?width=1340&height=670) ### 功能介紹 * 支援Youtube、SoundCloud等等平台 * 斜線指令 * 網頁控制台(僅能查看目前撥放的音樂) ## 注意事項 根據開源項目的LICENSE ### **機器人代碼應僅用於私人託管和個人使用** ### **將程式碼用於公共用途是不允許的** ## 建立Discord Bot 到 Discord Developer Portal 創建一個App ![](https://hackmd.io/_uploads/H1uTcHMZT.png) ## 將程式碼下載下來 可以直接按右上Download zip 或是 git clone(前提是電腦有安裝git) ![](https://hackmd.io/_uploads/Syz0hBfb6.png) ``` $ git clone https://github.com/SudhanPlayz/Discord-MusicBot.git ``` ## 修改config.js ### config.js的內容: 每項設定可以依自己需求更改,但還是要注意哪些不能碰,不過我都幫你註解了<3 ``` module.exports = { helpCmdPerPage: 10, //- /help指令每頁指令數 lyricsMaxResults: 5, //- 不要碰 adminId: "UserId", //- 設定管理員,填入你的Discord ID token: process.env.token || "", //- 機器人的token clientId: process.env.clientId || "", //- 機器人的ID clientSecret: process.env.clientSecret || "", //- 機器人的Client secret port: 4200, //- API和網頁控制台的port,依需求更改,否則保留不變 scopes: ["identify", "guilds", "applications.commands"], //- 不要碰 inviteScopes: ["bot", "applications.commands"], // 這也不要碰 serverDeafen: true, //- 機器人是否設為拒聽 defaultVolume: 100, //- 音樂播放時的聲音大小,建議50 supportServer: "https://discord.gg/sbySMS7m3v", //- 支援伺服器,預設為官方的就行 Issues: "https://github.com/SudhanPlayz/Discord-MusicBot/issues", //- 機器人的錯誤回報 permissions: 277083450689, //- 機器人權限,不用改 disconnectTime: 30000, //- 設定語音裡持續幾秒都沒有人便自動斷線(單位為毫秒),設定"1"則會立即斷線 twentyFourSeven: false, //- 設定為true,則機器人會永遠待在語音頻道,直到你手動將其斷線 autoQueue: false, //- 設為true,則機器人會在你指定的音樂撥放完畢後自動加入相關的歌曲(類似自動撥放) autoPause: true, //- 設為true,當所有人離開語音頻道後,機器人會自動暫停撥放音樂 autoLeave: false, //- 設為true,機器人會在所有人離開語音頻道後自動斷線 debug: false, //- 除錯模式,只有在你知道你在幹嘛的時候才能開啟這個選項 cookieSecret: "CodingWithSudhan is epic", //- 不要碰 website: "http://localhost:4200", //- 如果是在電腦上執行則不需要更改,如果是在雲端主機上執行則依需求更改 // Lavalink server; 連線到公共Lavalink -> https://lavalink-list.darrennathanael.com/; 自己創建一個 -> https://darrennathanael.com/post/how-to-lavalink nodes: [ { identifier: "Main Node", //- 不用改 host: "", //- Lavalink 的連線位置 port: 80, // Lavalink的port password: "", //- Lavalink密碼 retryAmount: 200, //- 連接斷開時重試連接的次數。 retryDelay: 40, //- 重試連線的間隔 secure: false, //- 是否有ssl }, ], embedColor: "#2f3136", //- Discord embed顏色 支援hex color // 狀態顯示 presence: { // PresenceData object | https://discord.js.org/#/docs/main/stable/typedef/PresenceData status: "online", //- 選項:online, idle, dnd invisible (注意: invisible 會讓其他人以為機器人未開啟) activities: [ { name: "Music", //- 狀態文字 type: "LISTENING", //- 選項:PLAYING, WATCHING, LISTENING, STREAMING }, ], }, iconURL: "https://cdn.darrennathanael.com/icons/spinning_disk.gif", //- 每個embed中會顯示的圖標 }; ``` ### 設定bot token、Client ID、Client secret 回到 Discord Developer Portal 將複製好的token貼到process.env.token後的""中 ![](https://hackmd.io/_uploads/Bys7dwMZp.png) 複製Client ID和 secret 並貼在process.env.clientId與process.env.clientSecret後的""中 ![](https://hackmd.io/_uploads/B1L-YvfWa.png) ### Lavalink 你可以選擇連線到公共的或是自己架設 以連線到公共的為例 ![](https://hackmd.io/_uploads/BkiCpPG-T.png) ``` nodes: [ { identifier: "Main Node", //- Used for indentifier in stats commands. host: "lava3.horizxon.tech", //- The host name or IP of the lavalink server. port: 443, // The port that lavalink is listening to. This must be a number! password: "horizxon.tech", //- The password of the lavalink server. retryAmount: 200, //- The amount of times to retry connecting to the node if connection got dropped. retryDelay: 40, //- Delay between reconnect attempts if connection is lost. secure: true, //- Can be either true or false. Only use true if ssl is enabled! }, ], ``` 最後請記得Ctrl+S儲存喔<3 ## 啟動機器人 如果你是用自己的電腦啟動bot則需要先安裝需要的套件,託管請跳至第三步驟 1. 安裝套件 ``` $ npm install ``` 2. 初始化斜線指令 ``` $ npm run deploy ``` 3. 啟動機器人 ``` $ node index.js ``` 如果顯示以下訊息就表示啟動成功 ``` [10:9:2023 - 14:18] | Node: Main Node | Lavalink node is connected. ``` 而你的網頁控制台會在 http://localhost:4200 api設定可以參考[這裡](https://github.com/SudhanPlayz/Discord-MusicBot/wiki/Installation-on-a-Linux-server#dashboard-with-ip)