javascript - 如何在 discord.js 中获得 twitch 流预览?
问题描述
目前我有这个代码来通知我的不和谐成员有人正在流式传输。当我将图像设置为 twitch url 时,由于某种原因它不会加载它......我怎样才能获得正确的 twitch 流预览?
client.on("presenceUpdate", (oldPresence, newPresence) => {
if (!newPresence.activities) return false;
newPresence.activities.forEach(activity => {
if (activity.type == "STREAMING") {
console.log(`${newPresence.user.tag} is streaming at ${activity.url}.`);
const twitchAnnouncementChannel = newPresence.guild.channels.cache.find(ch => ch.id === `789277245617209354`)
const twitchChannel = new Discord.MessageEmbed()
.setColor("#400080")
.setTitle(`${newPresence.user.tag} is now live on twitch`)
.setURL(activity.url)
.setDescription(`**Stream Started**`)
.setImage(activity.url)
.setTimestamp()
.setFooter("Enigma")
twitchAnnouncementChannel.send(`${newPresence.user.tag} IS NOW LIVE ON TWITCH GO CHECK HIM OUT! @everyone`, twitchChannel)
};
});
});
解决方案
在你的情况下,这应该这样做。要获得 Twitch 流的预览,您需要:https://static-cdn.jtvnw.net/previews-ttv/live_user_USERNAME-{width}x{height}.jpg,“activity.url”只是频道 url。
client.on("presenceUpdate", (oldPresence, newPresence) => {
if (!newPresence.activities) return false;
newPresence.activities.forEach(activity => {
if (activity.type == "STREAMING") {
console.log(`${newPresence.user.tag} is streaming at ${activity.url}.`);
const twitchAnnouncementChannel = newPresence.guild.channels.cache.find(ch => ch.id === `789277245617209354`)
const twitchChannel = new Discord.MessageEmbed()
.setColor("#400080")
.setTitle(`${newPresence.user.tag} is now live on twitch`)
.setURL(activity.url)
.setDescription(`**Stream Started**`)
.setImage(`https://static-cdn.jtvnw.net/previews-ttv/live_user_${activity.url.split("twitch.tv/").pop()}-1920x1080.jpg`)
.setTimestamp()
.setFooter("Enigma");
twitchAnnouncementChannel.send(`${newPresence.user.tag} IS NOW LIVE ON TWITCH GO CHECK HIM OUT! @everyone`, twitchChannel)
};
});
});
推荐阅读
- mongodb - 通过条件查找优化 Mongo 聚合查询
- javascript - 如何在d3甜甜圈饼图中实现折线标签?
- javascript - express js 无法访问多个静态页面
- css - 如何添加页脚模板以响应剑道网格
- spring - Elasticsearch 和 Spring 兼容性(2019 年 3 月)
- java - SOLR 7.4.0 异常:java.lang.NoClassDefFoundError:org/apache/solr/handler/dataimport/FieldStreamDataSource
- vpn - 无法通过 OpenVPN 连接到 VPN 服务器
- linux - shmget() 什么时候返回零 shmid?
- javascript - 使用同一类检查多个复选框
- python - 将一串ndarray转换为ndarray