#Call client in another file
19 messages · Page 1 of 1 (latest)
- What's your exact discord.js
npm list discord.jsand nodenode -vversion? - 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!
You could have your client exported somewhere and import it everywhere you need it
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 ? 🤔
You can't export in the index
You need to export from other file and import in the index
oh
export a function which takes the client as a parameter
Then inside of your index, import it and then pass your client
yea i realised it would be much easier to use the ready event to do what i need afterall, thanks for your help guys !
That works too ofc
no need for it to be a function
you can just export const client = new Client(/*...*/);