to send a message in a specific channel

hello I would like to send a message in a specific channel here is my code
56 Replies
d.js toolkit
d.js toolkit7mo 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!
./u.7-z
./u.7-z7mo ago
const channelId = '1183311645724917810'; // Remplace par l'ID de ton canal

try {
const channel = (channelId);

if (channel.isText()) {
channel.send('Salut à toi ! Ceci est un message envoyé depuis une commande slash.');
message.reply('Message envoyé dans le canal spécifié !');
} else {
message.reply('Le canal spécifié n\'est pas un canal textuel.');
}
} catch (error) {
console.log('Erreur lors de l\'envoi du message :', error);
}
const channelId = '1183311645724917810'; // Remplace par l'ID de ton canal

try {
const channel = (channelId);

if (channel.isText()) {
channel.send('Salut à toi ! Ceci est un message envoyé depuis une commande slash.');
message.reply('Message envoyé dans le canal spécifié !');
} else {
message.reply('Le canal spécifié n\'est pas un canal textuel.');
}
} catch (error) {
console.log('Erreur lors de l\'envoi du message :', error);
}
error : Cannot send empty message
Disaster
Disaster7mo ago
- const channel = (channelId);
- channel.send('ne marche pas');

+ const channel = client.channels.cache.get(channelId);
+ channel.send('marche');
- const channel = (channelId);
- channel.send('ne marche pas');

+ const channel = client.channels.cache.get(channelId);
+ channel.send('marche');
./u.7-z
./u.7-z7mo ago
i try it thx there are no more errors but the message is not sent
Disaster
Disaster7mo ago
Oh the channel type check is different in v14 btw
./u.7-z
./u.7-z7mo ago
ok
Disaster
Disaster7mo ago
-channel.isText()
+channel.type === ChannelType.GuildText
-channel.isText()
+channel.type === ChannelType.GuildText
ChannelType is imported from djs It's weird that this didn't sent any errors Thonk
./u.7-z
./u.7-z7mo ago
not work you are french ?
Disaster
Disaster7mo ago
Non
./u.7-z
./u.7-z7mo ago
ok no error
Disaster
Disaster7mo ago
Nothing is sent by the bot? Did you save the file and restarted the bot?
./u.7-z
./u.7-z7mo ago
yes
Disaster
Disaster7mo ago
Did you check is the channel invalid or is the id correct? Thinkeng
./u.7-z
./u.7-z7mo ago
🤣 im stupid wait
Disaster
Disaster7mo ago
Waiting dogeHaHa
./u.7-z
./u.7-z7mo ago
Erreur lors de l'envoi du message : ReferenceError: client is not defined
at Object.execute (E:\Areo bot\commands\ping.js:85:29)
at Client.<anonymous> (E:\Areo bot\index.js:64:17)
at Client.emit (node:events:531:35)
at MessageCreateAction.handle (E:\Areo bot\node_modules\discord.js\src\client\actions\MessageCreate.js:28:14)
at module.exports [as MESSAGE_CREATE] (E:\Areo bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (E:\Areo bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:355:31)
at WebSocketManager.<anonymous> (E:\Areo bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:239:12)
at WebSocketManager.emit (E:\Areo bot\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31)
at WebSocketShard.<anonymous> (E:\Areo bot\node_modules\@discordjs\ws\dist\index.js:1173:51)
at WebSocketShard.emit (E:\Areo bot\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31)
Erreur lors de l'envoi du message : ReferenceError: client is not defined
at Object.execute (E:\Areo bot\commands\ping.js:85:29)
at Client.<anonymous> (E:\Areo bot\index.js:64:17)
at Client.emit (node:events:531:35)
at MessageCreateAction.handle (E:\Areo bot\node_modules\discord.js\src\client\actions\MessageCreate.js:28:14)
at module.exports [as MESSAGE_CREATE] (E:\Areo bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (E:\Areo bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:355:31)
at WebSocketManager.<anonymous> (E:\Areo bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:239:12)
at WebSocketManager.emit (E:\Areo bot\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31)
at WebSocketShard.<anonymous> (E:\Areo bot\node_modules\@discordjs\ws\dist\index.js:1173:51)
at WebSocketShard.emit (E:\Areo bot\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31)
Disaster
Disaster7mo ago
Oh what did you define as the client?
./u.7-z
./u.7-z7mo ago
const { Client, EmbedBuilder} = require('discord.js');
const { Client, EmbedBuilder} = require('discord.js');
?
Disaster
Disaster7mo ago
The bot client
./u.7-z
./u.7-z7mo ago
? yes
Disaster
Disaster7mo ago
change the client with interaction.client Or what you named your interaction In the code snippet i sent
./u.7-z
./u.7-z7mo ago
noo
Erreur lors de l'envoi du message : ReferenceError: client is not defined
at Object.execute (E:\Areo bot\commands\ping.js:85:29)
at Client.<anonymous> (E:\Areo bot\index.js:64:17)
at Client.emit (node:events:531:35)
at MessageCreateAction.handle (E:\Areo bot\node_modules\discord.js\src\client\actions\MessageCreate.js:28:14)
at module.exports [as MESSAGE_CREATE] (E:\Areo bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (E:\Areo bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:355:31)
at WebSocketManager.<anonymous> (E:\Areo bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:239:12)
at WebSocketManager.emit (E:\Areo bot\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31)
at WebSocketShard.<anonymous> (E:\Areo bot\node_modules\@discordjs\ws\dist\index.js:1173:51)
at WebSocketShard.emit (E:\Areo bot\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31)
Erreur lors de l'envoi du message : ReferenceError: client is not defined
at Object.execute (E:\Areo bot\commands\ping.js:85:29)
at Client.<anonymous> (E:\Areo bot\index.js:64:17)
at Client.emit (node:events:531:35)
at MessageCreateAction.handle (E:\Areo bot\node_modules\discord.js\src\client\actions\MessageCreate.js:28:14)
at module.exports [as MESSAGE_CREATE] (E:\Areo bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (E:\Areo bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:355:31)
at WebSocketManager.<anonymous> (E:\Areo bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:239:12)
at WebSocketManager.emit (E:\Areo bot\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31)
at WebSocketShard.<anonymous> (E:\Areo bot\node_modules\@discordjs\ws\dist\index.js:1173:51)
at WebSocketShard.emit (E:\Areo bot\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31)
as been saved
Disaster
Disaster7mo ago
Yeah because client isn't defined You need the client, which is available via <interaction>.client and interaction being what you defined your interaction in the handler
./u.7-z
./u.7-z7mo ago
i no handler
./u.7-z
./u.7-z7mo ago
No description
./u.7-z
./u.7-z7mo ago
😅
kin.ts
kin.ts7mo ago
you have commands folder.. show ur index.js
Disaster
Disaster7mo ago
The error spawns at ping.js Thonk
./u.7-z
./u.7-z7mo ago
all index.js ??
kin.ts
kin.ts7mo ago
just the commands handler part
./u.7-z
./u.7-z7mo ago
I do not have any 😅
kin.ts
kin.ts7mo ago
then how do you access all commands in the commands folder?
./u.7-z
./u.7-z7mo ago
hmm
client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
kin.ts
kin.ts7mo ago
how ur interactionCreate event looks like
./u.7-z
./u.7-z7mo ago
euh ?
kin.ts
kin.ts7mo ago
mb, i just read the issue, use this in your ping command
./u.7-z
./u.7-z7mo ago
I don't think I have one either
Disaster
Disaster7mo ago
Just show ping.js
./u.7-z
./u.7-z7mo ago
euh
const { interactionclient, EmbedBuilder} = require('discord.js');


module.exports = {
name: 'ping',
description: 'Affiche la latence du bot Discord sous forme d\'embed.',
execute(message, args) {
const ping = Date.now() - message.createdTimestamp;
const memoryUsage = process.memoryUsage().heapUsed / 1024 / 1024;
const roundedMemory = Math.round(memoryUsage * 100) / 100;
let roundedping;
if (ping < 0) {
roundedping = Math.round(ping * -1);
} else {
roundedping = ping;
}

const guildId = message.guild.id;
const guildname = message.guild.name;
const username = message.author.username;
const userid = message.author.id;
const { interactionclient, EmbedBuilder} = require('discord.js');


module.exports = {
name: 'ping',
description: 'Affiche la latence du bot Discord sous forme d\'embed.',
execute(message, args) {
const ping = Date.now() - message.createdTimestamp;
const memoryUsage = process.memoryUsage().heapUsed / 1024 / 1024;
const roundedMemory = Math.round(memoryUsage * 100) / 100;
let roundedping;
if (ping < 0) {
roundedping = Math.round(ping * -1);
} else {
roundedping = ping;
}

const guildId = message.guild.id;
const guildname = message.guild.name;
const username = message.author.username;
const userid = message.author.id;
js
const log = `Commande !ping executer en ${roundedping}ms dans (${guildId} ${guildname}) par ${userid} ${username}`
console.log(log);




if (roundedping < 1) {
const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle(':ping_pong: Pong! :1872dndweb:' )
.setDescription(`La latence du bot est de ${roundedping}ms`)
.addFields(
{ name: 'Ram', value: `${roundedMemory}MB`, inline: false}
)
.setTimestamp();
message.reply({ content: ' ', embeds: [embed] })
.catch(console.error);


} else if (roundedping >= 1 && roundedping < 500) {
const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle(':ping_pong: Pong! :3832_status_online:' )
.setDescription(`La latence du bot est de ${roundedping}ms`)
.addFields(
{ name: 'Ram', value: `${roundedMemory}MB`, inline: false}
)
.setTimestamp();
message.reply({ content: ' ', embeds: [embed] })
.catch(console.error);



} else if (roundedping >= 500 && roundedping < 1000) {
const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle(':ping_pong: Pong! :6708_status_idle: ' )
.setDescription(`La latence du bot est de ${roundedping}ms`)
.addFields(
{ name: 'Ram', value: `${roundedMemory}MB`, inline: false}
)
.setTimestamp();
message.reply({ content: ' ', embeds: [embed] })
.catch(console.error);
} else {
js
const log = `Commande !ping executer en ${roundedping}ms dans (${guildId} ${guildname}) par ${userid} ${username}`
console.log(log);




if (roundedping < 1) {
const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle(':ping_pong: Pong! :1872dndweb:' )
.setDescription(`La latence du bot est de ${roundedping}ms`)
.addFields(
{ name: 'Ram', value: `${roundedMemory}MB`, inline: false}
)
.setTimestamp();
message.reply({ content: ' ', embeds: [embed] })
.catch(console.error);


} else if (roundedping >= 1 && roundedping < 500) {
const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle(':ping_pong: Pong! :3832_status_online:' )
.setDescription(`La latence du bot est de ${roundedping}ms`)
.addFields(
{ name: 'Ram', value: `${roundedMemory}MB`, inline: false}
)
.setTimestamp();
message.reply({ content: ' ', embeds: [embed] })
.catch(console.error);



} else if (roundedping >= 500 && roundedping < 1000) {
const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle(':ping_pong: Pong! :6708_status_idle: ' )
.setDescription(`La latence du bot est de ${roundedping}ms`)
.addFields(
{ name: 'Ram', value: `${roundedMemory}MB`, inline: false}
)
.setTimestamp();
message.reply({ content: ' ', embeds: [embed] })
.catch(console.error);
} else {
const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle(':ping_pong: Pong! :2267_status_dnd: ' )
.setDescription(`La latence du bot est de ${roundedping}ms`)
.addFields(
{ name: 'Ram', value: `${roundedMemory}MB`, inline: false}
)
.setTimestamp();
message.reply({ content: ' ', embeds: [embed] })
.catch(console.error);

}



const channelId = '1183311645724917810'; // Remplace par l'ID de ton canal

try {
const channel = client.channels.cache.get(channelId);

if (channel.type === 'GUILD_TEXT') {
channel.send('f');
message.reply('Message envoyé dans le canal spécifié !');
} else {
message.reply('Le canal spécifié n\'est pas un canal textuel.');
}
} catch (error) {
console.log('Erreur lors de l\'envoi du message :', error);
}





},
};
const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle(':ping_pong: Pong! :2267_status_dnd: ' )
.setDescription(`La latence du bot est de ${roundedping}ms`)
.addFields(
{ name: 'Ram', value: `${roundedMemory}MB`, inline: false}
)
.setTimestamp();
message.reply({ content: ' ', embeds: [embed] })
.catch(console.error);

}



const channelId = '1183311645724917810'; // Remplace par l'ID de ton canal

try {
const channel = client.channels.cache.get(channelId);

if (channel.type === 'GUILD_TEXT') {
channel.send('f');
message.reply('Message envoyé dans le canal spécifié !');
} else {
message.reply('Le canal spécifié n\'est pas un canal textuel.');
}
} catch (error) {
console.log('Erreur lors de l\'envoi du message :', error);
}





},
};
Disaster
Disaster7mo ago
const channel = message.client.channels.get(...)
const channel = message.client.channels.get(...)
./u.7-z
./u.7-z7mo ago
Erreur lors de l'envoi du message : TypeError: message.client.channels.get is not a function
Disaster
Disaster7mo ago
Thonk
kin.ts
kin.ts7mo ago
message.client.channels.cache.get()
Disaster
Disaster7mo ago
Oh woops
d.js docs
d.js docs7mo ago
RangeError [BitFieldInvalid]: Invalid bitfield flag or number: undefined - All SCREAMING_SNAKE_CASE enums have been changed to PascalCase - Intents: Intents.FLAGS.GUILD_MESSAGES -> GatewayIntentBits.GuildMessages - Permissions: Permissions.FLAGS.SEND_MESSAGES -> PermissionFlagsBits.SendMessages
kin.ts
kin.ts7mo ago
remove "GUILD_TEXT" and use ChannelType.GuildText
./u.7-z
./u.7-z7mo ago
🤣 wait
Disaster
Disaster7mo ago
Waiting again dogeHaHa
./u.7-z
./u.7-z7mo ago
No description
./u.7-z
./u.7-z7mo ago
Yes it is ok Hu Erreur lors de l'envoi du message : ReferenceError: ChannelType is not defined alors hmm
kin.ts
kin.ts7mo ago
import it const { ChannelType, EmbedBuilder } = require("discord.js");
./u.7-z
./u.7-z7mo ago
yes is work i love you Kin and Disaster
Disaster
Disaster7mo ago
De rien
./u.7-z
./u.7-z7mo ago
🙂
Disaster
Disaster7mo ago
I saw isText() being deprecated and the guide suggesting to use enums. I didn't know it just had a rename Thonk keep in mind Aah i didn't update to that yet I didn't notice meguFace