I am trying to code in / commands into my discord bot. But I keep on getting this error:
Cannot read property 'commands' of undefined
Below I have attached my main.js file, as well as the part that just keeps giving me the error:
Part that gives me the error
const getApp = (guildID) => {
const app = client.api.applications(client.user.id)
if (guildID) {
app.guilds(guildID)
}
}
client.once('ready', async() => {
client.user.setActivity('FALLBACK BOT, USE THE MAIN CHECKPOINT BOT INSTEAD. THIS IS FOR DEVELOPMENT PURPOSES')
const commands = await getApp(guildID).commands.get()
console.log(commands)
await getApp(guildID).commands.post({
data: {
name: 'ping',
description: 'Shows your current ping.',
},
})
});
Here is the full script
// Just grabbing some librarys
const Discord = require('discord.js');
require('dotenv').config();
const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION" ]});
const mongoose = require('mongoose');
// Defining the guildID
const guildID = (process.env.guildID);
// Filtering the command folder so it only includes .js files
const { join } = require('path');
const fs = require('fs');
require('./dashboard/server');
client.commands = new Discord.Collection();
client.events = new Discord.Collection();
const getApp = (guildID) => {
const app = client.api.applications(client.user.id)
if (guildID) {
app.guilds(guildID)
}
}
client.once('ready', async() => {
client.user.setActivity('FALLBACK BOT, USE THE MAIN CHECKPOINT BOT INSTEAD. THIS IS FOR DEVELOPMENT PURPOSES')
const commands = await getApp(guildID).commands.get()
console.log(commands)
await getApp(guildID).commands.post({
data: {
name: 'ping',
description: 'Shows your current ping.',
},
})
});
['command_handler', 'event_handler'].forEach(handler =>{
require(`./handlers/${handler}`)(client, Discord);
})
mongoose.connect(process.env.MONGODB_SRV, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
}).then(()=> {
console.log('Connected to Database')
}).catch((err) =>{
console.log(err);
})
// Logging into the bot (THIS INCLUDES THE TOKEN SO DONT INCLUDE IT WHEN SENDING MESSAGES)
client.login(process.env.DISCORD_TOKEN);
Note: I am quite new to JS so. keep that in mind
Your getApp() function doesn't return anything so you are trying to read commands property from undefined value.
You have to return you app.guilds(guildId) so you can read property from it.
const getApp = (guildID) => {
const app = client.api.applications(client.user.id)
if (guildID) {
app.guilds(guildID)
}
return app;
}
client.once('ready', async() => {
client.user.setActivity('FALLBACK BOT, USE THE MAIN CHECKPOINT BOT INSTEAD. THIS IS FOR DEVELOPMENT PURPOSES')
const app = getApp(guildID);
if (app === null) {
console.error('error');
return;
}
const commands = await app.commands.get()
console.log(commands)
await app.commands.post({
data: {
name: 'ping',
description: 'Shows your current ping.',
},
})
});
Related
it might be a stupid question but since I'm a beginner I sadly don't understand most of how javascript works yet.
I'm trying to create a small Discord bot for a project that I'm working on and I would like to make it so you can select an option from a dropdown menu and whatever you select it sends a different embedded message in the channel. But whatever I do it doesn't send the embedded message. I've looked up multiple tutorials, but it seems like I'm missing something. Maybe someone can direct me in the right direction.
It is sending the dropdown menu, but sending error "InteractionAlreadyReplied". And whenever I try to select an option it won't send the embedded messages.
Here is my code so you can see the approach I'm using. I've tried using "MessageActionRow" and "MessageSelectMenu", but it seems like they don't work anymore. After inserting "ActionRowBuilder" and "StringSelectMenuBuilder" I got the menu working, but as said I don't get the embedded messages as an option. (didn't include the .env file since its only the token in there) I'm just not sure how I can make it so it connects the values from the dropdown menu to the right embeds.
CommandFile:
const { Discord, MessageActionRow, MessageSelectMenu,
SlashCommandBuilder, EmbedBuilder, roleMention, ButtonBuilder,
StringSelectMenuBuilder, StringSelectMenuOptionBuilder, ActionRowBuilder } = require('discord.js');
const { MessageEmbed } = require("discord.js")
module.exports = {
data: new SlashCommandBuilder()
.setName('dropdown')
.setDescription('Returns a select dropdown!'),
async execute(interaction, client, message, args) {
const row = new ActionRowBuilder()
.addComponents(
new StringSelectMenuBuilder()
.setCustomId("select")
.setPlaceholder("Wähle etwas aus:")
.addOptions([
{
label: "Auswahl 1",
description: "Beschreibung Auswahl 1",
value: "first"
},
{
label: "Auswahl 2",
description: "Beschreibung Auswahl 2",
value: "second"
},
{
label: "Auswahl 3",
description: "Beschreibung Auswahl 3",
value: "third"
},
])
)
let embed = new EmbedBuilder()
.setTitle("Embed Startseite")
.setDescription("Ab hier kann man selecten, was man möchte")
.setColor(0x18e1ee)
let sendmsg = await interaction.reply({ content:
"ㅤ", ephemeral:true, embeds: [embed], components: [row] })
let embed1 = new EmbedBuilder()
.setTitle("Embed 1")
.setDescription("Embed 1 Beschreibung")
.setColor(0x18e1ee)
let embed2 = new EmbedBuilder()
.setTitle("Embed 2")
.setDescription("Embed 2 Beschreibung")
.setColor(0x18e1ee)
let embed3 = new EmbedBuilder()
.setTitle("Embed 3")
.setDescription("Embed 3 Beschreibung")
.setColor(0x18e1ee)
const collector = message.createMessageComponentCollector({
componentType: "StringSelectMenuBuilder"
})
collector.on("collect", async (collected) => {
const value = collected.values[0]
if(value === "first") {
collected.reply({ embeds: [embed1], ephemeral:true })
}
if(value === "second") {
collected.reply({ embeds: [embed2], ephemeral:true })
}
if(value === "third") {
collected.reply({ embeds: [embed3], ephemeral:true })
}
})
}
}
bot.js:
require("dotenv").config();
const { token } = process.env;
const { Client, Collection, GatewayIntentBits, ChannelSelectMenuInteraction } = require("discord.js");
const fs = require("fs");
const client = new Client({ intents: GatewayIntentBits.Guilds });
client.commands = new Collection();
client.buttons = new Collection();
client.selectMenus = new Collection();
client.commandArray = [];
const functionFolders = fs.readdirSync(`./src/functions`);
for (const folder of functionFolders) {
const functionFiles = fs
.readdirSync(`./src/functions/${folder}`)
.filter((file) => file.endsWith(".js"));
for (const file of functionFiles)
require(`./functions/${folder}/${file}`)(client);
}
client.handleEvents();
client.handleCommands();
client.handleComponents();
client.login(token);
HandleComponents.js :
const { readdirSync } = require('fs');
module.exports = (client) => {
client.handleComponents = async () => {
const componentFolders = readdirSync(`./src/components`);
for (const folder of componentFolders) {
const componentFiles = readdirSync(`./src/components/${folder}`).filter(
(file) => file.endsWith('.js')
);
const { buttons, selectMenus } = client;
switch (folder) {
case "buttons":
for (const file of componentFiles) {
const button = require(`../../components/${folder}/${file}`);
buttons.set(button.data.name, button);
}
break;
case "selectMenus":
for (const file of componentFiles) {
const menu = require(`../../components/${folder}/${file}`);
selectMenus.set(menu.data.name, menu);
}
break;
default:
break;
}
}
};
};
HandleCommands.js :
const { REST } = require('#discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const fs = require("fs");
module.exports = (client) => {
client.handleCommands = async () => {
const commandFolders = fs.readdirSync("./src/commands");
for (const folder of commandFolders) {
const commandFiles = fs
.readdirSync(`./src/commands/${folder}`)
.filter(file => file.endsWith(".js"));
const { commands, commandArray } = client;
for (const file of commandFiles) {
const command = require(`../../commands/${folder}/${file}`);
commands.set(command.data.name, command);
commandArray.push(command.data.toJSON());
}
}
const clientId = "IDbutCensored";
const guildId = "IDbutCensored";
const rest = new REST({ version: '9' }).setToken(process.env.token);
try {
console.log("Started refreshing application (/) commands.");
await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
body: client.commandArray,
});
console.log("Successfully reloaded application (/) commands.");
} catch (error) {
console.error(error);
}
};
};
handleEvents.js :
const fs = require("fs");
module.exports = (client) => {
client.handleEvents = async () => {
const eventFolders = fs.readdirSync(`./src/events`);
for (const folder of eventFolders) {
const eventFiles = fs
.readdirSync(`./src/events/${folder}`)
.filter((file) => file.endsWith(".js"));
switch (folder) {
case "client":
for (const file of eventFiles) {
const event = require(`../../events/${folder}/${file}`);
if (event.once) client.once(event.name, (...args) => event.execute(...args, client));
else client.on(event.name, (...args) => event.execute(...args, client));
}
break;
default:
break;
}
}
}
}
The error is pretty self explanatory. The error InteractionAlreadyReplied means you already replied and you're trying to reply again.
You can solve this by doing the following;
• Use .followUp() to send a new message
• If you deferred reply it's better to use .editReply()
• Information about responding to slash commands / buttons / select menus
From the Discord docs
I've been getting this error recently and I don't really know how I'm supposed to fix this;
TypeError: Cannot read properties of undefined (reading 'get')
This is the line of code it refers to:
const command1 = interaction.client.commands.get(interaction.commandName);
It was working earlier, but I tried adding the command refresh thingy and it's not working anymore.
Here's my full code:
const { REST, Routes } = require('discord.js');
const fs = require('node:fs');
const { clientId, guildId, token } = require('./config.json');
const path = require('node:path');
const { Client, Collection, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const commands = [];
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
commands.push(command.data.toJSON());
}
client.once('ready', () => {
console.log('Ready!');
});
const rest = new REST({ version: '10' }).setToken(token);
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
const data = await rest.put(
Routes.applicationCommands(clientId),
{ body: commands },
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
}
})();
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;
const command1 = interaction.client.commands.get(interaction.commandName);
if (!command1) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command1.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
client.login(token);
hey try adding this code
const channel = await client.channels.fetch('<channel-id>')
Im using discord js to make a multi-purpose discord bot for my server, but its giving me this error:
ValidationError: Expected a string primitive
It was working fine yesterday but i forgot to save something and i dont know what.
const fs = require('node:fs');
const path = require('node:path');
const {
Client,
GatewayIntentBits,
Partials,
Collection,
} = require("discord.js");
const { Player } = require('discord-player');
const { Routes } = require('discord-api-types/v10');
const { token, prefix, guildId, clientId } = require('./config.json');
const { REST } = require('#discordjs/rest');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildVoiceStates
],
partials: [
Partials.Channel,
Partials.Message,
Partials.User,
Partials.GuildMember,
],
});
client.player = new Player(client, {
ytdlOptions: {
quality: "highestaudio",
highWaterMark: 1 << 25
}
});
module.exports = client;
// command handler
//slash commands
const slashCommands = [];
client.slashCommands = new Collection();
const commandsPath = path.join(__dirname, "commands"); // E:\yt\discord bot\js\intro\commands
const slash_commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('S.js'));
for(const file of slash_commandFiles)
{
const filePath = path.join(commandsPath, file);
const command = require(filePath);
client.slashCommands.set(command.data.name, command);
slashCommands.push(command.toJSON());
}
console.log(slashCommands);
//message commands
client.messageCommands = new Collection();
const message_commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('M.js'));
for (const file of message_commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a new item in the Collection
// With the key as the command name and the value as the exported module
client.messageCommands.set(command.Name, command);
}
//event handler
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
// messageCommand handler
client.on('messageCreate', (message) => {
const args = message.content.slice(prefix.length).split(' ');
const command = args[0];
if (client.messageCommands.get(command)) {
let Command = client.messageCommands.get(command);
Command.execute(message);
}
});
client.on('ready', () => {
const rest = new REST({ version: '9' }).setToken(token);
rest.put(Routes.applicationGuildCommands(clientId, guildId),
{ body: slashCommands })
.then(() => console.log('Successfully updated commands for guild ' + guildId))
.catch(console.error);
console.log('bot is online!');
client.user.setStatus('idle');
});
client.login(token);
im sure there's nothing wrong with any of the command files because it was working fine yesterday.
there's 100% an error in this line slashCommands.push(command.toJSON()); but I cant seem to fix it. The command.toJSON() console logs just fine but gives an error while trying to push into the list
Ran into the same issue, turns out options (i.e. addUserOption) require a description. Point is it's really confusing as this error shows up when doing command.data.toJSON(). If you are dynamically loading the command files as described in the guide and running into this issue, then try manually doing a require to trigger the validation beforehand.
Try using command.data.toJSON() as command is a nested object with a data and an execute key.
I've fixed it, there was a url in the json which seemed to be causing some issue, I removed the file with the url and its working now
Anything that I change to make this code work just changes said undefined thing.
I have tried to fix using multiple different answers, but no luck.
// Require the necessary discord.js classes
const { Client, GatewayIntentBits, message } = require('discord.js');
const { token } = require('./config.json');
const guildId = '1009548907015057460';
const channelId = '1009548907702915245';
// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
// When the client is ready, run this code (only once)
client.once('ready', () => {
console.log('Ready!');
});
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;
const { commandName } = interaction;
if (commandName === 'invite') {
const guild = await client.guilds.fetch(guildId);
console.log(guild);
const channel = await guild.channels.cache.get(channelId);
let invite = await guild.channel.createInvite(
{
maxAge: 300000,
maxUses: 1,
},
'${message.author.tag} requested a invite',
).catch(console.log);
await interaction.deferReply({ ephemeral: true });
await interaction.editReply(invite ? 'Join: ${invite}' : 'Error');
}
}
Anything can help.
You don't need to fetch the guild, you can get the channel from the client
There's no need for await when you're getting information from cache
guild.channel.createInvite should be channel.createInvite
You're using single quotes for variables, they'll end up being strings
There's no need for invite to use let when the value doesn't change
Defer the reply at the beginning rather than right before replying
message.author.tag would return undefined, message doesn't exist. Use interaction.user.tag instead
Add await to channel.createInvite as it returns a promise
Replace ${invite} with ${invite.url}
Also it's best to store the token in a .env file rather than a JSON one.
// Require the necessary discord.js classes
const { Client, GatewayIntentBits, message } = require('discord.js');
const { token } = require('./config.json');
const channelId = '1009548907702915245';
// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
// When the client is ready, run this code (only once)
client.once('ready', () => {
console.log('Ready!');
});
client.on('interactionCreate', async (interaction) => {
if (!interaction.isChatInputCommand()) return;
await interaction.deferReply({ ephemeral: true });
const { commandName } = interaction;
if (commandName === 'invite') {
const channel = client.channels.cache.get(channelId);
const invite = await channel.createInvite({
maxAge: 604800, // 1 week
maxUses: 1,
reason: `${interaction.user.tag} requested an invite`
}).catch(console.log);
interaction.editReply(invite ? `Join: ${invite.url}` : 'Error');
}
}
Resolving the promise works. This is the code after:
// Require the necessary discord.js classes
const { Client, GatewayIntentBits, message } = require('discord.js');
const { token } = require('./config.json');
const channelId = '1009548907702915245';
// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
// When the client is ready, run this code (only once)
client.once('ready', () => {
console.log('Ready!');
});
client.on('interactionCreate', async (interaction) => {
if (!interaction.isChatInputCommand()) return;
await interaction.deferReply({ ephemeral: true });
const { commandName } = interaction;
if (commandName === 'invite') {
const channel = client.channels.cache.get(channelId);
const invite = channel.createInvite({
maxAge: 604800, // 1 week
maxUses: 1,
reason: `${interaction.user.tag} requested an invite`
})
.catch(console.log);
const promise = Promise.resolve(invite);
promise.then((value) => {
interaction.editReply(`Join: ${value}`);
});
}
}
My reaction role file:
module.exports = {
name: "rr",
desc: "make a reaction role --> <",
async run(message, client) {
const jsEmoji = "🍏";
const pythonEmoji = "🍍";
let embedMessage = await message.channel.send("react for a role");
embedMessage.react(jsEmoji);
embedMessage.react(pythonEmoji);
client.on("messageReactionAdd", (user, reaction) => {
console.log("he reacted");
});
},
};
my bot file:
const intents = new Discord.Intents(32767);
I initialized my client here
const client = new Discord.Client({ intents: intents });
const fs = require("fs");
const commandFolders = fs.readdirSync("./Commands");
client.commands = new Discord.Collection();
commandFolders.forEach((folder) => {
const commandFiles = fs
.readdirSync(`./Commands/${folder}`)
.filter((file) => file.endsWith(".js"));
commandFiles.forEach((file) => {
const command = require(`./Commands/${folder}/${file}`);
client.commands.set(command.name, command);
});
});
client.on("messageCreate", (message) => {
if (message.author.bot || !message.content.startsWith(config.prefix)) return;
const [command, ...args] = message.content
.substring(config.prefix.length)
.split(/\s+/);
I tried getting access to the client here and passing it into the run function but that didnt work.
client.commands.forEach((cmd) =>
command === cmd.name
? client.commands.get(cmd.name).run(message, args, client)
: null
);
});
client.login(config.token);
My Error:
client.on("messageReactionAdd", (user, reaction) => {
^
TypeError: client.on is not a function
Is there another way to get access to my client?
your arguments are wrong on command file.
You should use async run(client, message, args)
Here's a full code for you:
module.exports = {
name: "rr",
desc: "make a reaction role --> <",
async run(client, message, args) {
const jsEmoji = "🍏";
const pythonEmoji = "🍍";
let embedMessage = await message.channel.send("react for a role");
embedMessage.react(jsEmoji);
embedMessage.react(pythonEmoji);
client.on("messageReactionAdd", (user, reaction) => {
console.log("he reacted");
});
},
};