首页 > 解决方案 > Discord bot 不执行命令 (discord.js v11.5.1)

问题描述

昨晚我重写了我的 index.js,现在它不再执行命令了。我已经尝试更新和降级我的 discord.js 版本,但没有任何改变,我尝试使用我的旧 index.js 并且由于某种原因也不起作用。

这是我的 index.js:

const {Client} = require('discord.js')
const bot = new Client()

const prefix = "%";
const owners = [''];

bot.login('')

bot.on('ready', (msg) => {
  console.log(`${bot.user.tag}: [${new Date(Date.now()).toUTCString()}]`);
});

bot.on("message", async (message) => {
    if(!message.author.bot) return;
    
    if(!message.content.startsWith(prefix)) return;
  
    let args = message.content.slice(prefix.length).trim().split(/ +/g);
    
    let path = `./commands/${args.shift().toLowerCase()}.js`;

    if(!require("fs").existsSync(path)) return;

    const command = require(path);
    
    if(!command.ownersOnly) command.run(bot, message, args);
    else {return message.channel.send('You are not allowed to do that!')}
});

bot.on('message', (msg) => {
  if(msg.content === "prefix") {
    msg.reply("Prefix: " + prefix)
  }
});

bot.on("messageDelete", (m) => {
  if(m.author.bot) return
  const path = __dirname + "/snipe.json"
  let snipeFile = require(path)
  
  snipeFile[m.channel.id] = [m.author.id, m.content, m.attachments.first() ? m.attachments.first().proxyURL : undefined]
  
  require("fs").writeFileSync(path, JSON.stringify(snipeFile, null, 2))
});

谢谢!

标签: javascriptnode.jsdiscorddiscord.js

解决方案


您有一张支票,上面写着if(!message.author.bot) return;如果作者不是机器人,则返回,因此您必须!从该支票中删除


推荐阅读