This commit is contained in:
Daniel Odendahl Jr
2017-12-06 21:09:45 +00:00
parent 5f54fb8bc9
commit ce801860e6
+77
View File
@@ -0,0 +1,77 @@
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 } = 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(' ID',
body.id, true)
.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(' Created',
new Date(body.creation_date).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 = new Buffer(`${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;
}
};