【问题标题】:Adding a matching image to a random message | discord.js将匹配图像添加到随机消息 |不和谐.js
【发布时间】:2020-07-15 23:29:41
【问题描述】:

我目前正在尝试编写一个 Discord 机器人,该机器人将通过从数据表或字符串中选择一个选项来输出推荐的动漫节目。我已经完成了那部分,但我不知道如何添加与节目匹配的图像。这是我目前拥有的代码。 (针对一种类型)

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;

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    一个简单的解决方案是为每个节目使用一个对象,该对象有一个标题和一个图像(一些图像云服务中的图片的 URL,如 IMGur 或其他)

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

    你可以这样设置随机数:

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

    然后您只需使用对象属性设置描述和图像。

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

    【讨论】:

      猜你喜欢
      • 2020-11-18
      • 1970-01-01
      • 2020-04-12
      • 1970-01-01
      • 2020-05-07
      • 2021-08-14
      • 2013-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多