最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get Information of an URL with discord.js - Stack Overflow

programmeradmin0浏览0评论

I am currently working on a discord bot, and I want a channel in my server, where people can send link in, but I don't want to get them embeded automatically by the server, I want to pull from that URL link all the informations I need to create myself (the bot will create) the embed, the footer will say e.g. who suggested this link, I want the thumbnail to be matching the url's thumbnail (if it is pointing to a youtube vid e.g.)

The requirement is that I have a string with this URL, can someone send me maybe a library where I can find the stuff, because every url is different in its showing object (webpage, youtube vid, picture).

Also: is there a way to find out if a message is even containing a URL, or is only a URL. I would like to filter my channel, so that only URL can be postet, and other messages will be deleted(I have a delete function)

Thanks in advance!

I am currently working on a discord bot, and I want a channel in my server, where people can send link in, but I don't want to get them embeded automatically by the server, I want to pull from that URL link all the informations I need to create myself (the bot will create) the embed, the footer will say e.g. who suggested this link, I want the thumbnail to be matching the url's thumbnail (if it is pointing to a youtube vid e.g.)

The requirement is that I have a string with this URL, can someone send me maybe a library where I can find the stuff, because every url is different in its showing object (webpage, youtube vid, picture).

Also: is there a way to find out if a message is even containing a URL, or is only a URL. I would like to filter my channel, so that only URL can be postet, and other messages will be deleted(I have a delete function)

Thanks in advance!

Share Improve this question asked Jul 28, 2020 at 19:28 pragmatrickpragmatrick 6452 gold badges11 silver badges25 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

You can try get some info from the page using the Open Graph metadata or Twitter Cards, I think Discord use this to preview some links.

You have to disable "Embed Links" permissions to prevent Discord Embed appears and then send your embed.

You can get metadata from the page using node-fetch and cheerio

Example:

fetch(url).then(res => res.text())
.then(html => {
    const $ = cheerio.load(html)
    console.log($("meta[property='og:title']")[0].attribs.content) // Returns OG Title from the page

Example with url from this question here:

const fetch = require("node-fetch")
const cheerio = require("cheerio")

fetch("https://stackoverflow./questions/63141471/how-to-get-information-of-an-url-with-discord-js").then(res => res.text())
.then(html => {
    const $ = cheerio.load(html)
    const title = $("meta[property='og:title']")[0] || $("meta[name='twitter:title']")
    const description = $("meta[property='og:description']")[0] || $("meta[name='twitter:description']")[0]
    const image = $("meta[property='og:image']")[0] || $("meta[name='twitter:image']")[0]
    const nothing = $("meta[property='og:thisdoesntexists']")[0] // Try get something that doesn't exists

    console.log(title ? title.attribs.content : "no title") // How to get Information of an URL with discord.js
    console.log(description ? description.attribs.content : "no description") // Short description
    console.log(image ? image.attribs.content : "no image") // https://cdn.sstatic/Sites/stackoverflow/Img/[email protected]?v=73d79a89bded
    console.log(nothing ? nothing.attribs.content : "Literally, nothing") // Literally, nothing
})
发布评论

评论列表(0)

  1. 暂无评论