首页 > 解决方案 > 如何将别名添加到此不和谐机器人命令以及如何嵌入机器人的响应?

问题描述

我想给命令slowmode 起一个别名,但我不知道该怎么做。而且我还希望机器人嵌入响应,而不是像普通响应一样发送它。帮助表示赞赏!

const command = message.content
        .slice(prefix.length)
        .toLowerCase()
        .split(" ")[0]
        .toLowerCase();

    const args = message.content
        .slice(prefix.length)
        .split(" ")
        .slice(1);

    if (command === "slowmode") {
        if(!message.member.hasPermission("MANAGE_CHANNELS")) return message.channel.send("You don't have access to this command!");

        // Checks if `args[0]` doesn't exist or isn't a number.
        if (!args[0]) return message.channel.send("You did not specify a correct amount of time!")
        if(isNaN(args[0])) return message.channel.send("That is not a number!")

        // Check if `args[0]` is a correct amount of time to set slowmode
        // Array `validNumbers` are an array consistent of numbers of time you can set slowmode with.
        const validNumbers = [0, 5, 10, 15, 30, 60, 120, 300, 600, 900, 1800, 3600, 7200, 21600]

        // Check if `args[0]` parsed into an interger is included in `validNumbers`
        if(!validNumbers.includes(parseInt(args[0]))) return message.channel.send("Invalid Number! Number must be one of the following `5, 10, 15, 30, 60, 120, 300, 600, 900, 1800, 3600, 7200, 21600`.");

        // Set the slowmode
        message.channel.setRateLimitPerUser(args[0]);
        
        // Send the reply
        message.channel.send(`Slowmode Set to **${args[0]}**`)
    }
});

标签: javascriptnode.jsdiscorddiscord.js

解决方案


如果您只是为命令处理程序使用链,则使用逻辑运算符if...else创建别名相当容易,如果多个条件之一为真,它将返回。ORtrue

// if you have only one or two extra alias:
if (command === 'slowmode' || command = 'sm')

const name = 'john'

if (name === 'james') console.log('Your name is james');
if (name === 'john') console.log('Your name is john');
if (name === 'james' || name === 'john') console.log('Your name is james *or* john');


如果您想要使用大量的别名,您可以将它们放在一个数组中并使用该方法,如果数组元素之一等于给定参数,该Array.prototype.includes()方法将返回。true

const aliases = ['slowmode', 'sm', 'slow', 'sl', 'unfast'];

if (aliases.includes(command))

const names = ['john', 'james', 'jonah', 'jack', 'jerry']
const winner = 'jack'

if (names.includes(winner)) console.log('One of these people is the winner')


我还希望机器人嵌入响应,而不是像普通响应一样发送。

从官方指南访问此页面discord.js以了解有关嵌入的所有信息;包括如何制作和定制它们。要进行基本嵌入,您可以使用构造函数或对象:

const { MessageEmbed } = require("discord.js");

// regular text
message.channel.send("You did not specify a correct amount of time!");

// *sample* embed constructor
const embed = new MessageEmbed()
  .setColor("RED")
  .setTitle("You did not specify a correct amount of time!")
  .setFooter("Please try again");

message.channel.send(embed);

// *sample* embed object
const embed = {
  color: "RED",
  title: "You did not specify a correct amount of time!",
  footer: "Please try again",
};

message.channel.send({ embed });

推荐阅读