Trying to receive and log individual responses from two servers.

I am using Rcon to remotely connect to two game servers. I have created a slash command for my Discord bot to send whatever command typed in to both servers. I can get both of them to work, but I cannot get my bot to reply with both individual responses.
client.on('interactionCreate', async (interaction) => {
if (interaction.commandName === 'rcon') {

const conn = new Rcon(process.env.HOST1_IP, process.env.HOST1_PORT, process.env.HOST1_PASS, process.env.HOST2_IP, process.env.HOST2_PORT, process.env.HOST2_PASS);
const conn2 = new Rcon(process.env.HOST2_IP, process.env.HOST2_PORT, process.env.HOST2_PASS);
const command = interaction.options.get('command').value;

console.log("Authenticated");
console.log("Sending command.")

conn.on('auth', function () {
conn.send(`/${command}`);
}).on('response',
(str) => {
console.log('Server 1: ' + str);
}).on('error', (error) => {
console.log('Error: ' + error);
})
conn.connect();

conn2.on('auth', () => {
conn2.send(`/${command}`);
}).on('response',
(str) => {
console.log('Server 2: ' + str);
}).on('error', (error) => {
console.log('Error: ' + error);
})
conn2.connect();
};
},
);
client.on('interactionCreate', async (interaction) => {
if (interaction.commandName === 'rcon') {

const conn = new Rcon(process.env.HOST1_IP, process.env.HOST1_PORT, process.env.HOST1_PASS, process.env.HOST2_IP, process.env.HOST2_PORT, process.env.HOST2_PASS);
const conn2 = new Rcon(process.env.HOST2_IP, process.env.HOST2_PORT, process.env.HOST2_PASS);
const command = interaction.options.get('command').value;

console.log("Authenticated");
console.log("Sending command.")

conn.on('auth', function () {
conn.send(`/${command}`);
}).on('response',
(str) => {
console.log('Server 1: ' + str);
}).on('error', (error) => {
console.log('Error: ' + error);
})
conn.connect();

conn2.on('auth', () => {
conn2.send(`/${command}`);
}).on('response',
(str) => {
console.log('Server 2: ' + str);
}).on('error', (error) => {
console.log('Error: ' + error);
})
conn2.connect();
};
},
);
27 Replies
d.js toolkit
d.js toolkit17mo 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.
Munxo
MunxoOP17mo ago
I would like the bot to either reply or post a message with the content that is logged in the terminal (i.e. Server 1: (playerinfo munxo): No player with the username 'munxo'.
d.js docs
d.js docs17mo ago
Responding to interactions: - #reply immediately respond with a message - #update immediately update the original message (buttons, select menus) - #showModal immediately show a modal (cannot be deferred) - #deferReply/Update respond later (up to 15 minutes) - #followUp post an additional message The initial response has to happen within 3s of receiving the interaction!
Munxo
MunxoOP17mo ago
I have tried using interaction.reply() as well as interaction.followUp(), but no luck. It might have been the placement of that within my code.
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Munxo
MunxoOP17mo ago
I will put them back in and post the error. One sec. Server 2: (playerinfo munxo): No player with the username 'munxo'. /Users/mattmunson/Desktop/Desktop - Matt’s MacBook Pro/Test Bot/node_modules/discord.js/src/structures/interfaces/InteractionResponses.js:184 if (!this.deferred && !this.replied) return Promise.reject(new DiscordjsError(ErrorCodes.InteractionNotReplied)); ^ Error [InteractionNotReplied]: The reply to this interaction has not been sent or deferred. at ChatInputCommandInteraction.followUp (/Users/mattmunson/Desktop/Desktop - Matt’s MacBook Pro/Test Bot/node_modules/discord.js/src/structures/interfaces/InteractionResponses.js:184:64) at Rcon.<anonymous> (/Users/mattmunson/Desktop/Desktop - Matt’s MacBook Pro/Test Bot/index.js:229:23) at Rcon.emit (node:events:513:28) at Rcon._tcpSocketOnData (/Users/mattmunson/Desktop/Desktop - Matt’s MacBook Pro/Test Bot/node_modules/rcon/node-rcon.js:169:14) at Socket.<anonymous> (/Users/mattmunson/Desktop/Desktop - Matt’s MacBook Pro/Test Bot/node_modules/rcon/node-rcon.js:87:54) at Socket.emit (node:events:513:28) at addChunk (node:internal/streams/readable:324:12) at readableAddChunk (node:internal/streams/readable:297:9) at Readable.push (node:internal/streams/readable:234:10) at TCP.onStreamRead (node:internal/stream_base_commons:190:23) { code: 'InteractionNotReplied' } Node.js v18.16.1 [nodemon] app crashed - waiting for file changes before starting...
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Munxo
MunxoOP17mo ago
This is what I have
conn.on('auth', function () {
conn.send(`/${command}`);
}).on('response',
(str) => {
console.log('Server 1: ' + str);
interaction.reply('Server 1: ' + str);
}).on('error', (error) => {
console.log('Error: ' + error);
})
conn.connect();

conn2.on('auth', () => {
conn2.send(`/${command}`);
}).on('response',
(str) => {
console.log('Server 2: ' + str);
interaction.followUp('Server 2: ' + str);
}).on('error', (error) => {
console.log('Error: ' + error);
})
conn2.connect();
conn.on('auth', function () {
conn.send(`/${command}`);
}).on('response',
(str) => {
console.log('Server 1: ' + str);
interaction.reply('Server 1: ' + str);
}).on('error', (error) => {
console.log('Error: ' + error);
})
conn.connect();

conn2.on('auth', () => {
conn2.send(`/${command}`);
}).on('response',
(str) => {
console.log('Server 2: ' + str);
interaction.followUp('Server 2: ' + str);
}).on('error', (error) => {
console.log('Error: ' + error);
})
conn2.connect();
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs17mo ago
mdn Scope The scope is the current context of execution in which values and expressions are "visible" or can be referenced. If a variable or expression is not in the current scope, it will not be available for use. Scopes can also be layered in a hierarchy, so that child scopes have access to parent scopes, but not vice versa.
Munxo
MunxoOP17mo ago
Can you see a way to be able to pull the content spat back from "Server 1" and "Server 2" to create an
interaction.reply(`
**Server 1:** ${...}
**Server 2:** ${...}
`)
interaction.reply(`
**Server 1:** ${...}
**Server 2:** ${...}
`)
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Munxo
MunxoOP17mo ago
Or would that be essentially impossible? I have tried to define the individual str 's of each command So like function server1() & function server2()?
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Munxo
MunxoOP17mo ago
Yeah, so a single reply to the slash command saying something like Server 1: (playerinfo munxo): Name: Munxo / AGID: 123-123-123 / Dinosaur: N/A / Role: Manager / Marks: 4,819 / Growth: N/A Server 2: (playerinfo munxo): No player with the username 'Munxo'. I should still stay within this client.on('interactionCreate', async (interaction) => {} or within this client.on('interactionCreate', async (interaction) => { if (interaction.commandName === 'rcon') {}}
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Munxo
MunxoOP17mo ago
So what would you recommend? Which is going to be my simplest route? Still trying to deduce everything you've said.
client.on('interactionCreate', async (interaction) => {
if (interaction.commandName === 'rcon') {

const conn = new Rcon(process.env.HOST1_IP, process.env.HOST1_PORT, process.env.HOST1_PASS, process.env.HOST2_IP, process.env.HOST2_PORT, process.env.HOST2_PASS);
const conn2 = new Rcon(process.env.HOST2_IP, process.env.HOST2_PORT, process.env.HOST2_PASS);
const command = interaction.options.get('command').value;

console.log("Authenticated");
console.log("Sending command.")

conn.on('auth', function () {
conn.send(`/${command}`);
}).on('response',
function (str) {
console.log('Server 1: ' + str);

}).on('error', (error) => {
console.log('Error: ' + error);
})
conn.connect();

conn2.on('auth', function () {
conn2.send(`/${command}`);
}).on('response',
function (str) {
console.log('Server 2: ' + str);

}).on('error', (error) => {
console.log('Error: ' + error);
})
conn2.connect();
};

let str = interaction.reply(`**Server 1: **`)

},
);
client.on('interactionCreate', async (interaction) => {
if (interaction.commandName === 'rcon') {

const conn = new Rcon(process.env.HOST1_IP, process.env.HOST1_PORT, process.env.HOST1_PASS, process.env.HOST2_IP, process.env.HOST2_PORT, process.env.HOST2_PASS);
const conn2 = new Rcon(process.env.HOST2_IP, process.env.HOST2_PORT, process.env.HOST2_PASS);
const command = interaction.options.get('command').value;

console.log("Authenticated");
console.log("Sending command.")

conn.on('auth', function () {
conn.send(`/${command}`);
}).on('response',
function (str) {
console.log('Server 1: ' + str);

}).on('error', (error) => {
console.log('Error: ' + error);
})
conn.connect();

conn2.on('auth', function () {
conn2.send(`/${command}`);
}).on('response',
function (str) {
console.log('Server 2: ' + str);

}).on('error', (error) => {
console.log('Error: ' + error);
})
conn2.connect();
};

let str = interaction.reply(`**Server 1: **`)

},
);
That's what I have now and am testing
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Munxo
MunxoOP17mo ago
Sorry for my late response. I had to complete a scheduled job. I'm struggling a bit to follow. So, before my if (interaction.commandName === ... I should make a let str line?
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Munxo
MunxoOP17mo ago
A let str =?
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Munxo
MunxoOP17mo ago
I unsure as to what it should be equal to in order to benefit me the most for my desired outcome.
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Munxo
MunxoOP17mo ago
This will also have to work with there being the possibility of variance with what the server spits back as a response. I want it to reply back with whatever information/content the server gives depending on the command used. So if I do: /teleport munxo munxo
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Munxo
MunxoOP17mo ago
So the first chunk connecting to Server 1 should be interaction.deferReply() then later after connecting and sending the command on Server 2 then I put in interaction.editReply({‘Server 1: ‘ + str}, {‘Server 2: ’ + str}) ?
client.on('interactionCreate', async (interaction) => {

if (interaction.commandName === 'rcon') {

const conn = new Rcon(process.env.HOST1_IP, process.env.HOST1_PORT, process.env.HOST1_PASS);
const conn2 = new Rcon(process.env.HOST2_IP, process.env.HOST2_PORT, process.env.HOST2_PASS);
const command = interaction.options.get('command').value;

console.log("Authenticated");
console.log("Sending command.")

conn.on('auth', function () {
conn.send(`/${command}`);
}).on('response',
function (‘**Server 1: **’ + str) {
console.log('Server 1: ' + str);
}).on('error', (error) => {
console.log('Error: ' + error);
});
interaction.deferReply();
conn.connect();

conn2.on('auth', function () {
conn2.send(`/${command}`);
}).on('response',
function (‘**Server 2: **’ + str) {
console.log('Server 2: ' + str);
});
interaction.deferReply();
conn2.connect();
};
interaction.editReply(‘**Server 1: **’ + str, ‘**Server 2: **’ + str);
},
);
client.on('interactionCreate', async (interaction) => {

if (interaction.commandName === 'rcon') {

const conn = new Rcon(process.env.HOST1_IP, process.env.HOST1_PORT, process.env.HOST1_PASS);
const conn2 = new Rcon(process.env.HOST2_IP, process.env.HOST2_PORT, process.env.HOST2_PASS);
const command = interaction.options.get('command').value;

console.log("Authenticated");
console.log("Sending command.")

conn.on('auth', function () {
conn.send(`/${command}`);
}).on('response',
function (‘**Server 1: **’ + str) {
console.log('Server 1: ' + str);
}).on('error', (error) => {
console.log('Error: ' + error);
});
interaction.deferReply();
conn.connect();

conn2.on('auth', function () {
conn2.send(`/${command}`);
}).on('response',
function (‘**Server 2: **’ + str) {
console.log('Server 2: ' + str);
});
interaction.deferReply();
conn2.connect();
};
interaction.editReply(‘**Server 1: **’ + str, ‘**Server 2: **’ + str);
},
);
This kind of what you mean? I don’t think this would be viable, but I can’t check since I don’t have my Mac with me
Want results from more Discord servers?
Add your server