refactor(reddit-bot): don't use global variables

This commit is contained in:
GramingFoxTeam
2022-11-18 15:44:41 +03:00
parent 551891e4c3
commit e84edd3cd9
3 changed files with 26 additions and 29 deletions

View File

@@ -1,6 +1,6 @@
export default { export default {
command: 'train', command: 'train',
async execute(client, item, args) { async execute(client, helper, item, args) {
console.log(args); console.log(args);
const isAdmin = await client const isAdmin = await client
.getSubreddit('revancedapp') .getSubreddit('revancedapp')
@@ -14,7 +14,7 @@ export default {
if (isComment) { if (isComment) {
const commentData = (await client.getComment(item.parent_id).fetch()) const commentData = (await client.getComment(item.parent_id).fetch())
.body; .body;
client.helper.sendTrainData(commentData, args[0].toUpperCase()); helper.sendTrainData(commentData, args[0].toUpperCase());
} else { } else {
if (!args[1]) if (!args[1])
return client return client
@@ -24,7 +24,7 @@ export default {
); );
const postData = await client.getSubmission(item.parent_id).fetch(); const postData = await client.getSubmission(item.parent_id).fetch();
client.helper.sendTrainData( helper.sendTrainData(
args[1] === 'title' ? postData.title : postData.selftext, args[1] === 'title' ? postData.title : postData.selftext,
args[0].toUpperCase() args[0].toUpperCase()
); );

View File

@@ -1,8 +1,8 @@
export default { export default {
name: 'aiResponse', name: 'aiResponse',
once: false, once: false,
async execute(aiRes) { async execute(client, config, aiRes) {
const response = global.config.responses.find( const response = config.responses.find(
(res) => res.label === aiRes.predictions[0].label (res) => res.label === aiRes.predictions[0].label
); );
if (!response) return; if (!response) return;
@@ -14,12 +14,12 @@ export default {
switch (ids[0]) { switch (ids[0]) {
case 'comment': { case 'comment': {
global.client.getComment(ids[1]).reply(response.text); client.getComment(ids[1]).reply(response.text);
break; break;
} }
case 'post': { case 'post': {
global.client.getSubmission(ids[1]).reply(response.text); client.getSubmission(ids[1]).reply(response.text);
break; break;
} }
} }

View File

@@ -9,17 +9,14 @@ import HelperClient from '../../client/index.js';
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename); const __dirname = dirname(__filename);
const configJSON = readFileSync('../config.json', 'utf-8'); const config = JSON.parse(readFileSync('../config.json', 'utf-8'));
global.config = JSON.parse(configJSON);
global.client = new Snoowrap(global.config.reddit); const client = new Snoowrap(config.reddit);
const helper = new HelperClient(global.config); const helper = new HelperClient(config);
global.client.helper = helper;
helper.connect(); helper.connect();
global.client.commands = new Map(); client.commands = new Map();
global.client.helper = helper;
const commandsPath = join(__dirname, 'commands'); const commandsPath = join(__dirname, 'commands');
const commandFiles = readdirSync(commandsPath).filter((file) => const commandFiles = readdirSync(commandsPath).filter((file) =>
@@ -30,7 +27,7 @@ for (const file of commandFiles) {
const filePath = join(commandsPath, file); const filePath = join(commandsPath, file);
const command = (await import(`file://${filePath}`)).default; const command = (await import(`file://${filePath}`)).default;
if ('command' in command && 'execute' in command) { if ('command' in command && 'execute' in command) {
global.client.commands.set(command.command, command); client.commands.set(command.command, command);
} else { } else {
console.log( console.log(
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.` `[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 = { const args = {
subreddit: 'revancedapp', subreddit: 'revancedapp',
@@ -46,9 +43,9 @@ const args = {
pollTime: 5000 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) => { comments.on('item', async (item) => {
await handleItem(item, false); await handleItem(item, false);
@@ -60,28 +57,26 @@ posts.on('item', async (item) => {
async function handleItem(item, isPost) { async function handleItem(item, isPost) {
// The "skill issue (refresh)" incident. // 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; if (checkedItems.includes(item.id)) return;
global.checkedItems.push(item.id); checkedItems.push(item.id);
if (isPost) { if (isPost) {
// It's a post, we have to also send post body. // It's a post, we have to also send post body.
helper.scanText(item.title.toLowerCase(), `post/${item.id}`); helper.scanText(item.title.toLowerCase(), `post/${item.id}`);
helper.scanText(item.selftext.toLowerCase(), `post/${item.id}`); helper.scanText(item.selftext.toLowerCase(), `post/${item.id}`);
} else { } else {
const body = item.body.toLowerCase(); 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 const args = body
.replace(`u/${global.config.reddit.username.toLowerCase()} `, '') .replace(`u/${config.reddit.username.toLowerCase()} `, '')
.split(' '); .split(' ');
const command = args[0]; const command = args[0];
args.shift(); args.shift();
if (!global.client.commands.get(command)) return; if (!client.commands.get(command)) return;
await global.client.commands await client.commands.get(command).execute(client, helper, item, args);
.get(command)
.execute(global.client, item, args);
} else helper.scanText(item.body.toLowerCase(), `comment/${item.id}`); } else helper.scanText(item.body.toLowerCase(), `comment/${item.id}`);
} }
} }
@@ -97,8 +92,10 @@ for (const file of helperEventFiles) {
const filePath = join(helperEventsPath, file); const filePath = join(helperEventsPath, file);
const event = (await import(`file://${filePath}`)).default; const event = (await import(`file://${filePath}`)).default;
if (event.once) { if (event.once) {
helper.once(event.name, (...args) => event.execute(...args)); helper.once(event.name, (...args) =>
event.execute(client, config, ...args)
);
} else { } else {
helper.on(event.name, (...args) => event.execute(...args)); helper.on(event.name, (...args) => event.execute(client, config, ...args));
} }
} }