--- title: 20220729網頁組第一次BOT討論 tags : 網頁組, BOT image: --- # 網頁組第一次BOT討論(20220729) ## 今日討論內容 ### 張恩齊 [hackmd使用教學](https://hackmd.io/@Sunnus/ryzOn6A25) --- **[回復訊息]** "messageCreate"偵測訊息產生 ```javascript= client.on('messageCreate', async msg =>{ ...... } }); ``` Slash Command ```javascript= client.on('interactionCreate', async interaction => { if (!interaction.isChatInputCommand()) return; const { commandName } = interaction; if (commandName === '---') { await interaction.reply('---'); } }) ``` --- **[歡迎訊息]** 利用"GuildMemberAdd"偵測成員加入 ```javascript= client.on('GuildMemberAdd', async newMember =>{ await newMenber.channel.send(`Hi!, ${newMember}.`); } }); ``` --- **[依反應添加身分組]** 利用"messageReactionAdd"偵測反應產生 ```javascript= client.on('messageReactionAdd', async (reaction, user) => { const member = reaction.message.guild.members.cache.get(user.id); if (reaction.message.id === "messageId") switch (reaction.emoji.name) { case "1️⃣": member.roles.add("roleId"); break; } }); ``` 刪除就改成Remove ```javascript=37 client.on('messageReactionRemove', async (reaction, user) => { const member = reaction.message.guild.members.cache.get(user.id); if (reaction.message.id === "messageId") switch (reaction.emoji.name) { case "1️⃣": member.roles.add("roleId"); break; } }); ``` --- ### 黃俊翔 #### discord bot 偵測事件與讀取訊息內容之問題 - 偵測事件:無法偵測使用者加入、過去訊息反應 ```javascript= const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers,//伺服器成員 GatewayIntentBits.GuildMessages,//訊息 GatewayIntentBits.GuildMessageReactions,//訊息反應 GatewayIntentBits.MessageContent,//訊息內容 ], partials: [Partials.Message, Partials.Channel, Partials.Reaction],//過去訊息 }); ``` [相關參考文件](https://discord.com/developers/docs/topics/gateway#list-of-intents) 開啟機器人相關權限 ![](https://i.imgur.com/F6u0XqF.png) --- ### 黃昱維 #### 歡迎訊息 ```javascript= client.on('GuildMemberAdd', async (newMember) => { if (!newMember.isChatInputCommand()) return; if (Welcome === 'Hi!') { await newMember.reply(`Hi,${newMember}`); } }); ``` --- ## 下週主題 ### 斜線指令的按鈕、選單及嵌入式訊息 [參考資料](https://discordjs.guide/interactions/slash-commands.html#registering-slash-commands) --- ## 其他 下次會報為8/5 21:00