#Call client in another file

19 messages · Page 1 of 1 (latest)

noble oar
#

Hello !
I have my client instance declared in my index.ts as usual and would like to be able to call it in another file of my project and use it to send a message.

I'm listening for a twitch stream event, and would like to then send a message when this event trigger.

Any tips or documentation ?
Thanks !

digital moatBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Not a discord.js issue? Check out #1081585952654360687.
  • 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!
rare badger
#

You could have your client exported somewhere and import it everywhere you need it

noble oar
#

Oh

#

so

#
// Import necessary modules from libraries
import { ActivityType, Client, Collection, GatewayIntentBits } from "discord.js"; // Imports the Client and Collection classes from the "discord.js" library
import { token } from "../config.json"; // Imports the authentication token from the "config.json" file
import { BotEvent, SlashCommand } from "./types"; // Imports custom types "BotEvent" and "SlashCommand" from the "types" folder
import path from "path"; // Imports the "path" module to manage file paths
import fs, { PathLike } from "fs"; // Imports the "fs" module to handle file operations

// Create a new client instance
const client: Client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages],
    presence: {
        activities: [{
            name: 'salu',
            type: ActivityType.Streaming,
            url: 'https://twitch.tv/zaacklachevre'
        }]
    }
});

// Create a collection to store slash commands
client.slashCommands = new Collection();

// Set the path to the folder containing commands
const foldersPath: PathLike = path.join(__dirname, 'commands');
// Read the list of folders in the specified path
const commandFolders: string[] = fs.readdirSync(foldersPath);
#

In this, I could just replace const client... with export const client, then import it ?

#

looks like i'm accessing it from my other file when importing it, but then how would I create and send a message to a specific channel ? 🤔

rare badger
#

You need to export from other file and import in the index

noble oar
#

oh

patent shadow
#

Then inside of your index, import it and then pass your client

noble oar
#

yea i realised it would be much easier to use the ready event to do what i need afterall, thanks for your help guys !

patent shadow
#

That works too ofc

rare badger
#

you can just export const client = new Client(/*...*/);

patent shadow
#

You shouldn’t export your client at all

#

It’ll lead to circular dependency stuff