Skip to content

Instantly share code, notes, and snippets.

@apos37
Created August 12, 2022 21:15
Show Gist options
  • Save apos37/bbb1bb851c5c80735e8b9f543ce316bd to your computer and use it in GitHub Desktop.
Save apos37/bbb1bb851c5c80735e8b9f543ce316bd to your computer and use it in GitHub Desktop.
Pylon Bot Command for Sharing WP.org Plugin from Repository
import { commands } from './command_group';
import { SUGGESTED_PLUGINS_CHANNEL_ID } from './ids';
import { numberWithCommas } from './utility';
/**
* KV Namespace
*/
const pluginKV = new pylon.KVNamespace('plugins');
/**
* Command for listing all items in the KV Namespace
*/
commands.raw('plugins', async (message) => {
const keys = await pluginKV.list();
console.log(keys);
if (keys.includes('wp-crontrol')) {
console.log('yes');
}
await message.reply(`There are ${keys.length} plugins:\n${keys.join('\n')}`);
// Delete all except this one
// var omit = '';
// for (let i = 0; i < keys.length; i++) {
// if (keys[i] != omit) {
// pluginKV.delete(keys[i]);
// }
// }
});
/**
* Command for adding a plugin from the repository
*/
commands.on(
{
name: 'plugin',
description: 'Add a plugin from the WP repository',
},
(args) => ({
slug: args.text(),
}),
async (message, { slug }) => {
createPluginPost(message, slug);
message.delete();
}
);
// Create the plugin post
async function createPluginPost(message, pluginSlug: string) {
// Fetch WordPress Plugin
const _url =
'https://api.wordpress.org/plugins/info/1.2/?action=plugin_information&request[slug]=' +
pluginSlug +
'&request[fields]=short_description,icons,-screenshots,-donate_link,-ratings,-sections,-rating';
await fetch(_url, { headers: { 'user-agent': 'blah' } })
// await fetch(_url)
.then(function (response) {
return response.json();
})
.then(async function (data) {
console.log(data);
// Set the name
var name = data.name;
// Author
var author = data.author;
author = author.split('>')[1];
author = author.split('<')[0];
// Short Description
var desc = data.short_description;
desc = desc
.replaceAll('&amp;', '&')
.replaceAll('&lt;', '<')
.replaceAll('&gt;', '>')
.replaceAll('&quot;', '"')
.replaceAll('&apos;', "'")
.replaceAll('&#039;', "'");
// Date
var date = data.last_updated;
date = date.split(' ')[0];
var unix = Math.floor(Date.parse(date) / 1000);
var last_updated = `<t:${unix}:R> <t:${unix}:F>`;
// Link
const link = 'https://wordpress.org/plugins/' + pluginSlug + '/';
// Images
const banner = data.banners.low;
var icon = data.icons['2x'];
icon = icon.split('?')[0];
var test = await fetch(icon);
console.log(test);
// Contributors
var getContributors = data.contributors;
var contributors = [];
for (const [key, value] of Object.entries(getContributors)) {
contributors.push(key);
}
// Return detailed embed in same channel, unless we are posting directly in plugins channel
if (message.channelId !== SUGGESTED_PLUGINS_CHANNEL_ID) {
var embed = new discord.Embed({
title: name,
url: link,
description: `_By ${author}_\n\n${desc}
\n\n**Current Version:** ${data.version}
**Active Installs:** ${numberWithCommas(data.active_installs)}
**Requires PHP:** ${data.requires_php}
**Tested with:** ${data.tested}
**Last Updated:** ${last_updated}
**Contributors:** ${contributors.join(', ')}`,
thumbnail: {
url: icon,
},
footer: {
text: link,
},
}).setImage({ url: banner });
// Return the full message
await message.reply(embed);
}
// Now add to the plugins channel if it hasn't already been added
const plugins = await pluginKV.list();
if (plugins.includes(pluginSlug)) {
console.log(pluginSlug + ' has already been added.');
return;
} else {
const channel = await discord.getGuildTextChannel(
SUGGESTED_PLUGINS_CHANNEL_ID
);
if (!channel) {
return;
}
// Short version
var partialEmbed = new discord.Embed({
title: name,
url: link,
description: `_By ${author}_\n\n${desc}`,
thumbnail: {
url: icon,
},
footer: {
text: link,
},
}).setImage({ url: banner });
// Send the short message
channel.sendMessage({ embed: partialEmbed });
// Add to the KV
return await pluginKV.put(pluginSlug, 1);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment