bot just crashes

const { Client, Interaction, ApplicationCommandOptionType, PermissionFlagsBits } = require('discord.js');
const fs = require('fs');
const path = require('path');

// Define the path to the data file three directories up from the current directory
const dataFilePath = path.join(__dirname, '../../../data.json');

module.exports = {
name: 'add',
description: 'Set the stock',
options: [
{
name: 'account-type',
description: 'The type of account',
type: ApplicationCommandOptionType.Channel,
required: true,
},
{
name: 'state',
description: 'The state of the account',
type: ApplicationCommandOptionType.String,
required: true,
choices: [
{
name: 'resting',
value: 'resting',
},
{
name: 'stock',
value: 'stock',
},
],
},
{
name: 'amount',
description: 'The amount of accounts',
type: ApplicationCommandOptionType.Number,
required: true,
},
],
callback: async (client, interaction) => {
console.log('Callback function triggered');
try {
// Read the JSON data from the file
console.log(`Reading data from file: ${dataFilePath}`);
const data = JSON.parse(await fs.promises.readFile(dataFilePath, 'utf8'));
console.log('Data read successfully:', data);

// Extract options
const channelId = interaction.options.getChannel('account-type').id;
const state = interaction.options.getString('state');
const amount = interaction.options.getNumber('amount');

// Log the extracted options
console.log(`Extracted options - Channel ID: ${channelId}, State: ${state}, Amount: ${amount}`);

// Find the channel in the data
const channelData = data.channels.find(channel => channel.channelID === channelId);

if (!channelData) {
console.log(`Channel with ID ${channelId} not found.`);
return interaction.reply({ content: `Channel with ID ${channelId} not found.`, ephemeral: true });
}

// Update the state with the new amount
if (channelData.categories[state] !== undefined) {
console.log(`Current amount for ${state} before update: ${channelData.categories[state].amount}`);
oldamount = channelData.categories[state].amount
channelData.categories[state].amount += amount;
console.log(`Updated amount for ${state}: ${amount}`);

// Save the updated data back to the JSON file
await fs.promises.writeFile(dataFilePath, JSON.stringify(data, null, 2));
console.log('Data successfully written to file.');

return interaction.reply({ content: `Successfully updated ${state} amount to ${amount} for channel ID ${channelId}.`, ephemeral: true });
} else {
console.log(`State ${state} is not valid.`);
return interaction.reply({ content: `State ${state} is not valid.`, ephemeral: true });
}
} catch (err) {
console.error('Error processing the request:', err);
return interaction.reply({ content: 'An error occurred while updating the data.', ephemeral: true });
}
},
};
const { Client, Interaction, ApplicationCommandOptionType, PermissionFlagsBits } = require('discord.js');
const fs = require('fs');
const path = require('path');

// Define the path to the data file three directories up from the current directory
const dataFilePath = path.join(__dirname, '../../../data.json');

module.exports = {
name: 'add',
description: 'Set the stock',
options: [
{
name: 'account-type',
description: 'The type of account',
type: ApplicationCommandOptionType.Channel,
required: true,
},
{
name: 'state',
description: 'The state of the account',
type: ApplicationCommandOptionType.String,
required: true,
choices: [
{
name: 'resting',
value: 'resting',
},
{
name: 'stock',
value: 'stock',
},
],
},
{
name: 'amount',
description: 'The amount of accounts',
type: ApplicationCommandOptionType.Number,
required: true,
},
],
callback: async (client, interaction) => {
console.log('Callback function triggered');
try {
// Read the JSON data from the file
console.log(`Reading data from file: ${dataFilePath}`);
const data = JSON.parse(await fs.promises.readFile(dataFilePath, 'utf8'));
console.log('Data read successfully:', data);

// Extract options
const channelId = interaction.options.getChannel('account-type').id;
const state = interaction.options.getString('state');
const amount = interaction.options.getNumber('amount');

// Log the extracted options
console.log(`Extracted options - Channel ID: ${channelId}, State: ${state}, Amount: ${amount}`);

// Find the channel in the data
const channelData = data.channels.find(channel => channel.channelID === channelId);

if (!channelData) {
console.log(`Channel with ID ${channelId} not found.`);
return interaction.reply({ content: `Channel with ID ${channelId} not found.`, ephemeral: true });
}

// Update the state with the new amount
if (channelData.categories[state] !== undefined) {
console.log(`Current amount for ${state} before update: ${channelData.categories[state].amount}`);
oldamount = channelData.categories[state].amount
channelData.categories[state].amount += amount;
console.log(`Updated amount for ${state}: ${amount}`);

// Save the updated data back to the JSON file
await fs.promises.writeFile(dataFilePath, JSON.stringify(data, null, 2));
console.log('Data successfully written to file.');

return interaction.reply({ content: `Successfully updated ${state} amount to ${amount} for channel ID ${channelId}.`, ephemeral: true });
} else {
console.log(`State ${state} is not valid.`);
return interaction.reply({ content: `State ${state} is not valid.`, ephemeral: true });
}
} catch (err) {
console.error('Error processing the request:', err);
return interaction.reply({ content: 'An error occurred while updating the data.', ephemeral: true });
}
},
};
with this code my bot just crashes
2 Replies
d.js toolkit
d.js toolkit3mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Svitkona
Svitkona3mo ago
with what error?
Want results from more Discord servers?
Add your server