Fix tweet

This commit is contained in:
Dragon Fire
2024-03-22 14:36:09 -04:00
parent 1b3c1c8909
commit 182bf8c72f
9 changed files with 12 additions and 362 deletions
-56
View File
@@ -1,56 +0,0 @@
const Command = require('../../framework/Command');
const request = require('node-superfetch');
const { Agent } = require('https');
const { list } = require('../../util/Util');
const fonts = require('../../assets/json/cool-text');
const noRejectAgent = new Agent({ rejectUnauthorized: false });
module.exports = class CoolTextCommand extends Command {
constructor(client) {
super(client, {
name: 'cool-text',
group: 'edit-image-text',
memberName: 'cool-text',
description: 'Writes text in some cool fonts.',
details: `**Fonts:** ${Object.keys(fonts).join(', ')}`,
credit: [
{
name: 'Cool Text Graphics Generator',
url: 'https://cooltext.com/',
reason: 'API'
}
],
args: [
{
key: 'font',
prompt: `What font do you want to use? Either ${list(Object.keys(fonts), 'or')}.`,
type: 'string',
oneOf: Object.keys(fonts),
parse: font => font.toLowerCase()
},
{
key: 'text',
prompt: 'What text do you want to write?',
type: 'string'
}
]
});
}
async run(msg, { font, text }) {
try {
const { body, text: content } = await request
.post('https://cooltext.com/PostChange')
.attach({
...fonts[font],
Text: text
});
if (!content) return msg.say('Failed to create an image with this text.');
const { body: imageBody } = await request.get(body.renderLocation, { agent: noRejectAgent });
const format = body.isAnimated ? 'gif' : 'png';
return msg.say({ files: [{ attachment: imageBody, name: `${font}.${format}` }] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
};
-49
View File
@@ -1,49 +0,0 @@
const Command = require('../../framework/Command');
const request = require('node-superfetch');
const moment = require('moment');
module.exports = class NewspaperCommand extends Command {
constructor(client) {
super(client, {
name: 'newspaper',
group: 'edit-image-text',
memberName: 'newspaper',
description: 'Creates a fake newspaper with the headline and body of your choice.',
credit: [
{
name: 'The Newspaper Clipping Generator',
url: 'https://www.fodey.com/generators/newspaper/snippet.asp',
reason: 'API'
}
],
args: [
{
key: 'headline',
prompt: 'What do you want the headline to be?',
type: 'string',
max: 20
},
{
key: 'body',
prompt: 'What should the body of the article be?',
type: 'string'
}
]
});
}
async run(msg, { headline, body }) {
try {
const { text } = await request
.post('https://www.fodey.com/generators/newspaper/snippet.asp')
.attach('name', 'The Daily Whatever')
.attach('date', moment().format('dddd, MMMM D, YYYY'))
.attach('headline', headline)
.attach('text', body);
const newspaperURL = text.match(/<img src="(https:\/\/r[0-9]+\.fodey\.com\/[0-9]+\/.+\.jpg)"/i)[1];
return msg.say({ files: [newspaperURL] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
};
@@ -1,52 +0,0 @@
const Command = require('../../framework/Command');
const request = require('node-superfetch');
module.exports = class ShieldsIoBadgeCommand extends Command {
constructor(client) {
super(client, {
name: 'shields-io-badge',
aliases: ['shields-io'],
group: 'edit-image-text',
memberName: 'shields-io-badge',
description: 'Creates a badge from shields.io.',
clientPermissions: ['ATTACH_FILES'],
credit: [
{
name: 'Shields.io',
url: 'https://shields.io/',
reason: 'API'
}
],
args: [
{
key: 'subject',
prompt: 'What is the subject of the badge?',
type: 'string',
parse: subject => encodeURIComponent(subject.replaceAll('-', '--').replaceAll('_', '__'))
},
{
key: 'status',
prompt: 'What is the status of the badge?',
type: 'string',
parse: status => encodeURIComponent(status.replaceAll('-', '--').replaceAll('_', '__'))
},
{
key: 'color',
prompt: 'What is the color of the badge?',
type: 'string',
default: 'brightgreen'
}
]
});
}
async run(msg, { subject, status, color }) {
try {
const { body } = await request.get(`https://img.shields.io/badge/${subject}-${status}-${color}.png`);
return msg.say({ files: [{ attachment: body, name: 'badge.png' }] });
} catch (err) {
if (err.status === 404) return msg.reply('Could not create the badge...');
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
};
+11 -33
View File
@@ -1,11 +1,12 @@
const Command = require('../../framework/Command');
const { createCanvas, loadImage } = require('canvas');
const { TwitterOpenApi } = require('twitter-openapi-typescript');
const api = new TwitterOpenApi();
const moment = require('moment');
const request = require('node-superfetch');
const path = require('path');
const { base64, formatNumberK, randomRange } = require('../../util/Util');
const { formatNumberK, randomRange } = require('../../util/Util');
const { wrapText } = require('../../util/Canvas');
const { TWITTER_KEY, TWITTER_SECRET } = process.env;
module.exports = class TweetCommand extends Command {
constructor(client) {
@@ -50,12 +51,12 @@ module.exports = class TweetCommand extends Command {
]
});
this.token = null;
this.guestClient = null;
}
async run(msg, { user, text }) {
try {
if (!this.token) await this.fetchToken();
if (!this.guestClient) this.guestClient = await api.getGuestClient();
const userData = await this.fetchUser(msg, user);
const avatar = await loadImage(userData.avatar);
const base1 = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'tweet', 'bg-1.png'));
@@ -148,40 +149,17 @@ module.exports = class TweetCommand extends Command {
}
}
async fetchToken() {
const { body } = await request
.post('https://api.twitter.com/oauth2/token')
.set({
Authorization: `Basic ${base64(`${TWITTER_KEY}:${TWITTER_SECRET}`)}`,
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
})
.send('grant_type=client_credentials');
this.token = body.access_token;
return body;
}
async fetchUser(msg, user) {
if (user.toLowerCase() === 'realdonaldtrump') {
return {
screenName: 'realDonaldTrump',
name: 'Donald J. Trump',
avatar: path.join(__dirname, '..', '..', 'assets', 'images', 'tweet', 'realdonaldtrump.jpg'),
verified: true,
followers: 88776124
};
}
try {
const { body } = await request
.get('https://api.twitter.com/1.1/users/show.json')
.set({ Authorization: `Bearer ${this.token}` })
.query({ screen_name: user });
const avatarRes = await request.get(body.profile_image_url_https.replace('_normal', '_bigger'));
const { data } = await this.guestClient.getUserApi().getUserByScreenName({ screenName: user });
const body = data.user.legacy;
const avatarRes = await request.get(body.profileImageUrlHttps);
return {
screenName: body.screen_name,
screenName: body.screenName,
name: body.name,
avatar: avatarRes.body,
verified: body.verified,
followers: body.followers_count
verified: data.user.isBlueVerified,
followers: body.followersCount
};
} catch {
const avatarRes = await request.get(msg.author.displayAvatarURL({ format: 'png', size: 64 }));