Created
July 30, 2022 19:15
-
-
Save WiLuX-Source/850f40a63ff6ac5897773a33b343b5ec to your computer and use it in GitHub Desktop.
Pylon Reddit Feed Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @ts-nocheck | |
// Don't forget to give me a star to support! | |
// Don't forget to change the permissions for slash-commands through your server! | |
// Don't forget to give credit if you decide to make something from this! | |
// Don't forget to check about command to learn how to use it! | |
// Don't add more than 2 subreddits! | |
// Disclaimer: None of the devs take responsibility. | |
// Code By WiLuX-Source | |
// Embed Styling & Sub-Command System By PhantomNimbi | |
const reddit_instance = { | |
embed: { | |
footer_text: 'Powered By Pylon', | |
footer_icon: 'https://i.imgur.com/2QOYggo.png', | |
thumbnail: | |
'https://raw.githubusercontent.com/Socket-Development/Pylon-Bot/main/.github/assets/reddit.png', | |
color: 7441133, | |
}, | |
commands: { | |
staff_commands: ['about', 'list'], | |
info_commands: [ | |
'test', | |
'add [subreddit]', | |
'remove [subreddit]', | |
'set [channel]', | |
], | |
group: discord.interactions.commands, | |
}, | |
storages: { | |
channel: new pylon.KVNamespace('channels'), | |
subreddit: new pylon.KVNamespace('subreddits'), | |
}, | |
interval: '0 0/5 * * * * *', | |
channel: null, | |
subreddits: [], | |
firsttime: true, | |
firsttime2: true, | |
permission: discord.Permissions.MANAGE_CHANNELS, | |
function: { | |
async defaultchannel() { | |
reddit_instance.channel = await reddit_instance.storages.channel.get( | |
'reddit' | |
); | |
if (reddit_instance.channel == undefined) | |
throw new Error('Please configure your channel.'); | |
reddit_instance.firsttime2 = false; | |
}, | |
async checkSubreddits() { | |
reddit_instance.subreddits = | |
await reddit_instance.storages.subreddit.list(); | |
reddit_instance.firsttime = false; | |
}, | |
async createSubreddit(name) { | |
let array = name.split(' '); | |
for (let i of array) { | |
await reddit_instance.storages.subreddit.put(i, []); | |
} | |
reddit_instance.function.checkSubreddits(); | |
}, | |
async deleteSubreddit(name) { | |
let array = name.split(' '); | |
for (let i of array) { | |
await reddit_instance.storages.subreddit.delete(i); | |
} | |
reddit_instance.function.checkSubreddits(); | |
}, | |
async checkReddit() { | |
let lists = {}; | |
if (reddit_instance.firsttime2) | |
await reddit_instance.function.defaultchannel(); | |
if (reddit_instance.firsttime) | |
await reddit_instance.function.checkSubreddits(); | |
for (let i in reddit_instance.subreddits) { | |
lists[reddit_instance.subreddits[i]] = | |
await reddit_instance.storages.subreddit.get( | |
reddit_instance.subreddits[i] | |
); | |
} | |
for (let i of reddit_instance.subreddits) { | |
await fetch(`https://www.reddit.com/r/${i}/new.json?limit=10`) | |
.then((res) => res.json()) | |
.then((res) => | |
res.data.children | |
.map((x) => x.data) | |
.filter((x) => !lists[i].includes(x.id)) | |
) | |
.then((posts) => | |
reddit_instance.function | |
.Iterator(posts) | |
.then(reddit_instance.function.AddToKVS(i, posts, lists[i])) | |
); | |
} | |
}, | |
async AddToKVS(key, postsw, list) { | |
let lista = list; | |
for (let i of postsw) { | |
lista.push(i.id); | |
} | |
await reddit_instance.storages.subreddit.put(key, lista); | |
}, | |
async Iterator(posts: any) { | |
for (let i of posts) { | |
await reddit_instance.function.sendDiscord(i); | |
} | |
}, | |
async sendDiscord(a: any) { | |
let channel = await discord.getTextChannel(reddit_instance.channel); | |
let embed = new discord.Embed(); | |
embed.setTitle(a.title); | |
embed.setUrl(a.url); | |
embed.setAuthor({ | |
name: `New post on ${a.subreddit}`, | |
url: `https://reddit.com${a.permalink}`, | |
}); | |
embed.setThumbnail({ url: reddit_instance.embed.thumbnail }); | |
embed.setDescription(a.selftext.substring(0, 2030)); | |
embed.setImage({ | |
url: `${ | |
a.preview != null | |
? a.preview.images[0].source.url.replace(/&/g, '&') | |
: '' | |
}`, | |
}); | |
embed.setColor(reddit_instance.embed.color); | |
embed.setFields([ | |
{ name: 'Author', value: a.author, inline: true }, | |
{ name: 'Domain', value: a.domain, inline: true }, | |
]); | |
embed.setFooter({ | |
text: reddit_instance.embed.footer_text, | |
iconUrl: reddit_instance.embed.footer_icon, | |
}); | |
embed.setTimestamp(new Date(a.created * 1000).toISOString()); | |
await channel?.sendMessage(embed); | |
}, | |
}, | |
}; | |
pylon.tasks.cron('checkreddit', reddit_instance.interval, async () => { | |
reddit_instance.function.checkReddit(); | |
}); | |
pylon.tasks.cron('clearlist', '0 0 12 * * * *', async () => { | |
let lists = {}; | |
for (let i in reddit_instance.subreddits) { | |
lists[reddit_instance.subreddits[i]] = | |
await reddit_instance.storages.subreddit.get( | |
reddit_instance.subreddits[i] | |
); | |
if (lists[reddit_instance.subreddits[i]].length >= 100) { | |
let newlist = lists[reddit_instance.subreddits[i]].slice(-20); | |
await reddit_instance.storages.subreddit.put( | |
reddit_instance.subreddits[i], | |
newlist | |
); | |
} | |
} | |
}); | |
reddit_instance.commands.group.register( | |
{ | |
name: 'reddit', | |
description: 'Pylon Reddit Feed', | |
options: (opts) => ({ | |
command: opts.string({ | |
name: 'command', | |
description: 'Command to run', | |
required: true, | |
choices: ['about', 'list', 'test', 'add', 'delete', 'set'], | |
}), | |
subreddit: opts.string({ | |
name: 'subreddit', | |
description: 'Add or delete subreddit', | |
required: false, | |
}), | |
channel: opts.guildChannel({ | |
name: 'channel', | |
description: 'Set channel for feed.', | |
required: false, | |
}), | |
}), | |
ackBehavior: reddit_instance.commands.group.AckBehavior.AUTO_EPHEMERAL, | |
}, | |
async (interaction, { command, subreddit, channel }) => { | |
let staff_list = reddit_instance.commands.staff_commands | |
.toString() | |
.replaceAll(',', '\n - '); | |
let info_list = reddit_instance.commands.info_commands | |
.toString() | |
.replaceAll(',', '\n - '); | |
try { | |
if (command == 'about') { | |
let embed = new discord.Embed(); | |
embed.setTitle('Pylon Reddit Feed'); | |
embed.setDescription( | |
[ | |
'Reddit commands for Pylon.', | |
'', | |
'To use the reddit commands type: `/reddit [command]`', | |
'Make sure to replace the `[command]` flag with the specified command.', | |
'', | |
'***Made by [WiLuX](https://github.com/WiLuX-Source) & [PhantomNimbi](https://github.com/PhantomNimbi)***', | |
].join('\n') | |
); | |
embed.setFields([ | |
{ | |
name: 'Staff Commands', | |
value: '```\n - ' + staff_list + '\n```', | |
inline: false, | |
}, | |
{ | |
name: 'Info Commands', | |
value: '```\n - ' + info_list + '\n```', | |
inline: false, | |
}, | |
]); | |
embed.setThumbnail({ url: reddit_instance.embed.thumbnail }); | |
embed.setTimestamp(new Date().toISOString()); | |
embed.setColor(reddit_instance.embed.color); | |
embed.setFooter({ | |
text: reddit_instance.embed.footer_text, | |
iconUrl: reddit_instance.embed.footer_icon, | |
}); | |
await interaction.respond({ embeds: [embed] }).catch((_) => { | |
interaction.respondEphemeral( | |
':warning: **__Error Triggered__** :warning:' | |
); | |
}); | |
} else if (command == 'test') { | |
if (!interaction.member.can(reddit_instance.permission)) | |
return await interaction.respondEphemeral( | |
':x: `Permissions Error` You need the permission **Manage Channels** to use this command.' | |
); | |
reddit_instance.function.checkReddit().catch((err) => { | |
throw new Error(err); | |
}); | |
await interaction.respondEphemeral('Running Test!').catch((_) => { | |
interaction.respondEphemeral( | |
':warning: **__Error Triggered__** :warning:' | |
); | |
}); | |
} else if (command == 'list') { | |
if (reddit_instance.firsttime2) | |
await reddit_instance.function.checkSubreddits(); | |
let list = reddit_instance.subreddits | |
.toString() | |
.replaceAll(',', '\n - '); | |
let res = '```\n - ' + list + '\n```'; | |
let embed = new discord.Embed(); | |
embed.setTitle('Reddit Feeds'); | |
embed.setDescription( | |
['Here is a list of all currently subscribed subreddits', ''].join( | |
'\n' | |
) | |
); | |
embed.setFields([{ name: 'Subreddits', value: res, inline: false }]); | |
embed.setThumbnail({ url: reddit_instance.embed.thumbnail }); | |
embed.setTimestamp(new Date().toISOString()); | |
embed.setColor(reddit_instance.embed.color); | |
embed.setFooter({ | |
text: reddit_instance.embed.footer_text, | |
iconUrl: reddit_instance.embed.footer_icon, | |
}); | |
await interaction.respond({ embeds: [embed] }).catch((_) => { | |
interaction.respond(':warning: **__Error Triggered__** :warning:'); | |
}); | |
} else if (command == 'add' && subreddit !== undefined) { | |
await reddit_instance.function.createSubreddit(subreddit); | |
interaction | |
.respondEphemeral('Subreddit created successfully.') | |
.catch((_) => { | |
interaction.respondEphemeral( | |
':warning: **__Error Triggered__** :warning:' | |
); | |
}); | |
} else if (command == 'delete' && subreddit !== undefined) { | |
await reddit_instance.function.deleteSubreddit(subreddit); | |
interaction | |
.respondEphemeral('Subreddit deleted successfully.') | |
.catch((_) => { | |
interaction.respondEphemeral( | |
':warning: **__Error Triggered__** :warning:' | |
); | |
}); | |
} else if (command == 'set' && channel !== undefined) { | |
if (!interaction.member.can(reddit_instance.permission)) | |
return await interaction.respondEphemeral( | |
':x: `Permissions Error` You need the permission **Manage Channels** to use this command.' | |
); | |
await reddit_instance.storages.channel.put('reddit', channel.id); | |
reddit_instance.channel = channel.id; | |
interaction.respondEphemeral('Channel selected successfully.'); | |
} | |
} catch (err) { | |
throw new Error(err); | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment