Call client in another file

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 !
10 Replies
d.js toolkit
d.js toolkit•14mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - 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!
Memw
Memw•14mo ago
You could have your client exported somewhere and import it everywhere you need it
Zaack
ZaackOP•14mo ago
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);
// 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 ? 🤔
Memw
Memw•14mo ago
You can't export in the index You need to export from other file and import in the index
Zaack
ZaackOP•14mo ago
oh
Unknown User
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
Zaack
ZaackOP•14mo ago
yea i realised it would be much easier to use the ready event to do what i need afterall, thanks for your help guys !
Unknown User
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
Memw
Memw•14mo ago
no need for it to be a function you can just export const client = new Client(/*...*/);
Unknown User
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server