将欢迎消息嵌入到 discord.js

Posted

技术标签:

【中文标题】将欢迎消息嵌入到 discord.js【英文标题】:Making a welcome message an embed on discord.js 【发布时间】:2021-10-09 23:28:15 【问题描述】:

我已将 MongoDB 连接到我的 discord.js 代码,并已将 setwelcome 命令作为每个服务器的数据,以便每个服务器都可以自定义自己的欢迎消息。一切都很好,我只想知道是否有任何方法可以使消息显示为嵌入?代码如下:

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

我知道通常如何进行嵌入,但目前我只是对嵌入的 .setDescription() 内容感到困惑。

请指教。

【问题讨论】:

您只是希望设置消息以嵌入描述而不是普通消息内容的形式发送,还是希望用户能够自定义所有嵌入字段? 只是要在嵌入描述中发送的消息,而不是普通的消息内容。 【参考方案1】:

如果您只想将消息作为嵌入发送,请创建 MessageEmbed 并使用 setDescription() 并将描述作为唯一参数。然后用channel.send(embed)发送。

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

顺便说一句,如果你对如何使用特定方法感到困惑,你可以随时在官方discord.js documentation上搜索方法名称,这样你就不必在这里等待答案了。祝您创建机器人好运!

【讨论】:

非常感谢,它看起来很简单,现在我已经得到了答案,这很有意义,但是再一次,在不停地编码 2 小时后,你开始忘记了基础。再次感谢!

以上是关于将欢迎消息嵌入到 discord.js的主要内容,如果未能解决你的问题,请参考以下文章

Discord bot,嵌入欢迎消息不起作用

Discord.js 代码无法发送嵌入消息 [关闭]

将字段添加到 c# 嵌入式不和谐消息

如何将许多不和谐嵌入附加到一条消息中?

discord.py 加入消息嵌入未发送

Discord.js V13 欢迎消息