mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-22 18:29:14 +02:00
Stack overflow command
This commit is contained in:
@@ -0,0 +1,63 @@
|
|||||||
|
const { Command } = require('discord.js-commando');
|
||||||
|
const { MessageEmbed } = require('discord.js');
|
||||||
|
const snekfetch = require('snekfetch');
|
||||||
|
const { STACKOVERFLOW_KEY } = process.env;
|
||||||
|
|
||||||
|
module.exports = class StackOverflowCommand extends Command {
|
||||||
|
constructor(client) {
|
||||||
|
super(client, {
|
||||||
|
name: 'stack-overflow',
|
||||||
|
aliases: ['stack-exchange'],
|
||||||
|
group: 'search',
|
||||||
|
memberName: 'stack-overflow',
|
||||||
|
description: 'Searches Stack Overflow for your query.',
|
||||||
|
clientPermissions: ['EMBED_LINKS'],
|
||||||
|
args: [
|
||||||
|
{
|
||||||
|
key: 'query',
|
||||||
|
prompt: 'What question would you like to search for?',
|
||||||
|
type: 'string'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async run(msg, { query }) {
|
||||||
|
try {
|
||||||
|
const { body } = await snekfetch
|
||||||
|
.get('http://api.stackexchange.com/2.2/search/advanced')
|
||||||
|
.query({
|
||||||
|
page: 1,
|
||||||
|
pagesize: 1,
|
||||||
|
order: 'asc',
|
||||||
|
sort: 'relevance',
|
||||||
|
answers: 1,
|
||||||
|
q: query,
|
||||||
|
site: 'stackoverflow',
|
||||||
|
key: STACKOVERFLOW_KEY
|
||||||
|
});
|
||||||
|
if (!body.items.length) return msg.say('Could not find any results.');
|
||||||
|
const data = body.items[0];
|
||||||
|
const embed = new MessageEmbed()
|
||||||
|
.setColor(0xF48023)
|
||||||
|
.setAuthor('Stack Overflow', 'https://i.imgur.com/P2jAgE3.png')
|
||||||
|
.setURL(data.link)
|
||||||
|
.setTitle(data.title)
|
||||||
|
.addField('❯ Asker',
|
||||||
|
`[${data.owner.display_name}](${data.owner.link})`, true)
|
||||||
|
.addField('❯ Views',
|
||||||
|
data.view_count, true)
|
||||||
|
.addField('❯ Score',
|
||||||
|
data.score, true)
|
||||||
|
.addField('❯ Created',
|
||||||
|
new Date(data.creation_date * 1000).toDateString(), true)
|
||||||
|
.addField('❯ Modified',
|
||||||
|
new Date(data.last_edit_date * 1000).toDateString(), true)
|
||||||
|
.addField('❯ Last Activity',
|
||||||
|
new Date(data.last_activity_date * 1000).toDateString(), true);
|
||||||
|
return msg.embed(embed);
|
||||||
|
} catch (err) {
|
||||||
|
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "xiaobot",
|
"name": "xiaobot",
|
||||||
"version": "55.1.3",
|
"version": "55.2.0",
|
||||||
"description": "Your personal server companion.",
|
"description": "Your personal server companion.",
|
||||||
"main": "XiaoBot.js",
|
"main": "XiaoBot.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
Reference in New Issue
Block a user