简体   繁体   English

命令交互无响应

[英]Command interaction not responding

this code is not executing the desired function which is to be able to initiate a message to repeat on a set interval.此代码未执行所需的 function,它能够启动消息以在设定的时间间隔内重复。 When debugging there are no errors and the bot comes online fine but the command can't be used and it says nothing to suggest what the problem is.调试时没有错误,机器人可以正常联机,但无法使用该命令,它没有说明问题所在。 I need a professionals help to identify the reason this code is not working and why the bot acts as if it does not even exist.我需要专业人士的帮助来确定此代码不起作用的原因以及机器人为何表现得好像它甚至不存在一样。 This is the shortest amount of code to replicate the results I have left out the config.json because it has my token but I know for sure that that file is configured properly anyway这是复制结果的最短代码量我省略了 config.json 因为它有我的令牌但我确定该文件无论如何配置正确

Test.ts file in commands folder命令文件夹中的 Test.ts 文件

const Discord = require("discord.js");
const source = require('../index');
module.exports.run = (Client, message, args) => {
  if (!args [0]) return message.reply(`Please specify if you are turning the command on or off!`);
  if (args[1]) return message.reply(`Please specify if you are turning the command on or off! [Too many Arguments!]`);
  switch (args[0]) 
  {
    default:
    {
      message.reply('Invalid argument specified.')
      break;
    }
    case "on":
      {
        if (!source.timedCheck){
          source.timedCheck =setInterval(() =>{
            // Function for set interval
            console.log("Interval cycle run!" + (val++) + "times!");
            valcheck();
          }, 25 * 10000);
          message.reply('command started!');
        } else {
          return message.reply(`command already running!`)
        }
        break;
      }
    case "off":
      {
        if (source.timedCheck){
        message.reply(`user has turned off command!`);
        clearInterval(source.timedCheck);
        source.timedCheck = undefined;
        } else {
          return message.reply(`command already offline!`)
        }
        break;
      }  
  }

 let valcheck = () => {
   if (source.val > 5){
     clearInterval(source.timedCheck);
     source.timedCheck = undefined;
     message.channel.send(`command finished as scheduled!`);  
   } 
 };
};

module.exports.help = {
  name: "test",
  usage: "test <on/off>"
};

Index.js file索引.js 文件

// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');

// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

// When the client is ready, run this code (only once)
client.once('ready', () => {
    console.log('Ready!');
});

// Export variables for commands
module.exports.timedCheck = undefined;
module.exports.val = 0;

// Login to Discord with your client's token
client.login(token);

You didn't tell the bot to execute this command.你没有告诉机器人执行这个命令。 In your Index file you have to require the command file, catch the "message" or the "interactionCreate" events and then execute your command.在您的索引文件中,您必须需要命令文件,捕获“消息”或“交互创建”事件,然后执行您的命令。 This code is incomplete and I can only guide you in a direction as you miss many things in your code.这段代码是不完整的,我只能指导你一个方向,因为你错过了代码中的很多东西。

You should read a tutorial: https://discordjs.guide/creating-your-bot/creating-commands.html#command-deployment-script您应该阅读教程: https://discordjs.guide/creating-your-bot/creating-commands.html#command-deployment-script

client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./src/commands');

// require every command and add to commands collection
for (const file of commandFiles) {
  const command = require(`./src/commands/${file}`);
  client.commands.set(command.name, command);
}

client.on('message', async message => {

   // extract your args from the message, something like:
   const args = message.content.slice(prefix.length).split(/ +/);

   try {
      command.execute(message, args, client); // provide more vars if you need them
   } catch (error) {
      // handle errors here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM