Search code examples
mongodbmongoosediscord.jsbotsembed

Making a welcome message an embed on discord.js


I have connected MongoDB to my discord.js code and have made a setwelcome command as per-server data so that each server can customize their own welcome message. Everything works great, I just want to know if there is any way that I can make the message appear as an embed? Here's the code:

//importing all the needed files and languages
const mongo = require('./mongo')
const command = require('./command')
const welcomeSchema = require('./schemas/welcome-schema')
const mongoose = require('mongoose')
const Discord = require('discord.js')
mongoose.set('useFindAndModify', false);

//my code is inside this export
module.exports = (client) => {
//this next line is for later
    const cache = {}


    command(client, 'setwelcome', async (message) => {
        const { member, channel, content, guild } = message
//checking to see that only admins can do this
        if (!member.hasPermissions === 'ADMINISTRATOR') {
            channel.send('You do not have the permission to run this command')
            return
        }
//simplifying commands
        let text = content
//this is to store just the command and not the prefix in mongo compass
        const split = text.split(' ')

        if (split.length < 2) {
            channel.send('Please provide a welcome message!')
            return
        }

        split.shift()
        text = split.join(' ')
//this is to not fetch from the database after code ran once
        cache[guild.id] = [channel.id, text]
//this is to store the code inside mongo compass
        await mongo().then(async (mongoose) => {
            try {
                await welcomeSchema.findOneAndUpdate({
                    _id: guild.id
                }, {
                    _id: guild.id,
                    channelId: channel.id,
                    text,
                }, {
                    upsert: true
                })
            } finally {
                mongoose.connection.close()
            }
        })
    })
//this is to fetch from the database
    const onJoin = async (member) => {
        const { guild } = member
        
        let data = cache[guild.id]

        if (!data) {
            console.log('FETCHING FROM DATABASE')
            await mongo().then( async (mongoose) => {
                try {
                    const result = await welcomeSchema.findOne({ _id: guild.id })

                    cache[guild.id] = data = [result.channelId, result.text]
                } finally {
                    mongoose.connection.close()
                }
            })
        }
//this is to simplify into variables
        const channelId = data[0]
        const text = data[1]
/*this is where the message sends on discord. the second of these 2 lines is what I want embedded
which is basically the welcome message itself*/
        const channel = guild.channels.cache.get(channelId)
        channel.send(text.replace(/<@>/g, `<@${member.id}>`))
    }
//this is to test the command
    command(client, 'simjoin', message => {
        onJoin(message.member)
    })
//this is so the command works when someone joins
    client.on('guildMemberAdd', member => {
        onJoin(member)
    })
}

I know how to usually make an embed, but I'm just confused at the moment on what to put as .setDescription() for the embed.

Please advise.


Solution

  • If you just want to have the message be sent as an embed, create a MessageEmbed and use setDescription() with the description as the only argument. Then send it with channel.send(embed).

    const embed = new Discord.MessageEmbed();
    embed.setDescription(text.replace(/<@>/g, `<@${member.id}>`));
    channel.send(embed);
    

    By the way, if you are confused about how to use a specific method you can always search for the method name on the official discord.js documentation so you don’t have to wait for an answer here. Good luck creating your bot!