如何从传入的 webhook 字段中提取文本?
How I can extract text from a incoming webhook field?
是否可以从任何传入的 webhook 中提取变体代码并让机器人发送仅包含该变体代码的新消息?
网络钩子示例:
我认为您的意思是:如何从 webhook 发送的嵌入中提取文本?
Webhooks 经常发送嵌入,只是因为它是一种更漂亮的数据发送方式。当您发送 YouTube 或 Twitter link 时看到的内容也是一个嵌入。
如果是这种情况,我将假定您所指的嵌入是消息的第一个也是唯一一个嵌入。我还假设 message
是 webhook 发送的消息。
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);
是否可以从任何传入的 webhook 中提取变体代码并让机器人发送仅包含该变体代码的新消息?
网络钩子示例:
我认为您的意思是:如何从 webhook 发送的嵌入中提取文本?
Webhooks 经常发送嵌入,只是因为它是一种更漂亮的数据发送方式。当您发送 YouTube 或 Twitter link 时看到的内容也是一个嵌入。
如果是这种情况,我将假定您所指的嵌入是消息的第一个也是唯一一个嵌入。我还假设 message
是 webhook 发送的消息。
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);