以解決上面的問題
但是又有問題出現了
再輸入node bot之後 他跑出上圖的問題 希望各位邦友能幫忙解決一下
原本的:現在變成}了 附上auth.json的照片
bot.js
const Discord = require('discord.js');
const client = new Discord.Client();
const auth = require('./auth.json');
client.login(auth.key);
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (msg.content === 'ping') {
msg.reply('pong!'),
}
});
package.json
{
"name": "kjyang",
"version": "1.0.0",
"description": "kjyang",
"main": "bot.js",
"scripts": {"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
跟discord申請好,然後跟你的guild取得權限後,就可以在你的guild上跑你的機器人。一個目前版本(13)可以跑的例子:
const auth = require('./auth');
const { Client, Intents } = require('discord.js');
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS],
partials: ['MESSAGE', 'CHANNEL', 'REACTION']
});
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('messageCreate', async message => {
try {
if(message.partial) await message.fetch();
} catch(e) {
console.log(e);
}
if(message.content.indexOf('google') > -1) {
await message.reply('try yahoo!!');
}
});
client.on('interactionCreate', async interaction => {
if (interaction.isCommand() && interaction.commandName === 'google') {
await interaction.reply('yahoo!');
}
});
client.login(auth.key);
這個例子註冊了一個/google
命令,輸入命令他會回覆yahoo!
。另外,他會監看訊息,發現有人輸入google,就會回覆try yahoo!!
。(只是好玩,我跟yahoo沒關係的)
運行畫面: