Search code examples
javascriptnode.jsdiscorddiscord.js

Discord bot how to send a message (simplest form)


I want to send a message with my discord bot the simplest way possible, online I'll I've found is stuff that are too complex for me, and I can't get them too work.

require("dotenv").config(); //to start process from .env file
const { Client, GatewayIntentBits } = require('discord.js')
const client = new Client({
    intents: [
        GatewayIntentBits.Guilds, //adds server functionality
        GatewayIntentBits.GuildMessageTyping, //gets messages from our bot.
        GatewayIntentBits.GuildMessages,
        //GatewayIntentBits.GuildEmojisAndStickers,
        //GatewayIntentBits.GuildMembers
    ]
})
client.once("ready", () =>{
    console.log("Bot is online!"); //message when bot is online
})
client.login(process.env.TOKEN);


client.on('message',
function (messages){
    if(messages.content.toLocaleLowerCase()==='hello') 
    messages.channel.send('Hello: ' + messages.author.username); //reply hello word message with senders name
})

If I change my code to this, it still doesn't work:

const {
  Client,
  Partials,
  Collection,
  GatewayIntentBits,
} = require('discord.js');

const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.GuildPresences,
    GatewayIntentBits.GuildMessageReactions,
    GatewayIntentBits.DirectMessages,
    GatewayIntentBits.MessageContent,
  ],
  partials: [
    Partials.Channel,
    Partials.Message,
    Partials.User,
    Partials.GuildMember,
    Partials.Reaction,
  ],
});

Solution

  • Discord introduced the MessageContent intent a while ago which has to be enabled in your code (via GatewayIntentBits) and also has to be enabled in the Discord Developer Portal as "Privileged Intent" (Can be found under your bot > "Bot" (on the left navigation menu) > Pivileged Gateway Intents > Message Content Intet > enable this option).