根据命令输入从目录中抓取随机图像

时间:2018-10-20 04:11:18

标签: javascript discord discord.js

这是新事物,但是我正在尝试为不和谐的bot发出命令,该命令可以解释用户的输入,并根据他们输入的数字获取相应的图像。

示例:“!randomimag 3”

输出:在目录中张贴名为“ 3”的图像

我只是想弄清楚将字符串拆分为字符并读取数字以了解从目录中抓取哪个图像的细节

module.exports.run = async (bot, message, args) => {
let target = message.mentions.users.first() || message.author;
let chars = message.split();
let imageNumber = chars[12]; //grab the 1 digit number that starts on 12th character

let embededImage = new Discord.RichEmbed()
    .setImage("/images/%s", imageNumber) //get corresponding image from directory

message.channel.send({embededImage: embededImage}) //send it yo
}

module.exports.help = {
name: "randomimag"
}

2 个答案:

答案 0 :(得分:1)

您可以像这样分割字符串:

var content = message.content; //define content as the message
var parts = content.split(" "); //split it apart at the spaces
var userImput = parts[1] // get the 2nd part of the message
//(or whatever the user imputs after the command)

然后,要使图像查找部分正常工作,您可以创建一个带有变量的字符串:

var imagePath = `whatever-your-file-path-here/${userImput}.png`

答案 1 :(得分:0)

如果您期望像这样的命令 !randomimg 3 您可以对字符串进行简单分割

let arr = message.split(" ");
//Splitting message at spaces.
let num = arr[1];
// Num contains 3 now