mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-11 13:56:15 +00:00
feat(reddit-bot): command and event handler
This commit is contained in:
36
bots/reddit/commands/train.js
Normal file
36
bots/reddit/commands/train.js
Normal file
@@ -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.');
|
||||
}
|
||||
};
|
||||
30
bots/reddit/events/helper/aiResponse.js
Normal file
30
bots/reddit/events/helper/aiResponse.js
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -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;
|
||||
}
|
||||
});
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user