Files
xiao/commands/search/twitter.js
T
2017-12-12 04:06:26 +00:00

76 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const { Command } = require('discord.js-commando');
const { MessageEmbed } = require('discord.js');
const snekfetch = require('snekfetch');
const { TWITTER_KEY, TWITTER_SECRET } = process.env;
module.exports = class TwitterCommand extends Command {
constructor(client) {
super(client, {
name: 'twitter',
aliases: ['twitter-user'],
group: 'search',
memberName: 'twitter',
description: 'Responds with information on a Twitter user.',
clientPermissions: ['EMBED_LINKS'],
args: [
{
key: 'user',
prompt: 'What user would you like to get information on?',
type: 'string'
}
]
});
this.token = null;
}
async run(msg, { user }) {
try {
if (!this.token) await this.fetchToken();
const { body } = await snekfetch
.get('https://api.twitter.com/1.1/users/show.json')
.set({ Authorization: `Bearer ${this.token}` })
.query({ screen_name: user });
const embed = new MessageEmbed()
.setColor(0x55ADEE)
.setAuthor('Twitter', 'https://i.imgur.com/QnfcO7y.png')
.setThumbnail(body.profile_image_url_https)
.setURL(`https://twitter.com/${body.screen_name}`)
.setTitle(`${body.name} (@${body.screen_name})`)
.setDescription(body.description)
.addField(' Tweets',
body.statuses_count, true)
.addField(' Followers',
body.followers_count, true)
.addField(' Following',
body.friends_count, true)
.addField(' Protected?',
body.protected ? 'Yes' : 'No', true)
.addField(' Verified?',
body.verified ? 'Yes' : 'No', true)
.addField(' Creation Date',
new Date(body.created_at).toDateString(), true)
.addField(' Latest Tweet',
body.status ? body.status.text : 'None');
return msg.embed(embed);
} catch (err) {
if (err.status === 401) await this.fetchToken();
if (err.status === 404) return msg.say('Could not find any results.');
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
async fetchToken() {
const auth = Buffer.from(`${TWITTER_KEY}:${TWITTER_SECRET}`).toString('base64');
const { body } = await snekfetch
.post('https://api.twitter.com/oauth2/token')
.set({
Authorization: `Basic ${auth}`,
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
})
.send('grant_type=client_credentials');
this.token = body.access_token;
return body;
}
};