#Can't figure out how to make code work as slash command
38 messages · Page 1 of 1 (latest)
• 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.
const { SlashCommandBuilder } = require('discord.js');
const { Client, Intents, Message, ActivityType} = require('discord.js');
const server_ip = "IP"; //replace with real ip for the server
const server_port = 25565; // replace with the real port also thanks dashcruft for testing the code
module.exports = {
data: new SlashCommandBuilder()
.setName('mcstatus')
.setDescription('Shows status of the minecraft server'),
async execute(interaction) {Client.on('messageCreate', (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
if(message.content.startsWith(prefix)){
util.status(server_ip, server_port, options,)
.then((result) => {
client.user.setPresence({ activities: [{ name: ${server_ip}, type: ActivityType.Watching }], status: 'Online' });
const string1 = JSON.stringify(result);// turn the object into a string
const string = JSON.parse(string1);// make the string parsable
const embed = new EmbedBuilder()
.setColor("#00008B")
.setTitle("Status Server")
.setDescription(`Mai jos sunt informatii in legatura cu serverul de minecraft \n **IP Server:** ${server_ip} \n **Port Server:** ${server_port}`)
.addFields(
{name:"Versiune", value: `${string.version.name}`},
//{name:"Server Protocol Version", value:`${string.version.protocol}`},
{name:"Players Online", value:`${string.players.online}`},
{name:"Max Players", value:`${string.players.max}`},
{name:"Message of the day", value:`${string.motd.clean}`},
{name:"Latency", value:`${string.roundTripLatency}`},
)
.setTimestamp()
message.channel.send({embeds: [embed]})// send the embed
})
.catch((error) => {
console.log(error);// if the server was unable to be pinged or something else happened
client.user.setPresence({ activities: [{ name: `Server is offline`, type: ActivityType.Watching }], status: 'dnd' });
const embed = new EmbedBuilder()
.setColor("#FF0000")
.setTitle("Status necunoscut")
.setDescription(`Serverul este offline, se restarteaza sau are probleme de conexiune `)
.setTimestamp()
message.channel.send({embeds: [embed]})// send the embed
})}})
.catch((error) => {
console.log(error);// if the server was unable to be pinged or something else happened
client.user.setPresence({ activities: [{ name: Server is offline, type: ActivityType.Watching }], status: 'dnd' });
const embed = new EmbedBuilder()
.setColor("#FF0000")
.setTitle("Status necunoscut")
.setDescription(`Serverul este offline, se restarteaza sau are probleme de conexiune `)
.setTimestamp()
message.channel.send({embeds: [embed]})// send the embed
})
}}
Codeblocks:
```js
const Discord = require("discord.js");
// further code
```
becomes
const Discord = require("discord.js");
// further code
Inline Code:
`console.log('inline!');` becomes console.log('inline!');
Why do you register a messageCreate handler in your interaction handling function 
because i dont know how to make it work?
You probably only want to keep the code from util.status inside the execute function (delete the client.on in there)
It wants me to replace the . with a ,
Paste how your code looks now in there👆
Replace the message.channel.send with interaction.reply and import util and you might be ready to go
Or at least ready to debug
No. Where did you copy that code from?
util must‘ve been defined there somehow. It isn’t defined yet in your code
I put it under commands
when i use !mcstatus it works
with / it doesnt
2 sec
And this is the code i tried to modify
You need that line 2 in your current file. The const util =
you missing brackets after your execute and you’ve got a random comma after options for util.status
That comma can stay, no harm in trailing commas afaik.
Creating Your Bot: Individual command files