Skip to content

Instantly share code, notes, and snippets.

@sidedwards
Created July 13, 2021 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sidedwards/731634d073501cfa259176339f339def to your computer and use it in GitHub Desktop.
Save sidedwards/731634d073501cfa259176339f339def to your computer and use it in GitHub Desktop.
Discord Daily Quote

Description

A script that posts a daily quote and image to Discord via webhook.

Setup

  1. Install deno
  2. Create a new Discord webhook, copy URL, and update webhookURL value.
  3. Create a Pexels account, generate an API key, and update pexelsAPIKey value.

Usage

  1. Manually run deno run --allow-net daily-quote.ts
  2. Compile with deno compile --unstable --allow-net daily-quote.ts and set up a cronjob targeting the compiled script.
async function getDailyQuote(config: any) {
const pref_mode = (config.mode) ? config.mode : 'random' // Available modes: [today, random, author (premium only)].
// For premium subscriptions
const pref_author = (config.author) ? config.author : null // Available authors: https://premium.zenquotes.io/available-authors/
const pref_key = (config.key) ? config.key : null // https://premium.zenquotes.io/
const zenQuotesAPI = `https://zenquotes.io/api/`
const getDailyQuoteURL = (pref_mode == 'author' && pref_author && pref_key) ? `${zenQuotesAPI}quotes/${pref_mode}/${pref_author}/${pref_key}` : `${zenQuotesAPI}${pref_mode}`
const response = await fetch(getDailyQuoteURL)
if (response != null) {
const data = await response.json()
return data
} else {
return 'Error in zenquotes.io Daily Quote lookup.'
}
}
async function getDailyImage() {
const query = 'nature'
const pexelsAPI = 'https://api.pexels.com/v1'
const pexelsAPIKey = '<secret>'
const randomPage = Math.floor(Math.random() * (100 - 1) + 1)
const getDailyImageURL = `${pexelsAPI}/search?query=${query}&per_page=80&page=${randomPage}`
const response = await fetch(getDailyImageURL, {
method: 'GET',
headers: {
'Authorization': `Bearer ${pexelsAPIKey}`
}
})
if (response != null) {
const data = await response.json()
return data
} else {
return 'Error in Pexels image lookup.'
}
}
async function main() {
let quoteParams = {
'mode': 'random',
'author': null,
'key': null
}
const dailyQuote = await getDailyQuote(quoteParams)
const dailyImage = await getDailyImage()
const webhookURL = '<secret>'
const colors = [
'16670074',
'16768298',
'8280063',
'4968442',
'714881'
]
return await fetch(webhookURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({embeds: [{
title: dailyQuote[0].a,
image: {
url: dailyImage.photos[Math.floor(Math.random() * (79 - 1) + 1)].src.large
},
author: {
name: '💭 Daily Quote',
},
color: colors[Math.floor(Math.random() * colors.length)],
description: dailyQuote[0].q,
}]}),
})
}
export default main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment