【发布时间】: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