简体   繁体   English

将匹配图像添加到随机消息 | discord.js

[英]Adding a matching image to a random message | discord.js

I am currently trying to code a Discord bot that will output a recommended anime show by picking a choice from a data table or string.我目前正在尝试编写一个 Discord 机器人,它将 output 通过从数据表或字符串中进行选择来推荐动漫节目。 I already did that part, but I cannot figure out how to add an image that matches the show.我已经完成了那部分,但我不知道如何添加与节目匹配的图像。 Here is the code I currently have.这是我目前拥有的代码。 (for one genre) (针对一种类型)

bot.on('message', message => {

let args = message.content.substring(PREFIX.length).split(" ");

switch (args[0]) {

    case 'Action':


        let actions = ['show1',
            'show2',
            'show3',
            'show4',
            'show5',
            'show6',
            'show7',
            'show8',
        ];
       
        let pick1 = actions[Math.floor(Math.random() * actions.length)];
        const action = new Discord.MessageEmbed()
            .setTitle('Reccommended Action Anime:')
            .setDescription(actions[Math.floor(Math.random() * actions.length)])
        message.author.send(action);

        break;

One simple solution would be to use an object for each show, which has a title and an image (some url to a picture in some image cloud service like IMGur or else)一个简单的解决方案是为每个节目使用 object,它有一个标题和一个图像(一些 url 到一些图像云服务中的图片,如 IMGur 或其他)

    let actions = [
        {'name': 'show1', 'image': 'https://i.imgur.com/ktEA3WS.png'},
        {'name': 'show2', 'image': 'https://i.imgur.com/zcbnc6g.png'},
        ...
    ]

You can set the random like this:您可以像这样设置随机数:

const randomShow = actions[Math.floor(Math.random() * actions.length)]

And then you just set the description and the image using the object properties.然后您只需使用 object 属性设置描述和图像。

.setDescription(randomShow.name)  // I don't know how to set images in discord but should be similar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM