-1

I'm trying to figure out how to send a message to a discord channel through a function call instead of a user sending a message. I'm trying to make something that can trigger a message through an external button, but I need the function call to send a standalone message instead of a message reply (which is how I have it now). Current code in javascript: app.js

require('dotenv').config();
const { Client, IntentsBitField } = require('discord.js');

const client = new Client({
    intents: [
        IntentsBitField.Flags.Guilds,
        IntentsBitField.Flags.GuildMembers,
        IntentsBitField.Flags.GuildMessages,
        IntentsBitField.Flags.MessageContent
    ]
});

client.on('ready', (c) => {
    console.log(c.user.tag + ' is online');

    var generalChannel = c.users.cache.get(process.env.CHANNEL_ID)
    generalChannel.send('testSuccessful');
})


client.on('messageCreate', (message)=>{
     if (message.content ==='say ping'){
         message.reply('!pong');
     }
});

client.login(process.env.TOKEN);

Basically, my goal is to send a message to discord showing that the discord bot is ready, but I'm not sure how to send a message without it being a response to a message with message.reply() function.

One of the attempts appears up above with

var generalChannel = c.users.cache.get(process.env.CHANNEL_ID)
    generalChannel.send('testSuccessful');

where this just sends an error where send is "undefined". A similar thing I tried was to get the channel ID through the client

var generalChannel = c.channels.fetch(process.env.CHANNEL_ID);
generalChannel.send('testSuccessful');

with similar results. Is there a way to send a message without it being a reply to something or without dm-ing a user

1 Answer 1

1

Use either of the following methods

const generalChannel = c.channels.cache.get(process.env.CHANNEL_ID);
generalChannel.send('testSuccessful');
const generalChannel = await c.channels.fetch(process.env.CHANNEL_ID);
generalChannel.send('testSuccessful');

If you want to use await, you need to mark function as async.

client.on('ready', async (c) => {
  // ...

Not the answer you're looking for? Browse other questions tagged or ask your own question.