#to send a message in a specific channel

90 messages · Page 1 of 1 (latest)

loud fern
#

hello I would like to send a message in a specific channel here is my code

orchid stumpBOT
#
  • 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!
loud fern
#

        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

potent frost
#
- const channel = (channelId);
- channel.send('ne marche pas');

+ const channel = client.channels.cache.get(channelId);
+ channel.send('marche');
loud fern
#

i try it thx

#

there are no more errors but the message is not sent

potent frost
#

Oh the channel type check is different in v14 btw

loud fern
#

ok

potent frost
#
-channel.isText()
+channel.type === ChannelType.GuildText
#

ChannelType is imported from djs

#

It's weird that this didn't sent any errors Thonk

loud fern
#

not work

#

you are french ?

potent frost
#

Non

loud fern
#

ok

#

no error

potent frost
#

Nothing is sent by the bot?

#

Did you save the file and restarted the bot?

loud fern
#

yes

potent frost
#

Did you check is the channel invalid or is the id correct? Thinkeng

loud fern
#

🤣

#

im stupid

#

wait

potent frost
#

Waiting dogeHaHa

loud fern
#
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)
potent frost
#

Oh what did you define as the client?

loud fern
#
const { Client, EmbedBuilder} = require('discord.js');
#

?

potent frost
#

The bot client

loud fern
#

?

#

yes

potent frost
#

change the client with interaction.client

#

Or what you named your interaction

#

In the code snippet i sent

loud fern
#

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)
#

as been saved

potent frost
#

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

loud fern
#

i no handler

#

😅

strong veldt
#

you have commands folder..

#

show ur index.js

potent frost
#

The error spawns at ping.js Thonk

loud fern
#

all index.js ??

strong veldt
loud fern
#

I do not have any 😅

strong veldt
#

then how do you access all commands in the commands folder?

loud fern
#

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);
}```
strong veldt
loud fern
#

euh ?

strong veldt
loud fern
#

I don't think I have one either

potent frost
#

Just show ping.js

loud fern
#

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 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);
        }

        
  


    },
};
potent frost
#
const channel = message.client.channels.get(...)
loud fern
#

Erreur lors de l'envoi du message : TypeError: message.client.channels.get is not a function

potent frost
strong veldt
#

message.client.channels.cache.get()

potent frost
#

Oh woops

shadow pumiceBOT
#

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
strong veldt
#

remove "GUILD_TEXT" and use ChannelType.GuildText

loud fern
#

🤣

#

wait

potent frost
#

Waiting again dogeHaHa

loud fern
#

Yes it is

loud fern
#

Hu
Erreur lors de l'envoi du message : ReferenceError: ChannelType is not defined

#

alors

#

hmm

strong veldt
#

import it

#

const { ChannelType, EmbedBuilder } = require("discord.js");

loud fern
#

yes is work

#

i love you Kin and Disaster

potent frost
#

De rien

loud fern
rough pumice
potent frost
#

Will keep in mind

rough pumice
potent frost
#

Aah i didn't update to that yet

rough pumice
#

Wha… you mean you‘re still on 14.0.0? That’s deprecated