Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
Enabling and disabling command in JavaScript discord.js

I'm trying to make an automemes command, I can get it to send memes automatically, but when I try to disable it, it sends the Automemes disabled! command, but it still sends them. Here's the code:

const { SlashCommandBuilder } = require("@discordjs/builders");
const fetch = (...args) => import("node-fetch").then(({default: fetch}) => fetch(...args));

module.exports = {
    data: new SlashCommandBuilder()
    .setName("automemes")
    .setDescription("Sends random memes every 5 minutes (from r/memes)")
    .addBooleanOption(option =>
        option.setName("enabled")
        .setDescription("Set the automemes to on/off")
        .setRequired(true)),

    async execute(client, interaction, Discord) {

        let isEnabled = interaction.options.get("enabled").value;

        switch (isEnabled) {

            case true: interaction.reply("Automemes enabled! " + ENV.CATKISS)
            
            break;

            case false: isEnabled = false;

            break;
        }

        async function sendMemes() {

                fetch("https://meme-api.herokuapp.com/gimme/memes")
                .then(res => res.json())
                .then(async json => {
                    
                    const Embed = new Discord.MessageEmbed()
                    .setTitle(json.title)
                    .setImage(json.url)
        
                    if (Embed.title.length > 256) return;

                    await interaction.channel.send({embeds: [Embed]});
                });
        }

        isEnabled? setInterval(() => sendMemes(), 10000) : interaction.reply("Automemes disabled! " + ENV.CATKISS);
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I see what you're trying to accomplish but, as stated above by @Zsolt Meszaros, your interval is never getting cleared. That means that if that interval gets activated once, it will continue perpetually until your bot shuts down. Try declaring your interval as a constant so you can enable and disable it however you choose.

      const myInterval = setInterval(sendMemes, 10000) //declaring interval
      async fucntion startInterval() {
        myInterval; //function to start interval
      }
      async function disableInterval() {
        clearInterval(myInterval); //function to clear interval
        interaction.reply(interaction.reply("Automemes disabled! " + ENV.CATKISS));
      }
        isEnabled? startInterval() : disableInterval(); //added starter and stopper functions
    }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda