Unexpected string in JSON

I'm trying to create discord bot that can show an output by slash command but the code couldn't run. App used: Replit. Here's the code: index.js
const fs = require("node:fs");
const path = require("node:path");
const { Client, Collection, Events, GatewayIntentBits } = require("discord.js");
const { token } = require("./config.json");

const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.commands = new Collection();

const commandsPath = path.join(__dirname, "Commands");
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith(".js"));

for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ("data" in command && "execute" in command) {
client.commands.set(command.data.name, command);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}

client.on(Events.InteractionCreate, interaction => {
console.log(interaction);
});

client.on(Events.InteractionCreate, interaction => {
if (!interaction.isChatInputCommand()) return;
console.log(interaction);
});

client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;

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

if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}

try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: "There's an error while executing this command!", ephemeral: true });
} else {
await interaction.reply({ content: "There's an error while executing this command!", ephemeral: true });
}
}
});
const fs = require("node:fs");
const path = require("node:path");
const { Client, Collection, Events, GatewayIntentBits } = require("discord.js");
const { token } = require("./config.json");

const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.commands = new Collection();

const commandsPath = path.join(__dirname, "Commands");
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith(".js"));

for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ("data" in command && "execute" in command) {
client.commands.set(command.data.name, command);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}

client.on(Events.InteractionCreate, interaction => {
console.log(interaction);
});

client.on(Events.InteractionCreate, interaction => {
if (!interaction.isChatInputCommand()) return;
console.log(interaction);
});

client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;

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

if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}

try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: "There's an error while executing this command!", ephemeral: true });
} else {
await interaction.reply({ content: "There's an error while executing this command!", ephemeral: true });
}
}
});
15 Replies
d.js toolkit
d.js toolkit2y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
R
ROP2y ago
deploy-commands.js
const { REST, Routes } = require("discord.js");
const { clientId, guildId, token } = require("./config.json");
const fs = require("node:fs");
const path = require("node:path");

const commands = [];
const foldersPath = path.join(__dirname, "Commands");
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith(".js"));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ("data" in command && "execute" in command) {
commands.push(command.data.toJSON());
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}

const rest = new REST().setToken(token);

(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);

const data = await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands },
);

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
}
})();
const { REST, Routes } = require("discord.js");
const { clientId, guildId, token } = require("./config.json");
const fs = require("node:fs");
const path = require("node:path");

const commands = [];
const foldersPath = path.join(__dirname, "Commands");
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith(".js"));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ("data" in command && "execute" in command) {
commands.push(command.data.toJSON());
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}

const rest = new REST().setToken(token);

(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);

const data = await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands },
);

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
}
})();
Squid
Squid2y ago
You say that it "couldn't run" but that's not very specific What's happening in your terminal?
R
ROP2y ago
hold on config.json
{
"token": "token"
"clientId": "cID",
"guildId": "gID"
}
{
"token": "token"
"clientId": "cID",
"guildId": "gID"
}
R
ROP2y ago
Error message
Squid
Squid2y ago
So the error comes 100% from your config.json not being in valid JSON format From the redacted version you sent here, you're missing a comma after the first key-value pair
R
ROP2y ago
Fixed brb There's no error message rn, but the run button turn back to run once I clicked it (app used: replit)
Squid
Squid2y ago
Are you ever calling client.login(token)? It's not in the code that you sent
R
ROP2y ago
wait, lemme check Should it be placed in index.js?
Squid
Squid2y ago
Yes, that's what the guide says
R
ROP2y ago
It could run properly now, hold on I want to create a slash command hi.js
const { SlashCommandBuilder } = require("@discordjs/builders");

module.exports = {
data: new SlashCommandBuilder()
.setName("hi")
.setDescription("Replies with Hello!"),
async execute(interaction) {
await interaction.reply("Hello!");
},
};

exports.name = "hi"
const { SlashCommandBuilder } = require("@discordjs/builders");

module.exports = {
data: new SlashCommandBuilder()
.setName("hi")
.setDescription("Replies with Hello!"),
async execute(interaction) {
await interaction.reply("Hello!");
},
};

exports.name = "hi"
R
ROP2y ago
this error has been fixed
Squid
Squid2y ago
You should know basic javascript before developing a bot, especially how to diagnose ones that tell you the exact problem
R
ROP2y ago
I've added
const { SlashCommandBuilder } = require("@discordjs/builders");
const { SlashCommandBuilder } = require("@discordjs/builders");
Ok There's no error for now, but I can't access the slash command this? I've created that deploy-commands.js, or there's some missing code? If I press Run, all of the code should be run at the same time. App used: Replit So I need to click the run button while in the deploy-commands.js? I've run the deploy file, it works but inside the console tab it says "No command matching hi was found."
R
ROP2y ago

Did you find this page helpful?