如何从传入的Webhook字段中提取文本?

时间:2019-03-20 17:00:15

标签: discord.js

是否可以从任何传入的Webhook中提取变体代码,并使bot仅使用该变体代码发送新消息?

Webhook示例: enter link description here

1 个答案:

答案 0 :(得分:0)

我认为您的意思是:如何从网络钩子发送的嵌入内容中提取文本
Webhooks经常发送嵌入内容,只是因为这是发送数据的一种更漂亮的方式。发送YouTube或Twitter链接时看到的东西也是嵌入的。

如果是这种情况,我将假设您所指的嵌入是消息的第一个也是唯一的嵌入。我还将假设message是网络挂钩发送的消息。

let embed = message.embeds[0], // This is the embed we're searching in.
  field, text, number;
if (!embed) return; // If there are no embeds in the message, return.

for (let f of embed.fields) { // Loop through every field in the embed...
  if (f.name == 'Sizes') { // ...until you find the one named 'Sizes'
    field = f; // Then store it into the field variable
    break; // Exit the loop
  }
}
if (!field) return; // If there are no 'Sizes' fields, return.

// This will split the message in two parts to get the 2nd
// It will get the string cantaining the number
text = field.value.split('-')[1].trim();

// If you want the result as a number, you can parse it:
number = parseInt(text);