首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不和谐的js v13消息应答不起作用(前缀)

不和谐的js v13消息应答不起作用(前缀)
EN

Stack Overflow用户
提问于 2022-02-22 16:44:11
回答 1查看 624关注 0票数 0

我在制造不和谐的机器人,用前缀回答用户的单词。例如,用户说!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.) },};

EN

回答 1

Stack Overflow用户

发布于 2022-02-24 21:12:45

index.js (或主文件)的代码如下:

代码语言:javascript
运行
复制
  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 -

代码语言:javascript
运行
复制
module.exports={
   name:'ping', 
   execute(message, arg){
       message.reply('hello!!')
   }
}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71225103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档