我在制造不和谐的机器人,用前缀回答用户的单词。例如,用户说!Hello
,机器人将用Hello
回复
错误是(node:11208) DeprecationWarning: The message event is deprecated. Use messageCreate instead (Use node --trace-deprecation ... to show where the warning was created)
因此,我将代码消息更改为messageCreate,但机器人也没有回复,控制台日志中也没有错误,我应该更改脚本的代码吗?
这是源代码prefix is ! (It is in the config.json)
index.js
const { Client,Intents,Collection} =需要量(“discord.js”);const client =新客户端({intents:Intents.FLAGS.GUILDS,Intents.FLAGS.GUILD_MESSAGES,Intents.FLAGS.GUILD_MEMBERS}) const fs = require("fs");const { token,前缀}= require("./config.json");client.commands =新集合();const commandFiles =fs.readdirSync(“./命令”).filter((文件) => file.endsWith(".js"));var data = [] for(const file of commandFiles) { const命令= require(./commands/${file}
);client.commands.set(command.name,命令);data.push({name:command.name,description:command.description,options:command.options})} client.guilds.cache.get('874178319434801192')?.commands.set(data) client.once(“就绪”)、异步() =>{ console.log(Logged in as ${client.user.tag}
) //等待console.log setInterval() => { const状态= 'mimc!‘、'wtf’、‘前缀= !’const = statusesMath.floor(Math.random()*statuses.length) client.user.setActivity( status,{type:“PLAYING”} },10000) };client.on("messageCreate",(message) =>{ if(!message.content.startsWith(前缀)x x message.author.bot)返回;const = message.content.slice(prefix.length).trim().split(/ +/;const命令= args.shift();如果(!client.commands.has(命令))返回;尝试{client.commands.get(命令).execute(消息,args);}catch(错误){console.error(错误);});client.login(令牌) Hello.js (在命令文件夹中)
module.exports ={ name:"Hello",description:"Say Hi",execute(消息){返回message.channel.send(${message.author}, Hello.
) },};
发布于 2022-02-24 21:12:45
index.js (或主文件)的代码如下:
const {Client, Intents, MessageEmbed, Presence, Collection, Interaction}= require ('discord.js')
const client = new Client({intents:[Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]})
const TOKEN = ''// get your TOKEN!
const PREFIX = '!'
const fs = require('fs')
client.commands = new Collection();
const commandFiles = fs.readdirSync('commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.on('ready', ()=> {
console.log(' I am online!!')
client.user.setActivity('A GAME ', {type: ('PLAYING')})
client.user.setPresence({
status: 'Online'
})
})
client.on('message', message => {
if(!message.content.startsWith(PREFIX)|| message.author.bot) return
const arg = message.content.slice(PREFIX.length).split(/ +/)
const command= arg.shift().toLowerCase()
if (command === 'ping'){
client.commands.get('ping').execute(message,arg)
}
})
client.login(TOKEN)
在创建了一个文件夹命令之后,添加一个新的.js文件,并将其命名为 ping.js -
module.exports={
name:'ping',
execute(message, arg){
message.reply('hello!!')
}
}
https://stackoverflow.com/questions/71225103
复制相似问题