javascript - 如何将别名添加到此不和谐机器人命令以及如何嵌入机器人的响应?
问题描述
我想给命令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]}**`)
}
});
解决方案
如果您只是为命令处理程序使用链,则使用逻辑运算符if...else
创建别名相当容易,如果多个条件之一为真,它将返回。OR
true
// 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 });
推荐阅读
- python - 从类名中获取数据
- typescript - 如何将对象状态恢复为初始状态?
- rust - 在 Rust 中,如何在格式中使用变量!宏?
- firebase - Flutter - 后台通知在 iOS 中不起作用
- mongodb - 放置在数组MongoDB中时如何对对象字段的值求和
- jsonpath - 如何通过 JSONPath 通过其中的特定值过滤整个字典?
- c - 我无法在 C 中返回字符串
- ionic-framework - 如何使用电容器中的 ionic-native-http 在 Post 请求中传递表单数据?
- sql - SQL 如果注释被分成多行,我如何更新列以包含更多文本
- javascript - 使用 DragNDrop api 在跳棋游戏中删除 Dragover 上的棋子