【问题标题】:How to make this function start via a slash command如何通过斜杠命令启动此功能
【发布时间】:2022-01-20 22:05:27
【问题描述】:

我有一段代码每 4 小时自动运行一次,以将设置的消息发送到我的不和谐服务器上的特定不和谐频道,但我希望此功能在命令时启动,我希望该命令位于如果有人能给我这个代码的模板作为斜杠命令,我将不胜感激,你会为我节省数天可能数周的繁琐研究和反复试验我需要最新的代码discord.js v13 并且将与我的主索引文件代码兼容,该代码将设置在我想要完成的特定功能下方。

setInterval(() => {
    client.channels.cache.get("916642101989617684").send("!stats");
  }, (1000 * 60) * 240);


const fs = require('fs');
const { Client, Collection, Intents } = require('discord.js');
const { token } = require('./config.json');

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

client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.data.name, command);
}

const 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));
    } else {
        client.on(event.name, (...args) => event.execute(...args));
    }
}

client.once('ready', () => {
    console.log('Ready!');
});

setInterval(() => {
    client.channels.cache.get("916642101989617684").send("!stats");
  }, (1000 * 60) * 240);
   
  

client.on('interactionCreate', async interaction => {
    if (!interaction.isCommand()) return;

    const command = client.commands.get(interaction.commandName);

    if (!command) return;

    try {
        await command.execute(interaction);
    } catch (error) {
        console.error(error);
        return interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
    }
});

client.login(token);

【问题讨论】:

    标签: javascript node.js discord.js command bots


    【解决方案1】:

    只需将它放在一个interactionCreate 事件中,但要确保它是一次,这样它就不会不断地产生更多的间隔

    client.once("interactionCreate", async (interaction) => {
      if (interaction.commandName === "register") { //or whatever name the command should be
        //the code you want to start here
    })
    

    【讨论】:

      猜你喜欢
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多