Add wiki and weather commands

This commit is contained in:
2026-01-26 14:10:46 +01:00
parent a86c90f8ce
commit ef7ca16a85
18 changed files with 266 additions and 48 deletions
+54
View File
@@ -0,0 +1,54 @@
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const MAX_DESCRIPTION = 4096;
const truncate = (text, maxLength) => {
if (!text) return 'No summary available.';
if (text.length <= maxLength) return text;
return `${text.slice(0, maxLength - 3)}...`;
};
module.exports = {
data: new SlashCommandBuilder()
.setName('wiki')
.setDescription('Search Wikipedia for a topic.')
.addStringOption(option =>
option
.setName('topic')
.setDescription('Topic to search')
.setRequired(true)),
category: 'Utility',
async execute(interaction) {
const topic = interaction.options.getString('topic');
const endpoint = `https://en.wikipedia.org/api/rest_v1/page/summary/${encodeURIComponent(topic)}`;
await interaction.deferReply();
const response = await fetch(endpoint, {
headers: {
'User-Agent': 'DiscordBot/1.0 (https://github.com)',
},
});
if (!response.ok) {
return interaction.editReply({
content: 'I could not find a Wikipedia summary for that topic.',
});
}
const data = await response.json();
const embed = new EmbedBuilder()
.setColor(0x8e44ad)
.setTitle(data.title ?? topic)
.setURL(data.content_urls?.desktop?.page ?? `https://en.wikipedia.org/wiki/${encodeURIComponent(topic)}`)
.setDescription(truncate(data.extract, MAX_DESCRIPTION))
.setFooter({ text: 'Source: Wikipedia' });
if (data.thumbnail?.source) {
embed.setThumbnail(data.thumbnail.source);
}
return interaction.editReply({ embeds: [embed] });
},
};