fediembed: polls

This commit is contained in:
Cynthia Foxwell 2023-11-25 12:26:36 -07:00
parent 0006b5eaa4
commit d209d12506

View file

@ -137,6 +137,7 @@ async function processUrl(msg, url, spoiler = false) {
author,
timestamp,
title,
poll,
emotes = [];
// Fetch post
@ -360,6 +361,17 @@ async function processUrl(msg, url, spoiler = false) {
}
}
}
if (postData2.poll) {
poll = {
end: new Date(postData2.poll.expires_at),
total: postData2.poll.votes_count,
options: postData2.poll.options.map((o) => ({
name: o.title,
count: o.votes_count,
})),
};
}
}
}
} else {
@ -453,6 +465,17 @@ async function processUrl(msg, url, spoiler = false) {
avatar: authorData.icon?.url,
};
}
if (postData.endTime && postData.oneOf && postData.votersCount) {
poll = {
end: new Date(postData.endTime),
total: postData.votersCount,
options: postData.oneOf.map((o) => ({
name: o.name,
count: o.replies.totalItems,
})),
};
}
}
// We could just continue without author but it'd look ugly and be confusing.
@ -591,6 +614,24 @@ async function processUrl(msg, url, spoiler = false) {
}
}
if (poll) {
baseEmbed.fields.push({
name: "Poll",
value:
poll.options
.map((o) => {
const percent = o.count / poll.total;
const bar = Math.round(percent * 30);
return `**${o.name}** (${percent.toFixed(2)}%)\n\`[${"=".repeat(
bar
)}${" ".repeat(30 - bar)}]\``;
})
.join("\n\n") +
`\n\nEnds: <t:${Math.floor(poll.end.getTime() / 1000)}:R>`,
});
}
const embeds = [];
if (images.length > 0) {