Setting different commands for different types of servers.

Hello there, so i have a few server types. the user must /register before being able to see all the commands. Seperate kinds of servers should have seperate set of commands, and they should not be able to see each other's commands. for example (if you didnt understand) : - A server adds my bot. - They should initially only be able to see the /register command, and should not be able to see any other commands until they register. - They can register the type of server. for example: government server / mafia server - Government can only see gov related commands - mafia can see mafia related commands etc.. how do i make it update commands conditionally?
15 Replies
d.js toolkit
d.js toolkit10mo 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! - Marked as resolved by OP
mallusrgreat
mallusrgreat10mo ago
use guild-specific commands
d.js docs
d.js docs10mo ago
:guide: Creating Your Bot: Registering slash commands read more
Somebody
SomebodyOP10mo ago
const { REST, Routes } = require('discord.js');
clientId = '1172582289805430794';
guildId = '1172581606687518750';
const fs = require('node:fs');
const path = require('node:path');
token = process.env.SSRP_BOT_TOKEN;
const rest = new REST().setToken(token);

rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: [] })
.then(() => console.log('Successfully deleted all guild commands.'))
.catch(console.error);

rest.put(Routes.applicationCommands(clientId), { body: [] })
.then(() => console.log('Successfully deleted all application commands.'))
.catch(console.error);


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

for (const folder of commandFolders) {
// Grab all the command files from the commands directory you created earlier
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.`);
}
}
}
(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');
clientId = '1172582289805430794';
guildId = '1172581606687518750';
const fs = require('node:fs');
const path = require('node:path');
token = process.env.SSRP_BOT_TOKEN;
const rest = new REST().setToken(token);

rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: [] })
.then(() => console.log('Successfully deleted all guild commands.'))
.catch(console.error);

rest.put(Routes.applicationCommands(clientId), { body: [] })
.then(() => console.log('Successfully deleted all application commands.'))
.catch(console.error);


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

for (const folder of commandFolders) {
// Grab all the command files from the commands directory you created earlier
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.`);
}
}
}
(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);
}
})();
this is how im doing it, can you elaborate further? how can i do it this is for just one guild
mallusrgreat
mallusrgreat10mo ago
you'd probably have to make a custom command handler for commands with separated folders for different types use that code to only register the /register command
Somebody
SomebodyOP10mo ago
im doing it like this
No description
mallusrgreat
mallusrgreat10mo ago
then make a different one, getting all the guilds with their id and registering the commands appropriate to their type
Somebody
SomebodyOP10mo ago
criminal folder has criminal specific stuff (which is a faction type for a game) same with law factions are common cmds
mallusrgreat
mallusrgreat10mo ago
just read my previous messages, and if you don't understand a specific part i'll elaborate further
Somebody
SomebodyOP10mo ago
ok i'll try something and be back
Somebody
SomebodyOP10mo ago
Somebody
SomebodyOP10mo ago
so i did what you said, all works fine except, i wrote this refresh commands module based on faction type however,, sometimes it compltely deletes all the faction commands (im trying to refresh them) whilst sometimes it works perfect i suspect its due to the async nature but im unsure
mallusrgreat
mallusrgreat10mo ago
that's pretty weird
probablyraging
probablyraging10mo ago
It's likely that the for loop isn't finishing before trying to refresh your commands, meaning the commands array is empty You could use Promise.all() with a map instead
Somebody
SomebodyOP10mo ago
that worked, thanks a lot.
Want results from more Discord servers?
Add your server