diff --git a/bots/reddit/commands/train.js b/bots/reddit/commands/train.js new file mode 100644 index 0000000..4503fdc --- /dev/null +++ b/bots/reddit/commands/train.js @@ -0,0 +1,36 @@ +export default { + command: 'train', + async execute(client, item, args) { + console.log(args); + const isAdmin = await client + .getSubreddit('revancedapp') + .getModerators({ name: item.author.name }); + if (!isAdmin) + return client.getComment(item.id).reply('You\'re not an admin.'); + if (!args[0]) + return client.getComment(item.id).reply('You didn\'t specifiy the label!'); + const isComment = item.parent_id.split('_')[0] === 't1'; + if (isComment) { + const commentData = (await client.getComment(item.parent_id).fetch()) + .body; + client.helper.sendTrainData(commentData, args[0].toUpperCase()); + } else { + if (!args[1]) + return client + .getComment(item.id) + .reply( + 'You didn\'t specifiy whether if title or description should be sent!' + ); + const postData = await client.getSubmission(item.parent_id).fetch(); + + client.helper.sendTrainData( + args[1] === 'title' ? postData.title : postData.selftext, + args[0].toUpperCase() + ); + } + + return client + .getComment(item.id) + .reply('Sent the training data to the server.'); + } +}; diff --git a/bots/reddit/events/helper/aiResponse.js b/bots/reddit/events/helper/aiResponse.js new file mode 100644 index 0000000..c0d4545 --- /dev/null +++ b/bots/reddit/events/helper/aiResponse.js @@ -0,0 +1,30 @@ +export default { + name: 'aiResponse', + once: false, + async execute(aiRes) { + const response = global.config.responses.find( + (res) => res.label === aiRes.predictions[0].label + ); + if (!response) return; + + if (Number(aiRes.predictions[0].score) >= response.threshold) { + const ids = aiRes.id.split('/'); + + if (!response.text) return; + + switch (ids[0]) { + case 'comment': { + global.client.getComment(ids[1]).reply(response.text); + break; + } + + case 'post': { + global.client.getSubmission(ids[1]).reply(response.text); + break; + } + } + + return; + } + } +}; diff --git a/bots/reddit/index.js b/bots/reddit/index.js index a12b889..d28bbe8 100644 --- a/bots/reddit/index.js +++ b/bots/reddit/index.js @@ -1,79 +1,107 @@ import Snoowrap from 'snoowrap'; import { SubmissionStream, CommentStream } from 'snoostorm'; -import { readFileSync } from 'node:fs'; +import { readFileSync, readdirSync } from 'node:fs'; +// Fix __dirname not being defined in ES modules. (https://stackoverflow.com/a/64383997) +import { fileURLToPath } from 'node:url'; +import { dirname, join } from 'node:path'; import HelperClient from '../../client/index.js'; +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const configJSON = readFileSync('../config.json', 'utf-8'); global.config = JSON.parse(configJSON); global.client = new Snoowrap(global.config.reddit); const helper = new HelperClient(global.config); +global.client.helper = helper; helper.connect(); +global.client.commands = new Map(); +global.client.helper = helper; + +const commandsPath = join(__dirname, 'commands'); +const commandFiles = readdirSync(commandsPath).filter((file) => + file.endsWith('.js') +); + +for (const file of commandFiles) { + const filePath = join(commandsPath, file); + const command = (await import(`file://${filePath}`)).default; + if ('command' in command && 'execute' in command) { + global.client.commands.set(command.command, command); + } else { + console.log( + `[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.` + ); + } +} + const BOT_START = Date.now() / 1000; global.checkedItems = []; const args = { - subreddit: 'revancedapp', - limit: 10, - pollTime: 5000 -} + subreddit: 'revancedapp', + limit: 10, + pollTime: 5000 +}; const comments = new CommentStream(global.client, args); const posts = new SubmissionStream(global.client, args); -comments.on('item', (item) => { - if (item.created_utc < BOT_START) return; - handleItem(item); +comments.on('item', async (item) => { + if (item.created_utc < BOT_START) return; + await handleItem(item, false); }); -posts.on('item', (item) => { - if (item.created_utc < BOT_START) return; - handleItem(item); +posts.on('item', async (item) => { + //if (item.created_utc < BOT_START) return; + await handleItem(item, true); }); -function handleItem(item) { - console.log(item); - if (global.checkedItems.includes(item.id)) return; - if (item.hasOwnProperty('over_18')) { - // It's a post, we have to also send post body. - helper.scanText(item.title.toLowerCase(), `post/${item.id}`); - helper.scanText(item.selftext.toLowerCase(), `post/${item.id}`); - } else { - // It's a comment! - helper.scanText(item.body.toLowerCase(), `comment/${item.id}`); - } +async function handleItem(item, isPost) { + // The "skill issue (refresh) incident." + if (item.author.name === global.config.reddit.username) return; + + if (global.checkedItems.includes(item.id)) return; + global.checkedItems.push(item.id); + if (isPost) { + // It's a post, we have to also send post body. + helper.scanText(item.title.toLowerCase(), `post/${item.id}`); + helper.scanText(item.selftext.toLowerCase(), `post/${item.id}`); + } else { + const body = item.body.toLowerCase(); + if (body.startsWith(`u/${global.config.reddit.username.toLowerCase()}`)) { + const args = body + .replace(`u/${global.config.reddit.username.toLowerCase()} `, '') + .split(' '); + const command = args[0]; + args.shift(); + + if (!global.client.commands.get(command)) return; + + await global.client.commands + .get(command) + .execute(global.client, item, args); + } else helper.scanText(item.body.toLowerCase(), `comment/${item.id}`); + } } -// Feel free to add an event handler. +// The ReVanced Helper events. -helper.on('aiResponse', async (aiRes) => { - const response = global.config.responses.find( - (res) => res.label === aiRes.predictions[0].label - ); - if (!response) return; +const helperEventsPath = join(__dirname, 'events/helper'); +const helperEventFiles = readdirSync(helperEventsPath).filter((file) => + file.endsWith('.js') +); - if (Number(aiRes.predictions[0].score) >= response.threshold) { - const ids = aiRes.id.split('/'); - - if (!response.text) return; - - switch (ids[0]) { - case 'comment': { - client.getComment(ids[1]).reply(response.text); - global.checkedItems.push(ids[1]); - break; - }; - - case 'post': { - client.getSubmission(ids[1]).reply(response.text); - global.checkedItems.push(ids[1]); - break; - } - } - - return; - } -}); \ No newline at end of file +for (const file of helperEventFiles) { + const filePath = join(helperEventsPath, file); + const event = (await import(`file://${filePath}`)).default; + if (event.once) { + helper.once(event.name, (...args) => event.execute(...args)); + } else { + helper.on(event.name, (...args) => event.execute(...args)); + } +} diff --git a/bots/telegram/commands/train.js b/bots/telegram/commands/train.js index c48a27e..1e2ae10 100644 --- a/bots/telegram/commands/train.js +++ b/bots/telegram/commands/train.js @@ -37,7 +37,6 @@ export default { } } - console.log(options); const admins = await global.bot.getChatAdministrators(msg.chat.id); const isAdmin = admins.find((admin) => admin.user.id === msg.from.id);