如何在命令库中嵌入消息

Posted

技术标签:

【中文标题】如何在命令库中嵌入消息【英文标题】:How to embed a message in a command base 【发布时间】:2021-10-11 00:38:21 【问题描述】:

我正在使用 WOK 的高级命令处理程序,我只想将返回的最终消息作为嵌入。

我正在处理一个特定的测试命令,当我运行 !add 5 10 时,我输入前缀(例如 !)我希望机器人 ping 我,所以它说 @Ping , 5 + 10 = 15

我想解决两件事:

    我不想在 ping 后加逗号 我希望将出现在 discord 上的消息嵌入。

这是命令库的代码:

const Discord = require('discord.js')
const  prefix  = require('../config.json')

const validatePermissions = (permissions) => 
    const validPermissions = [
        'ADMINISTRATOR',
        'CREATE_INSTANT_INVITE',
        'KICK_MEMBERS',
        'BAN_MEMBERS',
        'MANAGE_CHANNELS',
        'MANAGE_GUILD',
        'ADD_REACTIONS',
        'VIEW_AUDIT_LOG',
        'PRIORITY_SPEAKER',
        'STREAM',
        'VIEW_CHANNEL',
        'SEND_MESSAGES',
        'SEND_TTS_MESSAGES',
        'MANAGE_MESSAGES',
        'EMBED_LINKS',
        'ATTACH_FILES',
        'READ_MESSAGE_HISTORY',
        'MENTION_EVERYONE',
        'USE_EXTERNAL_EMOJIS',
        'VIEW_GUILD_INSIGHTS',
        'CONNECT',
        'SPEAK',
        'MUTE_MEMBERS',
        'DEAFEN_MEMBERS',
        'MOVE_MEMBERS',
        'USE_VAD',
        'CHANGE_NICKNAME',
        'MANAGE_NICKNAMES',
        'MANAGE_ROLES',
        'MANAGE_WEBHOOKS',
        'MANAGE_EMOJIS_AND_STICKERS',
        'USE_SLASH_COMMANDS',
        'REQUEST_TO_SPEAK',
        'MANAGE_THREADS',
        'USE_PUBLIC_THREADS',
        'USE_PRIVATE_THREADS',
        'USE_EXTERNAL_STICKERS',
    ]

    for (const permission of permissions) 
        if (!validPermissions.includes(permission)) 
            throw new Error(`Unknown permission node "$permission"`)
        
    


module.exports = (client, commandOptions) => 
    let 
        commands,
        expectedArgs = '',
        permissionError = 'You do not have the permission to run this command.',
        minArgs = 0,
        maxArgs = null,
        permissions = [],
        requiredRoles = [],
        callback
     = commandOptions

    if (typeof commands === 'string') 
        commands = [commands]
    

    console.log(`Registering the "$commands[0]" command!`)

    if (permissions.length) 
        if (typeof permissions === 'string') 
            permissions = [permissions]
        

        validatePermissions(permissions)
    

    client.on('message', (message) => 
        const  member, content, guild  = message

        for (const alias of commands) 
            const command = `$prefix$alias.toLowerCase()`

      if (
        content.toLowerCase().startsWith(`$command `) ||
        content.toLowerCase() === command
      ) 

        for (const permission of permissions) 
          if (!member.hasPermission(permission)) 
            message.reply(permissionError)
            return
          
        

                for (const requiredRole of requiredRoles) 
                    const role = guild.roles.cache.find((role) => role.name === requiredRole)

                    if (!role || !member.roles.cache.has(role.id)) 
                        message.reply(`You must have the "$requiredRole" role to use this command.`)
                        return
                    
                

                const arguments = content.split(/[ ]+/)

                arguments.shift()

                if (arguments.length < minArgs || (
                    maxArgs !== null && arguments.length > maxArgs
                )) 
                    const embed = new Discord.MessageEmbed()
                    embed.setTitle(`Incorrect syntax! Use $prefix$alias $expectedArgs`)
                    message.reply(embed)
                    return
                

                callback(message, arguments, arguments.join(' '), client) 

                return
            
        
    )

这里是特定add 命令的代码:

module.exports = 
    commands: ['add', 'addition'],
    expectedArgs: '<num1> <num2>',
    permissionError: 'You do not have the permission to run this command.',
    minArgs: 2,
    maxArgs: 2,
    callback: (message, arguments, text) => 
        const num1 = +arguments[0]
        const num2 = +arguments[1]

        message.reply(`$num1 + $num2 = $num1 + num2`)
    ,
    permissions: ['SEND_MESSAGES'],
    requiredRoles: ['.???????????? ₪ | ????????????????????????????????'],

请指教。

【问题讨论】:

【参考方案1】:

在您的函数中需要 discord.js 并使用 Discord.MessageEmbed() 创建嵌入。

要 ping 运行命令的用户,请使用 &lt;@$message.author.id&gt;

module.exports = 
    commands: ['add', 'addition'],
    expectedArgs: '<num1> <num2>',
    permissionError: 'You do not have the permission to run this command.',
    minArgs: 2,
    maxArgs: 2,
    callback: (message, arguments, text) => 
        const Discord = require("discord.js")
        const num1 = +arguments[0]
        const num2 = +arguments[1]

        const addEmbed = new Discord.MessageEmbed()
          .setTitle(`<@$message.author.id> $num1 + $num2 = $num1 + num2`)

        message.reply(addEmbed)
    ,
    permissions: ['SEND_MESSAGES'],
    requiredRoles: ['.??? ₪ | ????????'],

【讨论】:

以上是关于如何在命令库中嵌入消息的主要内容,如果未能解决你的问题,请参考以下文章

如何在机器人嵌入中复制​​用户的消息

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

如何使用变量设置 Discord 嵌入消息的图像?

Discord js - 如何在给定的时间段内继续对消息做出反应并编辑嵌入

如何添加对嵌入消息 JDA 的反应

如何使用 discord.py 获取消息