task command work
This commit is contained in:
@@ -1,10 +1,32 @@
|
|||||||
const { SlashCommandBuilder } = require('discord.js');
|
const { SlashCommandBuilder } = require('discord.js');
|
||||||
|
const torn = require('../../torn.js');
|
||||||
|
const config = require('../../config.json');
|
||||||
|
const state = require('../../state.json');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
.setName('task')
|
.setName('task')
|
||||||
.setDescription('Execute a task.'),
|
.setDescription('Execute a task.')
|
||||||
|
.addStringOption(option =>
|
||||||
|
option.setName('name')
|
||||||
|
.setDescription('The name of the task to execute.')
|
||||||
|
.setRequired(true)),
|
||||||
async execute(interaction) {
|
async execute(interaction) {
|
||||||
await interaction.reply('todo');
|
const taskName = interaction.options.getString('name');
|
||||||
|
const task = interaction.client.tasks[taskName];
|
||||||
|
|
||||||
|
if (!task) {
|
||||||
|
await interaction.reply({ content: `Task "${taskName}" not found.`, ephemeral: true });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await interaction.reply({ content: `Executing task "${taskName}"...`, ephemeral: true });
|
||||||
|
await task(interaction.client, torn, config, state);
|
||||||
|
await interaction.followUp({ content: `Task "${taskName}" executed successfully.`, ephemeral: true });
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
await interaction.followUp({ content: `There was an error while executing task "${taskName}"!`, ephemeral: true });
|
||||||
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -1,10 +1,22 @@
|
|||||||
const { SlashCommandBuilder } = require('discord.js');
|
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
.setName('tasks')
|
.setName('tasks')
|
||||||
.setDescription('List tasks'),
|
.setDescription('Lists all available tasks.'),
|
||||||
async execute(interaction) {
|
async execute(interaction) {
|
||||||
await interaction.reply('todo');
|
const taskNames = Object.keys(interaction.client.tasks);
|
||||||
|
|
||||||
|
if (taskNames.length === 0) {
|
||||||
|
await interaction.reply({ content: 'No tasks found.', ephemeral: true });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const embed = new EmbedBuilder()
|
||||||
|
.setColor(0xBB99FF)
|
||||||
|
.setTitle('Available Tasks')
|
||||||
|
.setDescription(taskNames.map(name => `- ${name}`).join('\n'));
|
||||||
|
|
||||||
|
await interaction.reply({ embeds: [embed], ephemeral: true });
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
5
index.js
5
index.js
@@ -32,15 +32,14 @@ client.once(Events.ClientReady, readyClient => {
|
|||||||
});
|
});
|
||||||
client.login(config.token);
|
client.login(config.token);
|
||||||
client.commands = new Collection();
|
client.commands = new Collection();
|
||||||
|
client.tasks = {};
|
||||||
|
|
||||||
|
|
||||||
let task = {};
|
|
||||||
fs.readdir('./tasks/', (err, files) => {
|
fs.readdir('./tasks/', (err, files) => {
|
||||||
if (err) return console.log(err);
|
if (err) return console.log(err);
|
||||||
files.forEach(file => {
|
files.forEach(file => {
|
||||||
const taskFile = require(`./tasks/${file}`);
|
const taskFile = require(`./tasks/${file}`);
|
||||||
const taskName = file.split('.')[0];
|
const taskName = file.split('.')[0];
|
||||||
task[taskName] = taskFile;
|
client.tasks[taskName] = taskFile;
|
||||||
if (taskFile.schedule) {
|
if (taskFile.schedule) {
|
||||||
console.debug(`Tasks: Scheduling "${taskName}" for ${taskFile.schedule}`);
|
console.debug(`Tasks: Scheduling "${taskName}" for ${taskFile.schedule}`);
|
||||||
cron.schedule(taskFile.schedule, () => { taskFile(client, torn, config, state); });
|
cron.schedule(taskFile.schedule, () => { taskFile(client, torn, config, state); });
|
||||||
|
|||||||
Reference in New Issue
Block a user