【问题标题】:discord.js - validate embed?discord.js - 验证嵌入?
【发布时间】:2020-11-19 16:23:00
【问题描述】:

我想在发送之前验证不和谐嵌入。 这意味着,我想知道嵌入在实际发送到频道之前是否有任何问题。

var embed = {
  title: 'some title',
  description: 'some description',
  color: 'foo bar'
}

// embed.color is not a valid color, so it will trigger an error when I send it.
// Here I want some way to know that there's something wrong with the embed object.

message.channel.send({embed});

【问题讨论】:

标签: discord bots discord.js


【解决方案1】:

我已经创建了一个验证嵌入的函数。但是,它并不完整,可能有误报或漏报,但应该可以满足您的需求。

function validURL(url) {
  try {
    new URL(url);
  } catch (err) {
    return false;
  }

  return true;
}

function validEmbed(embed) {
  return Object.entries({
    title: (title) => typeof title === 'string',
    description: (description) => typeof description === 'string',
    url: (url) => validURL(url),
    timestamp: (timestamp) => (new Date(timestamp)).getTime() > 0,
    color: (color) => typeof color === 'number',
    fields: (fields) => fields.every(field => typeof field.name === 'string' && typeof field.value === 'string' && typeof field.inline === 'boolean'),
    thumbnail: (thumbnail) => validURL(thumbnail.url),
    image: (image) => validURL(image.url),
    author: (author) => typeof author.name === 'string' && validURL(author.url) && validURL(author.icon_url),
    footer: (footer) => typeof footer.text === 'string' && validURL(footer.icon_url)
  }).every(field => {
    if (!(field[0] in embed)) return true;

    return embed[field[0]] && field[1](embed[field[0]]);
  });
}

这是一个示例 sn-p:

function validURL(e){try{new URL(e)}catch(e){return!1}return!0}function validEmbed(e){return Object.entries({title:e=>"string"==typeof e,description:e=>"string"==typeof e,url:e=>validURL(e),timestamp:e=>new Date(e).getTime()>0,color:e=>"number"==typeof e,fields:e=>e.every(e=>"string"==typeof e.name&&"string"==typeof e.value&&"boolean"==typeof e.inline),thumbnail:e=>validURL(e.url),image:e=>validURL(e.url),author:e=>"string"==typeof e.name&&validURL(e.url)&&validURL(e.icon_url),footer:e=>"string"==typeof e.text&&validURL(e.icon_url)}).every(t=>!(t[0]in e)||e[t[0]]&&t[1](e[t[0]]))}



/* returns true */
console.log(validEmbed({
  title: 'The title'
}));



/* returns true */
console.log(validEmbed({
  title: 'The title',
  description: 'Sample Description.',
  url: 'https://i.imgur.com/ttHwmCl.jpg',
  timestamp: '2020-07-30T14:44:05.876Z',
  color: 3447003,
  fields: [{
      name: 'name',
      value: 'value',
      inline: false
    },
    {
      name: 'name2',
      value: 'value2',
      inline: true
    }
  ],
  thumbnail: {
    url: 'attachment://stackoverflow2.jpg'
  },
  image: {
    url: 'attachment://stackoverflow.jpg'
  },
  author: {
    name: 'Daemon Beast',
    url: 'https://i.imgur.com/P4L260P.jpg',
    icon_url: 'https://i.imgur.com/P4L260P.jpg'
  },
  footer: {
    text: 'footer text',
    icon_url: 'https://i.imgur.com/ttHwmCl.jpg'
  }
}));



/* returns false */
console.log(validEmbed({
  author: 4567876567
}));

【讨论】:

    猜你喜欢
    • 2021-02-07
    • 2020-10-28
    • 1970-01-01
    • 2020-12-05
    • 2021-07-23
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    相关资源
    最近更新 更多