mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-11 13:56:15 +00:00
refactor(reddit-bot): don't use global variables
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
export default {
|
||||
command: 'train',
|
||||
async execute(client, item, args) {
|
||||
async execute(client, helper, item, args) {
|
||||
console.log(args);
|
||||
const isAdmin = await client
|
||||
.getSubreddit('revancedapp')
|
||||
@@ -14,7 +14,7 @@ export default {
|
||||
if (isComment) {
|
||||
const commentData = (await client.getComment(item.parent_id).fetch())
|
||||
.body;
|
||||
client.helper.sendTrainData(commentData, args[0].toUpperCase());
|
||||
helper.sendTrainData(commentData, args[0].toUpperCase());
|
||||
} else {
|
||||
if (!args[1])
|
||||
return client
|
||||
@@ -24,7 +24,7 @@ export default {
|
||||
);
|
||||
const postData = await client.getSubmission(item.parent_id).fetch();
|
||||
|
||||
client.helper.sendTrainData(
|
||||
helper.sendTrainData(
|
||||
args[1] === 'title' ? postData.title : postData.selftext,
|
||||
args[0].toUpperCase()
|
||||
);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
export default {
|
||||
name: 'aiResponse',
|
||||
once: false,
|
||||
async execute(aiRes) {
|
||||
const response = global.config.responses.find(
|
||||
async execute(client, config, aiRes) {
|
||||
const response = config.responses.find(
|
||||
(res) => res.label === aiRes.predictions[0].label
|
||||
);
|
||||
if (!response) return;
|
||||
@@ -14,12 +14,12 @@ export default {
|
||||
|
||||
switch (ids[0]) {
|
||||
case 'comment': {
|
||||
global.client.getComment(ids[1]).reply(response.text);
|
||||
client.getComment(ids[1]).reply(response.text);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'post': {
|
||||
global.client.getSubmission(ids[1]).reply(response.text);
|
||||
client.getSubmission(ids[1]).reply(response.text);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,17 +9,14 @@ 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);
|
||||
const config = JSON.parse(readFileSync('../config.json', 'utf-8'));
|
||||
|
||||
global.client = new Snoowrap(global.config.reddit);
|
||||
const helper = new HelperClient(global.config);
|
||||
global.client.helper = helper;
|
||||
const client = new Snoowrap(config.reddit);
|
||||
const helper = new HelperClient(config);
|
||||
|
||||
helper.connect();
|
||||
|
||||
global.client.commands = new Map();
|
||||
global.client.helper = helper;
|
||||
client.commands = new Map();
|
||||
|
||||
const commandsPath = join(__dirname, 'commands');
|
||||
const commandFiles = readdirSync(commandsPath).filter((file) =>
|
||||
@@ -30,7 +27,7 @@ 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);
|
||||
client.commands.set(command.command, command);
|
||||
} else {
|
||||
console.log(
|
||||
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
|
||||
@@ -38,7 +35,7 @@ for (const file of commandFiles) {
|
||||
}
|
||||
}
|
||||
|
||||
global.checkedItems = [];
|
||||
const checkedItems = [];
|
||||
|
||||
const args = {
|
||||
subreddit: 'revancedapp',
|
||||
@@ -46,9 +43,9 @@ const args = {
|
||||
pollTime: 5000
|
||||
};
|
||||
|
||||
const comments = new CommentStream(global.client, args);
|
||||
const comments = new CommentStream(client, args);
|
||||
|
||||
const posts = new SubmissionStream(global.client, args);
|
||||
const posts = new SubmissionStream(client, args);
|
||||
|
||||
comments.on('item', async (item) => {
|
||||
await handleItem(item, false);
|
||||
@@ -60,28 +57,26 @@ posts.on('item', async (item) => {
|
||||
|
||||
async function handleItem(item, isPost) {
|
||||
// The "skill issue (refresh)" incident.
|
||||
if (item.author.name === global.config.reddit.username) return;
|
||||
if (item.author.name === config.reddit.username) return;
|
||||
|
||||
if (global.checkedItems.includes(item.id)) return;
|
||||
global.checkedItems.push(item.id);
|
||||
if (checkedItems.includes(item.id)) return;
|
||||
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()}`)) {
|
||||
if (body.startsWith(`u/${config.reddit.username.toLowerCase()}`)) {
|
||||
const args = body
|
||||
.replace(`u/${global.config.reddit.username.toLowerCase()} `, '')
|
||||
.replace(`u/${config.reddit.username.toLowerCase()} `, '')
|
||||
.split(' ');
|
||||
const command = args[0];
|
||||
args.shift();
|
||||
|
||||
if (!global.client.commands.get(command)) return;
|
||||
if (!client.commands.get(command)) return;
|
||||
|
||||
await global.client.commands
|
||||
.get(command)
|
||||
.execute(global.client, item, args);
|
||||
await client.commands.get(command).execute(client, helper, item, args);
|
||||
} else helper.scanText(item.body.toLowerCase(), `comment/${item.id}`);
|
||||
}
|
||||
}
|
||||
@@ -97,8 +92,10 @@ 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));
|
||||
helper.once(event.name, (...args) =>
|
||||
event.execute(client, config, ...args)
|
||||
);
|
||||
} else {
|
||||
helper.on(event.name, (...args) => event.execute(...args));
|
||||
helper.on(event.name, (...args) => event.execute(client, config, ...args));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user