Reputation: 21
So, I've been working on this discord bot that takes '/' commands in order to display details about the user's (for the game osu!) most recent play, their top play, etc. However, I've been struggling with trying to take in arguments using the '/' commands to try and add their username onto the bot's database. I understand how to approach adding their username onto a database but I don't understand how to get an argument they send in addition to the /set command. For example, I want to retrive the String 'David' if they give the command '/set David'
Here's my index.js which is from the discord.js v13.0 documentation:
const fs = require('node:fs');
const { Client, Collection, Intents, User } = require('discord.js');
const { token } = require('./config.json');
const fetch = require('node-fetch');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.data.name, command);
}
client.once('ready', () => {
console.log('Ready!');
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
client.login(token);
And here's the skeleton code for the command file where I want to have the discord user give their osu! username as an argument:
const { SlashCommandBuilder } = require('@discordjs/builders');
const { v1, auth } = require('osu-api-extended');
const { Client, Intents, MessageEmbed } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('set')
.setDescription('Allows the user to add their osu! username onto the bot\'s system'),
async execute(interaction) {
},
};
Upvotes: 0
Views: 2673
Reputation: 4520
Simple. Take a look at the docs.
const arg = interaction.options.getString("insert argument name here");
That is the basic format for getting the value of a String
argument. If you have a different type of argument such as integer or boolean, you would use getInteger()
or getBoolean()
respectively instead of getString()
. The methods for each different type of argument are fully documented here.
As a more specific example, let's say you have a command like this: /set <username>
, where the command is named "set" and the argument is named "username." This is how you would get the username value in that instance:
const username = interaction.options.getString("username");
For questions regarding how to do basic discord.js actions like this one, I would recommend taking a look at the official djs documentation before asking a question here. The docs will always have the answer to such questions, and usually those answers aren't too difficult to find.
Upvotes: 1