#variables

24 messages · Page 1 of 1 (latest)

lucid condor

Or, how do I define my connection to a variable, that could be used anywhere

lone herald

When you initialize your client you can give it extra properties like a connection to something.

async main() {
  const client = new Client(...)
  client.connection = new ConnectionToSomething()
  await client.connection.connect() 
  await client.login()
}
main().catch(console.error)

When you handle interactions you can than do interaction.client.connection to access your connection since js Objects are mutable and are thus passed around in form of references.

async (interaction) => {
  assert(interaction.client.connection instanceof ConnectionToSomething)
}

@lucid condor

lucid condor
lone herald
lucid condor

That is what I tried

lone herald

oh my

lucid condor

I set is as var atm, I was just testing to see if there was a differnce

lucid condor
lone herald

is connection->connect async ?

lucid condor

Not sure

But it is this function

lone herald

it is async

you can export a async function that creates the connection

lucid condor

That is my ready.js

lone herald
module.exports = async function rconConnect() {
  const conn = new Rcon(...)
  await conn.connect()
  return conn
}

then in client

const rconConnect = require(...);

async main() {
  const client = new Client(...)
  client.rcon = await rconConnect()
  await client.login()
}

main().catch(console.error)
lone herald

try to avoid dangling promises