ManicModder
ManicModder

Reputation: 11

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. 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

Test.ts file in commands folder

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

// 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);

Upvotes: 1

Views: 203

Answers (1)

omnomnom
omnomnom

Reputation: 302

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

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
}

Upvotes: 1

Related Questions