Grab an event from this day in history (Wikipedia's page) using the Wikipedia API and send it in a Discord channel
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN}); // Import standard lib
const wikipediaAPIToken = process.env.WIKIPEDIA_CLIENT_ID; // Your Wikipedia API key
const channel_id = process.env.CHANNEL_ID; // This is the channel you wish for the message to be sent in
const dailyRole = process.env.ROLE_ID; // This is the role you want pinged each day
try {
// Get information about this day in history from English Wikipedia
let today = new Date();
let month = String(today.getMonth() + 1).padStart(2,'0');
let day = String(today.getDate()).padStart(2,'0');
// Make the API request
let result = await lib.http.request['@1.1.7'].get({
url: `https://api.wikimedia.org/feed/v1/wikipedia/en/onthisday/all/${month}/${day}`,
headers: {
'authorization': wikipediaAPIToken
}
});
// Check if there is at least one event
if (result.data.events && result.data.events.length > 0) {
// Randomly select an event
let randomIndex = Math.floor(Math.random() * result.data.events.length);
let event = result.data.events[randomIndex];
// Extract the necessary fields from the event object
let title = event.text || 'Error grabbing title';
let year = event.year || 'Error grabbing year';
let description = '';
if (event.pages && event.pages.length > 0) {
description = event.pages[0].extract || 'Error grabbing description';
}
let imageURL = event.pages && event.pages.length > 0 && event.pages[0].thumbnail ? event.pages[0].thumbnail.source : '';
await lib.discord.channels['@0.3.4'].messages.create({
channel_id: channel_id, content: `<@&${dailyRole}>`,
embeds: [
{
type: 'rich',
author: {name: `NAME`, icon_url: `https://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png`}, // Name it what you want
title: `${title} (${year})`,
description: description,
color: 0xFFFFFF, // This is white
image: {
url: imageURL,
},
footer: {
text: `Grabbed from Wikipedia`,
icon_url: `https://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png`,
},
timestamp: today.toISOString(),
}
]
});
}
} catch (error) {
console.error(error);
}
/*
Some extra things:
This can also be turned into a command, just change the channel_id to context.params.event.channel_id and set up a
command in Autocode's slash command builder.
You can change how it shows the information, for example; you could remove the embed or grab more than one event.
*/