首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >为什么会出现js命令处理程序错误?

为什么会出现js命令处理程序错误?
EN

Stack Overflow用户
提问于 2022-07-10 01:42:42
回答 1查看 76关注 0票数 -1

我尝试过实现一个命令处理程序,所以我可以生成嵌入命令,因为我在index.js文件中嵌入代码时遇到了很多错误,所有教程中的每个人都使用命令处理程序。因此,无论如何,我一步一步地执行命令,现在我得到了错误。这是机器人代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
const express = require("express");
const app = express();

const discord = require("discord.js");
const client = new discord.Client({intents:["GUILDS","GUILD_MESSAGES"]});

const fs = require("fs");
var read_data = fs.readFileSync("./userdata.json");
var datafile = JSON.parse(read_data);

const levels_api = require("mee6-levels-api");

var bump_timeout = true;

var port = process.env.PORT || 3000;

var eventFiles = fs.readdirSync("./events").filter(file => file.endsWith('.js'));

for(const file of eventFiles){
  const event = require(`./events/${file}`);

  if(event.once){
    client.once(event.name, (...args) => event.execute(...args, commands));
  }
  else{
    client.on(event.name, (...args) => event.execute(...args, commands));
  }
}

app.listen(port, "0.0.0.0", function() {
console.log("Listening on Port 3000");
});


client.on("message", message => {
  var msg_value = message.content.toLowerCase();
  var username = message.author.username;
  var id = message.author.id;
  var user_joined = new Date(message.author.joinedTimestamp).toLocaleDateString();
  if(msg_value.includes('hello') && msg_value.includes('bot') || msg_value.includes('hi') && msg_value.includes('bot')){
    message.channel.send("hi dad");
  }
  if(message.content === "!d bump" && bump_timeout==true){
    if(!datafile[id]){
      datafile[id] = {coins: 100};
      fs.writeFileSync("./userdata.json", JSON.stringify(datafile, null, 2));
      message.channel.send(`<@${id}> got 100 coins!`);
    }
    else{
      var chillcoins = Number(datafile[id].coins) + 100;
      message.channel.send(`<@${id}> got ${chillcoins} coins!`);
      datafile[id] = {coins: chillcoins};
      fs.writeFileSync("./userdata.json", JSON.stringify(datafile, null, 2));
    };
    bump_timeout = false;
    setTimeout(bump_switchtimeout, 72000000);
  };
  if(message.content.startsWith('!squid') && message.content.includes('bal')){
    var mentioned_user = message.mentions.users.first() || message.member.user;
    var mentioned_user_id = mentioned_user.id;
    if(!datafile[mentioned_user_id]){
      datafile[mentioned_user_id] = {coins: 0};
      fs.writeFileSync("./userdata.json", JSON.stringify(datafile, null, 2));
      message.channel.send(`<@${mentioned_user_id}> has 0 coins`);
    }
    else{
      var coins = Number(datafile[mentioned_user_id].coins);
      message.channel.send(`<@${mentioned_user_id}> has ${coins} coins`);
    }
  }
  if(msg_value == "!squid help"){
    const help_embed = new discord.MessageEmbed
      .setTitle("Squid bot Commands")
      .addFields(
            { name: 'Hi Bot', value: 'You greet our bot' },
            { name: '!squid help', value: 'You call the ambulance' },
            { name: '!d bump', value: 'Bumping the server will give you chillcoins', inline: true },
            { name: '!squid bal', value: 'This command displays your chillcoins', inline: true },
      );
    message.channel.send({embeds: [help_embed]});
  }

  
})

function bump_switchtimeout(){
  bump_timeout = true;
}

client.on("ready", () => {
  console.log("bot is ready");
  client.user.setActivity({name: "chilling", type: "PLAYING"});
})


client.login("token");

这是错误消息:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
ReferenceError: commands is not defined
    at Client.<anonymous> (C:\Users\User\Documents\GitHub\Squidbot1\index.js:23:65)
    at Object.onceWrapper (node:events:646:26)
    at Client.emit (node:events:538:35)
    at WebSocketManager.triggerClientReady (C:\Users\User\Documents\GitHub\Squidbot1\node_modules\discord.js\src\client\websocket\WebSocketManager.js:379:17)
    at WebSocketManager.checkShardsReady (C:\Users\User\Documents\GitHub\Squidbot1\node_modules\discord.js\src\client\websocket\WebSocketManager.js:362:10)
    at WebSocketShard.<anonymous> (C:\Users\User\Documents\GitHub\Squidbot1\node_modules\discord.js\src\client\websocket\WebSocketManager.js:189:14)
    at WebSocketShard.emit (node:events:526:28)
    at WebSocketShard.checkReady (C:\Users\User\Documents\GitHub\Squidbot1\node_modules\discord.js\src\client\websocket\WebSocketShard.js:509:12)
    at WebSocketShard.onPacket (C:\Users\User\Documents\GitHub\Squidbot1\node_modules\discord.js\src\client\websocket\WebSocketShard.js:481:16)
    at WebSocketShard.onMessage (C:\Users\User\Documents\GitHub\Squidbot1\node_modules\discord.js\src\client\websocket\WebSocketShard.js:317:10)
EN

回答 1

Stack Overflow用户

发布于 2022-07-10 02:54:23

commands在任何地方都没有定义,至少在您发布的代码中没有定义,所以不要使用不存在的变量

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
// client.once(event.name, (...args) => event.execute(...args, commands));
client.once(event.name, (...args) => event.execute(...args));
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
// client.on(event.name, (...args) => event.execute(...args, commands));
client.on(event.name, (...args) => event.execute(...args));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72927540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文