mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-11 13:56:15 +00:00
feat: prettier and eslint
This commit is contained in:
18
.eslintrc.cjs
Normal file
18
.eslintrc.cjs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
module.exports = {
|
||||||
|
env: {
|
||||||
|
es2021: true,
|
||||||
|
node: true
|
||||||
|
},
|
||||||
|
extends: 'eslint:recommended',
|
||||||
|
overrides: [],
|
||||||
|
parserOptions: {
|
||||||
|
ecmaVersion: 'latest',
|
||||||
|
sourceType: 'module'
|
||||||
|
},
|
||||||
|
rules: {
|
||||||
|
indent: ['error', 'tab'],
|
||||||
|
'linebreak-style': ['error', 'windows'],
|
||||||
|
quotes: ['error', 'single'],
|
||||||
|
semi: ['error', 'always']
|
||||||
|
}
|
||||||
|
};
|
||||||
18
.eslintrc.js
Normal file
18
.eslintrc.js
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
module.exports = {
|
||||||
|
env: {
|
||||||
|
es2021: true,
|
||||||
|
node: true
|
||||||
|
},
|
||||||
|
extends: 'eslint:recommended',
|
||||||
|
overrides: [],
|
||||||
|
parserOptions: {
|
||||||
|
ecmaVersion: 'latest',
|
||||||
|
sourceType: 'module'
|
||||||
|
},
|
||||||
|
rules: {
|
||||||
|
indent: ['error', 'tab'],
|
||||||
|
'linebreak-style': ['error', 'windows'],
|
||||||
|
quotes: ['error', 'single'],
|
||||||
|
semi: ['error', 'always']
|
||||||
|
}
|
||||||
|
};
|
||||||
4
.prettierrc.json
Normal file
4
.prettierrc.json
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"singleQuote": true,
|
||||||
|
"trailingComma": "none"
|
||||||
|
}
|
||||||
@@ -1,19 +1,19 @@
|
|||||||
{
|
{
|
||||||
"discord": {
|
"discord": {
|
||||||
"token": "YOUR-BOT-TOKEN-HERE",
|
"token": "YOUR-BOT-TOKEN-HERE",
|
||||||
"id": "1038762591805247518"
|
"id": "1038762591805247518"
|
||||||
},
|
},
|
||||||
|
|
||||||
"server": {
|
|
||||||
"port": 3000,
|
|
||||||
"host": "192.168.1.6"
|
|
||||||
},
|
|
||||||
|
|
||||||
"responses": [
|
"server": {
|
||||||
{
|
"port": 3000,
|
||||||
"label": "DOWNLOAD",
|
"host": "192.168.1.6"
|
||||||
"threshold": 0.85,
|
},
|
||||||
"text": "you wanted peevanced"
|
|
||||||
}
|
"responses": [
|
||||||
]
|
{
|
||||||
}
|
"label": "DOWNLOAD",
|
||||||
|
"threshold": 0.85,
|
||||||
|
"text": "you wanted peevanced"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,35 +1,49 @@
|
|||||||
import { ContextMenuCommandBuilder, ApplicationCommandType, ActionRowBuilder, SelectMenuBuilder, ComponentType } from 'discord.js';
|
import {
|
||||||
|
ContextMenuCommandBuilder,
|
||||||
|
ApplicationCommandType,
|
||||||
|
ActionRowBuilder,
|
||||||
|
SelectMenuBuilder,
|
||||||
|
ComponentType
|
||||||
|
} from 'discord.js';
|
||||||
export default {
|
export default {
|
||||||
data: new ContextMenuCommandBuilder()
|
data: new ContextMenuCommandBuilder()
|
||||||
.setName('Train Message')
|
.setName('Train Message')
|
||||||
.setType(ApplicationCommandType.Message),
|
.setType(ApplicationCommandType.Message),
|
||||||
async execute(interaction) {
|
async execute(interaction) {
|
||||||
const options = [];
|
const options = [];
|
||||||
|
|
||||||
for (const { label } of global.config.responses) {
|
for (const { label } of global.config.responses) {
|
||||||
options.push({
|
options.push({
|
||||||
label: label,
|
label: label,
|
||||||
description: `The ${label} label.`,
|
description: `The ${label} label.`,
|
||||||
value: label.toLowerCase()
|
value: label.toLowerCase()
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
const row = new ActionRowBuilder()
|
const row = new ActionRowBuilder().addComponents(
|
||||||
.addComponents(
|
new SelectMenuBuilder()
|
||||||
new SelectMenuBuilder()
|
.setCustomId('select')
|
||||||
.setCustomId('select')
|
.setPlaceholder('Nothing selected')
|
||||||
.setPlaceholder('Nothing selected')
|
.addOptions(options)
|
||||||
.addOptions(options),
|
);
|
||||||
);
|
const reply = await interaction.reply({
|
||||||
const reply = await interaction.reply({ content: 'Please select the corresponding label to train the bot.', components: [row], ephemeral: true });
|
content: 'Please select the corresponding label to train the bot.',
|
||||||
|
components: [row],
|
||||||
|
ephemeral: true
|
||||||
|
});
|
||||||
|
|
||||||
const collector = reply.createMessageComponentCollector({ componentType: ComponentType.StringSelect, time: 15000 });
|
const collector = reply.createMessageComponentCollector({
|
||||||
|
componentType: ComponentType.StringSelect,
|
||||||
|
time: 15000
|
||||||
|
});
|
||||||
|
|
||||||
collector.on('collect', i => {
|
collector.on('collect', (i) => {
|
||||||
i.client.helper.sendTrainData(interaction.targetMessage.content, i.values[0].toUpperCase());
|
i.client.helper.sendTrainData(
|
||||||
|
interaction.targetMessage.content,
|
||||||
|
i.values[0].toUpperCase()
|
||||||
|
);
|
||||||
|
|
||||||
i.reply({ content: 'Sent train data to server.', ephemeral: true });
|
i.reply({ content: 'Sent train data to server.', ephemeral: true });
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,23 +1,28 @@
|
|||||||
import { Events } from 'discord.js';
|
import { Events } from 'discord.js';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: Events.InteractionCreate,
|
name: Events.InteractionCreate,
|
||||||
once: false,
|
once: false,
|
||||||
async execute(interaction) {
|
async execute(interaction) {
|
||||||
if (!interaction.isMessageContextMenuCommand()) return;
|
if (!interaction.isMessageContextMenuCommand()) return;
|
||||||
|
|
||||||
const command = interaction.client.commands.get(interaction.commandName);
|
const command = interaction.client.commands.get(interaction.commandName);
|
||||||
|
|
||||||
if (!command) {
|
if (!command) {
|
||||||
console.error(`No command matching ${interaction.commandName} was found.`);
|
console.error(
|
||||||
return;
|
`No command matching ${interaction.commandName} was found.`
|
||||||
}
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await command.execute(interaction);
|
await command.execute(interaction);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
|
await interaction.reply({
|
||||||
}
|
content: 'There was an error while executing this command!',
|
||||||
}
|
ephemeral: true
|
||||||
}
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { Events } from 'discord.js';
|
import { Events } from 'discord.js';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: Events.MessageCreate,
|
name: Events.MessageCreate,
|
||||||
once: false,
|
once: false,
|
||||||
execute(msg) {
|
execute(msg) {
|
||||||
msg.client.helper.scanText(msg.content, `${msg.channelId}/${msg.id}`);
|
msg.client.helper.scanText(msg.content, `${msg.channelId}/${msg.id}`);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -1,30 +1,31 @@
|
|||||||
export default {
|
export default {
|
||||||
name: 'aiResponse',
|
name: 'aiResponse',
|
||||||
once: false,
|
once: false,
|
||||||
async execute(aiRes) {
|
async execute(aiRes) {
|
||||||
console.log(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;
|
||||||
|
|
||||||
if (Number(aiRes.predictions[0].score) >= response.threshold) {
|
if (Number(aiRes.predictions[0].score) >= response.threshold) {
|
||||||
const ids = aiRes.id.split('/');
|
const ids = aiRes.id.split('/');
|
||||||
let channel = client.channels.cache.get(ids[0]);
|
let channel = global.client.channels.cache.get(ids[0]);
|
||||||
|
|
||||||
if (!channel) {
|
if (!channel) {
|
||||||
await client.channels.fetch(ids[0]);
|
await global.client.channels.fetch(ids[0]);
|
||||||
channel = client.channels.cache.get(ids[0]);
|
channel = global.client.channels.cache.get(ids[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
let message = channel.messages.cache.get(ids[1]);
|
let message = channel.messages.cache.get(ids[1]);
|
||||||
|
|
||||||
if (!message) {
|
if (!message) {
|
||||||
await channel.messages.fetch(ids[1]);
|
await channel.messages.fetch(ids[1]);
|
||||||
message = channel.messages.cache.get(ids[1]);
|
message = channel.messages.cache.get(ids[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
message.reply(response.text);
|
message.reply(response.text);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
export default {
|
export default {
|
||||||
name: 'ocrResponse',
|
name: 'ocrResponse',
|
||||||
once: false,
|
once: false,
|
||||||
execute(client, ocrRes) {
|
execute() {
|
||||||
// TODO
|
// TODO
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Client, Events, GatewayIntentBits, Collection } from 'discord.js';
|
import { Client, GatewayIntentBits, Collection } from 'discord.js';
|
||||||
import { readFileSync, readdirSync } from 'node:fs';
|
import { readFileSync, readdirSync } from 'node:fs';
|
||||||
// Fix __dirname not being defined in ES modules. (https://stackoverflow.com/a/64383997)
|
// Fix __dirname not being defined in ES modules. (https://stackoverflow.com/a/64383997)
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
@@ -11,45 +11,59 @@ import HelperClient from '../../client/index.js';
|
|||||||
const configJSON = readFileSync('../config.json', 'utf-8');
|
const configJSON = readFileSync('../config.json', 'utf-8');
|
||||||
global.config = JSON.parse(configJSON);
|
global.config = JSON.parse(configJSON);
|
||||||
|
|
||||||
const helper = new HelperClient(config);
|
const helper = new HelperClient(global.config);
|
||||||
|
|
||||||
helper.connect();
|
helper.connect();
|
||||||
|
|
||||||
global.client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
|
global.client = new Client({
|
||||||
|
intents: [
|
||||||
|
GatewayIntentBits.Guilds,
|
||||||
|
GatewayIntentBits.GuildMessages,
|
||||||
|
GatewayIntentBits.MessageContent
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
client.commands = new Collection();
|
global.client.commands = new Collection();
|
||||||
client.helper = helper;
|
global.client.helper = helper;
|
||||||
|
|
||||||
const commandsPath = join(__dirname, 'commands');
|
const commandsPath = join(__dirname, 'commands');
|
||||||
const commandFiles = readdirSync(commandsPath).filter(file => file.endsWith('.js'));
|
const commandFiles = readdirSync(commandsPath).filter((file) =>
|
||||||
|
file.endsWith('.js')
|
||||||
|
);
|
||||||
|
|
||||||
for (const file of commandFiles) {
|
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 ('data' in command && 'execute' in command) {
|
if ('data' in command && 'execute' in command) {
|
||||||
client.commands.set(command.data.name, command);
|
global.client.commands.set(command.data.name, command);
|
||||||
} else {
|
} else {
|
||||||
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
|
console.log(
|
||||||
|
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const discordEventsPath = join(__dirname, 'events/discord');
|
const discordEventsPath = join(__dirname, 'events/discord');
|
||||||
const discordEventFiles = readdirSync(discordEventsPath).filter(file => file.endsWith('.js'));
|
const discordEventFiles = readdirSync(discordEventsPath).filter((file) =>
|
||||||
|
file.endsWith('.js')
|
||||||
|
);
|
||||||
|
|
||||||
for (const file of discordEventFiles) {
|
for (const file of discordEventFiles) {
|
||||||
const filePath = join(discordEventsPath, file);
|
const filePath = join(discordEventsPath, file);
|
||||||
const event = (await import(`file://${filePath}`)).default;
|
const event = (await import(`file://${filePath}`)).default;
|
||||||
if (event.once) {
|
if (event.once) {
|
||||||
client.once(event.name, (...args) => event.execute(...args));
|
global.client.once(event.name, (...args) => event.execute(...args));
|
||||||
} else {
|
} else {
|
||||||
client.on(event.name, (...args) => event.execute(...args));
|
global.client.on(event.name, (...args) => event.execute(...args));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// The ReVanced Helper events.
|
// The ReVanced Helper events.
|
||||||
|
|
||||||
const helperEventsPath = join(__dirname, 'events/helper');
|
const helperEventsPath = join(__dirname, 'events/helper');
|
||||||
const helperEventFiles = readdirSync(helperEventsPath).filter(file => file.endsWith('.js'));
|
const helperEventFiles = readdirSync(helperEventsPath).filter((file) =>
|
||||||
|
file.endsWith('.js')
|
||||||
|
);
|
||||||
|
|
||||||
for (const file of helperEventFiles) {
|
for (const file of helperEventFiles) {
|
||||||
const filePath = join(helperEventsPath, file);
|
const filePath = join(helperEventsPath, file);
|
||||||
@@ -61,4 +75,4 @@ for (const file of helperEventFiles) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
client.login(config.discord.token);
|
global.client.login(global.config.discord.token);
|
||||||
|
|||||||
@@ -5,7 +5,9 @@ const config = JSON.parse(configJSON);
|
|||||||
|
|
||||||
const commands = [];
|
const commands = [];
|
||||||
// Grab all the command files from the commands directory you created earlier
|
// Grab all the command files from the commands directory you created earlier
|
||||||
const commandFiles = readdirSync('./commands').filter(file => file.endsWith('.js'));
|
const commandFiles = readdirSync('./commands').filter((file) =>
|
||||||
|
file.endsWith('.js')
|
||||||
|
);
|
||||||
|
|
||||||
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
|
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
|
||||||
for (const file of commandFiles) {
|
for (const file of commandFiles) {
|
||||||
@@ -17,16 +19,17 @@ for (const file of commandFiles) {
|
|||||||
const rest = new REST({ version: '10' }).setToken(config.discord.token);
|
const rest = new REST({ version: '10' }).setToken(config.discord.token);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log(`Started refreshing ${commands.length} application (/) commands.`);
|
console.log(
|
||||||
|
`Started refreshing ${commands.length} application (/) commands.`
|
||||||
|
);
|
||||||
|
|
||||||
// The put method is used to fully refresh all commands in the guild with the current set
|
// The put method is used to fully refresh all commands in the guild with the current set
|
||||||
const data = await rest.put(
|
const data = await rest.put(Routes.applicationCommands(config.discord.id), {
|
||||||
Routes.applicationCommands(config.discord.id),
|
body: commands
|
||||||
{ body: commands },
|
});
|
||||||
);
|
|
||||||
|
|
||||||
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
|
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// And of course, make sure you catch and log any errors!
|
// And of course, make sure you catch and log any errors!
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
|
|||||||
122
client/index.js
122
client/index.js
@@ -3,76 +3,84 @@ import { serialize, deserialize } from 'bson';
|
|||||||
import EventEmitter from 'node:events';
|
import EventEmitter from 'node:events';
|
||||||
|
|
||||||
class HelperClient extends EventEmitter {
|
class HelperClient extends EventEmitter {
|
||||||
constructor({ server }) {
|
constructor({ server }) {
|
||||||
super();
|
super();
|
||||||
if (!server?.port) throw new Error('You did not specify the server port.');
|
if (!server?.port) throw new Error('You did not specify the server port.');
|
||||||
this.server = server;
|
this.server = server;
|
||||||
};
|
}
|
||||||
|
|
||||||
connect() {
|
connect() {
|
||||||
this.client = createConnection(this.server.port, this.server.host ? this.server.host : 'localhost', () => {
|
this.client = createConnection(
|
||||||
this.emit('connect');
|
this.server.port,
|
||||||
});
|
this.server.host ? this.server.host : 'localhost',
|
||||||
|
() => {
|
||||||
|
this.emit('connect');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
this.client.on('data', (data) => {
|
this.client.on('data', (data) => {
|
||||||
const eventData = deserialize(data, { allowObjectSmallerThanBufferSize: true });
|
const eventData = deserialize(data, {
|
||||||
|
allowObjectSmallerThanBufferSize: true
|
||||||
|
});
|
||||||
|
|
||||||
switch (eventData.op) {
|
switch (eventData.op) {
|
||||||
case 2: {
|
case 2: {
|
||||||
// The 'aiResponse' event.
|
// The 'aiResponse' event.
|
||||||
|
|
||||||
this.emit('aiResponse', eventData);
|
this.emit('aiResponse', eventData);
|
||||||
};
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case 6: {
|
case 6: {
|
||||||
// The 'ocrResponse' event.
|
// The 'ocrResponse' event.
|
||||||
|
|
||||||
this.emit('ocrResponse', eventData);
|
this.emit('ocrResponse', eventData);
|
||||||
};
|
break;
|
||||||
};
|
}
|
||||||
});
|
}
|
||||||
};
|
});
|
||||||
|
}
|
||||||
|
|
||||||
sendData(data) {
|
sendData(data) {
|
||||||
this.client.write(serialize(data));
|
this.client.write(serialize(data));
|
||||||
return;
|
return;
|
||||||
};
|
}
|
||||||
|
|
||||||
scanText(text, id) {
|
scanText(text, id) {
|
||||||
this.sendData({
|
this.sendData({
|
||||||
op: 1,
|
op: 1,
|
||||||
id,
|
id,
|
||||||
text
|
text
|
||||||
});
|
});
|
||||||
|
|
||||||
return;
|
return;
|
||||||
};
|
}
|
||||||
|
|
||||||
scanImage(url, id) {
|
scanImage(url, id) {
|
||||||
this.sendData({
|
this.sendData({
|
||||||
op: 5,
|
op: 5,
|
||||||
id,
|
id,
|
||||||
url
|
url
|
||||||
});
|
});
|
||||||
|
|
||||||
return;
|
return;
|
||||||
};
|
}
|
||||||
|
|
||||||
sendTrainData(text, label) {
|
sendTrainData(text, label) {
|
||||||
this.sendData({
|
this.sendData({
|
||||||
op: 3,
|
op: 3,
|
||||||
label,
|
label,
|
||||||
text
|
text
|
||||||
});
|
});
|
||||||
|
|
||||||
return;
|
return;
|
||||||
};
|
}
|
||||||
|
|
||||||
trainAI() {
|
trainAI() {
|
||||||
this.sendData({ op: 4 });
|
this.sendData({ op: 4 });
|
||||||
|
|
||||||
return;
|
return;
|
||||||
};
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
export default HelperClient;
|
export default HelperClient;
|
||||||
|
|||||||
1939
package-lock.json
generated
Normal file
1939
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -11,5 +11,12 @@
|
|||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/reisxd/revanced-helper/issues"
|
"url": "https://github.com/reisxd/revanced-helper/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/reisxd/revanced-helper#readme"
|
"homepage": "https://github.com/reisxd/revanced-helper#readme",
|
||||||
|
"scripts": {
|
||||||
|
"lint": "prettier --write . && eslint --fix ."
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"eslint": "^8.27.0",
|
||||||
|
"prettier": "2.7.1"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,9 +8,9 @@ Sending the server this JSON (BSON) will send you back the AI predictions.
|
|||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"op": 1,
|
"op": 1,
|
||||||
"id": "String",
|
"id": "String",
|
||||||
"text": "How do i download ReVanced?"
|
"text": "How do i download ReVanced?"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -18,14 +18,14 @@ And the server would return something like this:
|
|||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"op": 2,
|
"op": 2,
|
||||||
"id": "String",
|
"id": "String",
|
||||||
"predictions": [
|
"predictions": [
|
||||||
{
|
{
|
||||||
"label": "DOWNLOAD",
|
"label": "DOWNLOAD",
|
||||||
"score": "1"
|
"score": "1"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -35,9 +35,9 @@ To add data to the train data, send a BSON (JSON) like this:
|
|||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"op": 3,
|
"op": 3,
|
||||||
"label": "FALSEPOSITIVE",
|
"label": "FALSEPOSITIVE",
|
||||||
"text": "how"
|
"text": "how"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -45,7 +45,7 @@ To train the AI and to re-load it, send this BSON (JSON):
|
|||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"event": 4
|
"event": 4
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -55,9 +55,9 @@ Sending the server this JSON (BSON) will send you back the read text.
|
|||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"op": 5,
|
"op": 5,
|
||||||
"id": "String",
|
"id": "String",
|
||||||
"url": "https://cdn.discordapp.com/attachments/1033338556493606963/1033338557231796224/Screenshot_20221022-121318.jpg"
|
"url": "https://cdn.discordapp.com/attachments/1033338556493606963/1033338557231796224/Screenshot_20221022-121318.jpg"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -65,8 +65,8 @@ And the server would return something like this:
|
|||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"op": 6,
|
"op": 6,
|
||||||
"id": "String",
|
"id": "String",
|
||||||
"ocrText": "..."
|
"ocrText": "..."
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"server": {
|
"server": {
|
||||||
"port": 3000
|
"port": 3000
|
||||||
},
|
},
|
||||||
|
|
||||||
"fasttext": {
|
"fasttext": {
|
||||||
"bin": "./model/fastText/fasttext",
|
"bin": "./model/fastText/fasttext",
|
||||||
"loadModel": "./model/model.bin",
|
"loadModel": "./model/model.bin",
|
||||||
"trainFile": "./model/train.tsv",
|
"trainFile": "./model/train.tsv",
|
||||||
"debug": true
|
"debug": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,15 +2,21 @@ import { readFileSync, writeFileSync } from 'node:fs';
|
|||||||
import { join } from 'node:path';
|
import { join } from 'node:path';
|
||||||
|
|
||||||
export default function addTrainData(eventData) {
|
export default function addTrainData(eventData) {
|
||||||
const file = readFileSync(join(global.__dirname, global.config.fasttext.trainFile), 'utf-8');
|
const file = readFileSync(
|
||||||
const data = file.split('\n');
|
join(global.__dirname, global.config.fasttext.trainFile),
|
||||||
const { label, text } = eventData;
|
'utf-8'
|
||||||
|
);
|
||||||
|
const data = file.split('\n');
|
||||||
|
const { label, text } = eventData;
|
||||||
|
|
||||||
const labelIndex = data.findIndex((data) => data.startsWith(label));
|
const labelIndex = data.findIndex((data) => data.startsWith(label));
|
||||||
|
|
||||||
data.splice(labelIndex === -1 ? 0 : labelIndex, 0, `${label} ${text}`);
|
data.splice(labelIndex === -1 ? 0 : labelIndex, 0, `${label} ${text}`);
|
||||||
|
|
||||||
writeFileSync(join(global.__dirname, global.config.fasttext.trainFile), data.join('\n'));
|
writeFileSync(
|
||||||
|
join(global.__dirname, global.config.fasttext.trainFile),
|
||||||
|
data.join('\n')
|
||||||
|
);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
import { serialize } from 'bson';
|
import { serialize } from 'bson';
|
||||||
|
|
||||||
export default async function runAI(client, data) {
|
export default async function runAI(client, data) {
|
||||||
const predictions = await global.ft.predict(data.text);
|
const predictions = await global.ft.predict(data.text);
|
||||||
const jsonData = {
|
const jsonData = {
|
||||||
op: 2,
|
op: 2,
|
||||||
id: data.id,
|
id: data.id,
|
||||||
predictions
|
predictions
|
||||||
};
|
};
|
||||||
|
|
||||||
const bsonData = serialize(jsonData);
|
const bsonData = serialize(jsonData);
|
||||||
client.write(bsonData);
|
client.write(bsonData);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,4 @@ import trainAI from './trainAI.js';
|
|||||||
import runOCR from './ocr.js';
|
import runOCR from './ocr.js';
|
||||||
import addTrainData from './addTrainData.js';
|
import addTrainData from './addTrainData.js';
|
||||||
|
|
||||||
export {
|
export { runAI, trainAI, runOCR, addTrainData };
|
||||||
runAI,
|
|
||||||
trainAI,
|
|
||||||
runOCR,
|
|
||||||
addTrainData
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -2,22 +2,22 @@ import { recognize } from 'node-tesseract-ocr';
|
|||||||
import { serialize } from 'bson';
|
import { serialize } from 'bson';
|
||||||
|
|
||||||
export default async function runOCR(client, eventData) {
|
export default async function runOCR(client, eventData) {
|
||||||
const config = {
|
const config = {
|
||||||
lang: 'eng',
|
lang: 'eng',
|
||||||
oem: 3,
|
oem: 3,
|
||||||
psm: 3,
|
psm: 3
|
||||||
};
|
};
|
||||||
|
|
||||||
const ocrText = await recognize(eventData.url, config);
|
const ocrText = await recognize(eventData.url, config);
|
||||||
|
|
||||||
const jsonData = {
|
const jsonData = {
|
||||||
op: 6,
|
op: 6,
|
||||||
id: eventData.id,
|
id: eventData.id,
|
||||||
ocrText
|
ocrText
|
||||||
};
|
};
|
||||||
|
|
||||||
const bsonData = serialize(jsonData);
|
const bsonData = serialize(jsonData);
|
||||||
client.write(bsonData);
|
client.write(bsonData);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,53 +2,56 @@ import FastText from 'fasttext.js';
|
|||||||
import { join } from 'node:path';
|
import { join } from 'node:path';
|
||||||
|
|
||||||
export default async function trainAI() {
|
export default async function trainAI() {
|
||||||
const ft = new FastText({
|
const ft = new FastText({
|
||||||
train: {
|
train: {
|
||||||
// number of concurrent threads
|
// number of concurrent threads
|
||||||
thread: 8,
|
thread: 8,
|
||||||
// verbosity level [2]
|
// verbosity level [2]
|
||||||
verbose: 4,
|
verbose: 4,
|
||||||
// number of negatives sampled [5]
|
// number of negatives sampled [5]
|
||||||
neg: 7,
|
neg: 7,
|
||||||
// loss function {ns, hs, softmax} [ns]
|
// loss function {ns, hs, softmax} [ns]
|
||||||
loss: 'ns',
|
loss: 'ns',
|
||||||
// learning rate [0.05]
|
// learning rate [0.05]
|
||||||
lr: 1,
|
lr: 1,
|
||||||
// change the rate of updates for the learning rate [100]
|
// change the rate of updates for the learning rate [100]
|
||||||
lrUpdateRate: 1000,
|
lrUpdateRate: 1000,
|
||||||
// max length of word ngram [1]
|
// max length of word ngram [1]
|
||||||
wordNgrams: 5,
|
wordNgrams: 5,
|
||||||
// minimal number of word occurences
|
// minimal number of word occurences
|
||||||
minCount: 1,
|
minCount: 1,
|
||||||
// minimal number of word occurences
|
// minimal number of word occurences
|
||||||
minCountLabel: 1,
|
minCountLabel: 1,
|
||||||
// size of word vectors [100]
|
// size of word vectors [100]
|
||||||
dim: 100,
|
dim: 100,
|
||||||
// size of the context window [5]
|
// size of the context window [5]
|
||||||
ws: 5,
|
ws: 5,
|
||||||
// number of epochs [5]
|
// number of epochs [5]
|
||||||
epoch: 20,
|
epoch: 20,
|
||||||
// number of buckets [2000000]
|
// number of buckets [2000000]
|
||||||
bucket: 2000000,
|
bucket: 2000000,
|
||||||
// min length of char ngram [3]
|
// min length of char ngram [3]
|
||||||
minn: process.env.TRAIN_MINN || 3,
|
minn: process.env.TRAIN_MINN || 3,
|
||||||
// max length of char ngram [6]
|
// max length of char ngram [6]
|
||||||
maxn: process.env.TRAIN_MAXN || 6,
|
maxn: process.env.TRAIN_MAXN || 6,
|
||||||
// sampling threshold [0.0001]
|
// sampling threshold [0.0001]
|
||||||
t: 0.0001,
|
t: 0.0001,
|
||||||
// load pre trained word vectors from unsupervised model
|
// load pre trained word vectors from unsupervised model
|
||||||
pretrainedVectors: ''
|
pretrainedVectors: ''
|
||||||
},
|
},
|
||||||
serializeTo: join(global.__dirname, global.config.fasttext.loadModel).replace('.bin', ''),
|
serializeTo: join(
|
||||||
trainFile: join(global.__dirname, global.config.fasttext.trainFile),
|
global.__dirname,
|
||||||
bin: join(global.__dirname, global.config.fasttext.bin)
|
global.config.fasttext.loadModel
|
||||||
});
|
).replace('.bin', ''),
|
||||||
|
trainFile: join(global.__dirname, global.config.fasttext.trainFile),
|
||||||
global.ft.unload();
|
bin: join(global.__dirname, global.config.fasttext.bin)
|
||||||
|
});
|
||||||
|
|
||||||
await ft.train()
|
global.ft.unload();
|
||||||
|
|
||||||
global.ft.load();
|
await ft.train();
|
||||||
|
|
||||||
return;
|
global.ft.load();
|
||||||
}
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { readFileSync } from 'node:fs';
|
import { readFileSync } from 'node:fs';
|
||||||
// Fix __dirname not being defined in ES modules. (https://stackoverflow.com/a/64383997)
|
// Fix __dirname not being defined in ES modules. (https://stackoverflow.com/a/64383997)
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
import { dirname, join } from 'node:path';
|
import { dirname } from 'node:path';
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
global.__dirname = dirname(__filename);
|
global.__dirname = dirname(__filename);
|
||||||
@@ -22,33 +22,33 @@ ft.load();
|
|||||||
global.ft = ft;
|
global.ft = ft;
|
||||||
|
|
||||||
const server = createServer(async (client) => {
|
const server = createServer(async (client) => {
|
||||||
client.on('data', async (data) => {
|
client.on('data', async (data) => {
|
||||||
const eventData = deserialize(data, { allowObjectSmallerThanBufferSize: true });
|
const eventData = deserialize(data, {
|
||||||
|
allowObjectSmallerThanBufferSize: true
|
||||||
|
});
|
||||||
|
|
||||||
switch(eventData.op) {
|
switch (eventData.op) {
|
||||||
case 1: {
|
case 1: {
|
||||||
runAI(client, eventData);
|
runAI(client, eventData);
|
||||||
break;
|
break;
|
||||||
};
|
}
|
||||||
|
|
||||||
case 3: {
|
case 3: {
|
||||||
addTrainData(eventData);
|
addTrainData(eventData);
|
||||||
break;
|
break;
|
||||||
};
|
}
|
||||||
|
|
||||||
case 4: {
|
case 4: {
|
||||||
trainAI();
|
trainAI();
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
};
|
|
||||||
|
|
||||||
case 5: {
|
case 5: {
|
||||||
runOCR(client, eventData);
|
runOCR(client, eventData);
|
||||||
break;
|
break;
|
||||||
};
|
}
|
||||||
};
|
}
|
||||||
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
server.listen(global.config.server.port || 3000);
|
server.listen(global.config.server.port || 3000);
|
||||||
|
|||||||
Reference in New Issue
Block a user