我一直试图向我的Discord机器人添加一些更有趣的命令,这些命令之一是用户可以键入带有文本的命令,并且该机器人会将这些文本随机放入大写字母。 我一直在尝试使它工作一段时间,但是只能在文本已经编码后才能使它工作。随机化本身确实可以工作
命令示例:
用户:+随机文本在这里
Bot:TeXt HeRe
到目前为止,我的代码是
const Discord = require("discord.js");
module.exports = {
name: "random",
category: "fun",
description: "Sends random caps text back",
usage: "+random <text>",
run: async (client, message, args) => {
//Checks if there is something to randomize
if (!args[0]) return message.reply("Please also provide text to randomize");
//Text needs to be in a const here
const str = "text the user has submitted";
//Gets the randomized text and sends it
const randified = randify(str);
message.channel.send(randified);
//Randomizes the text
function randify(string) {
const arr = string.toLowerCase().split("");
arr.forEach((e, i, a) => (Math.random() > .4) ? a[i] = e.toUpperCase() : e);
return arr.join("");
};
}
如您所见,我目前需要将文本保存在const中,并且一直没有它就可以使用。
答案 0 :(得分:0)
请尝试使用此方法,它使用map
而不是forEach
,因为它将返回一个可以连接的字符数组。
const randified = text
.split('')
.map(char => Math.random() > 0.5 ? char.toUpperCase() : char.toLowerCase())
.join('')
或者如果您需要单独的功能:
function randify(input) {
return input
.split('')
.map(char => Math.random() > 0.5 ? char.toUpperCase() : char.toLowerCase())
.join('')
}