From 22ce6dfa7c8cda79b8315790396f0aeec5ca70d6 Mon Sep 17 00:00:00 2001 From: Matthew R Date: Sun, 27 Sep 2020 02:11:26 -0400 Subject: [PATCH] neural networks --- package.json | 1 + src/class/Client.ts | 6 +++--- src/class/Util.ts | 4 ++++ src/commands/index.ts | 1 + src/commands/score.ts | 1 + src/commands/train.ts | 37 ++++++++++++++++++++++++++++++++++++ src/models/NNTrainingData.ts | 13 +++++++++++++ src/models/index.ts | 1 + 8 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 src/commands/train.ts create mode 100644 src/models/NNTrainingData.ts diff --git a/package.json b/package.json index ed3780a..67c497b 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ }, "dependencies": { "axios": "^0.19.2", + "brain.js": "^2.0.0-beta.2", "eris": "^0.13.3", "eris-pagination": "bsian03/eris-pagination", "express": "^4.17.1", diff --git a/src/class/Client.ts b/src/class/Client.ts index a89d964..4fe7ff4 100644 --- a/src/class/Client.ts +++ b/src/class/Client.ts @@ -3,7 +3,7 @@ import pluris from 'pluris'; import mongoose from 'mongoose'; import { promises as fs } from 'fs'; import { Collection, Command, LocalStorage, Util, ServerManagement, Event } from '.'; -import { File, FileInterface, Member, MemberInterface, Moderation, ModerationInterface, Note, NoteInterface, PagerNumber, PagerNumberInterface, Rank, RankInterface, Redirect, RedirectInterface, Score, ScoreInterface, Staff, StaffInterface, Stat, StatInterface } from '../models'; +import { File, FileInterface, Member, MemberInterface, Moderation, ModerationInterface, NNTrainingData, NNTrainingDataInterface, Note, NoteInterface, PagerNumber, PagerNumberInterface, Rank, RankInterface, Redirect, RedirectInterface, Score, ScoreInterface, Staff, StaffInterface, Stat, StatInterface } from '../models'; import { Config } from '../../types'; // eslint-disable-line pluris(eris); @@ -21,14 +21,14 @@ export default class Client extends eris.Client { public serverManagement: ServerManagement; - public db: { File: mongoose.Model, Member: mongoose.Model, Moderation: mongoose.Model, Note: mongoose.Model, PagerNumber: mongoose.Model, Rank: mongoose.Model, Redirect: mongoose.Model, Score: mongoose.Model, Staff: mongoose.Model, Stat: mongoose.Model, local: { muted: LocalStorage } }; + public db: { File: mongoose.Model, Member: mongoose.Model, Moderation: mongoose.Model, NNTrainingData: mongoose.Model, Note: mongoose.Model, PagerNumber: mongoose.Model, Rank: mongoose.Model, Redirect: mongoose.Model, Score: mongoose.Model, Staff: mongoose.Model, Stat: mongoose.Model, local: { muted: LocalStorage } }; constructor(token: string, options?: eris.ClientOptions) { super(token, options); this.commands = new Collection(); this.events = new Collection(); this.intervals = new Collection(); - this.db = { File, Member, Moderation, Note, PagerNumber, Rank, Redirect, Score, Staff, Stat, local: { muted: new LocalStorage('muted') } }; + this.db = { File, Member, Moderation, NNTrainingData, Note, PagerNumber, Rank, Redirect, Score, Staff, Stat, local: { muted: new LocalStorage('muted') } }; } public async loadDatabase() { diff --git a/src/class/Util.ts b/src/class/Util.ts index e9914cc..a49fbaa 100644 --- a/src/class/Util.ts +++ b/src/class/Util.ts @@ -147,4 +147,8 @@ export default class Util { public randomNumber(min: number, max: number): number { return Math.round(Math.random() * (max - min) + min); } + + public encode(arg: string) { + return arg.split('').map((x) => x.charCodeAt(0) / 400); + } } diff --git a/src/commands/index.ts b/src/commands/index.ts index 41469fc..cc2ddba 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -27,6 +27,7 @@ export { default as roleinfo } from './roleinfo'; export { default as score } from './score'; export { default as stats } from './stats'; export { default as storemessages } from './storemessages'; +export { default as train } from './train'; export { default as unban } from './unban'; export { default as unmute } from './unmute'; export { default as whois } from './whois'; diff --git a/src/commands/score.ts b/src/commands/score.ts index 2f7c318..82c5452 100644 --- a/src/commands/score.ts +++ b/src/commands/score.ts @@ -79,6 +79,7 @@ export default class Score extends Command { } if (!user) return this.error(message.channel, 'Member not found.'); const score = await this.client.db.Score.findOne({ userID: user.id }); + if (!score) return this.error(message.channel, 'Community Report has not been generated yet.'); let totalScore = '0'; let activityScore = '0'; let moderationScore = '0'; diff --git a/src/commands/train.ts b/src/commands/train.ts new file mode 100644 index 0000000..7837840 --- /dev/null +++ b/src/commands/train.ts @@ -0,0 +1,37 @@ +import { Message, TextChannel } from 'eris'; +import { Client, Command } from '../class'; + +export default class Train extends Command { + constructor(client: Client) { + super(client); + this.name = 'train'; + this.description = 'Trains a neural network.'; + this.usage = `${this.client.config.prefix}train <0: good | 1: bad>`; + this.permissions = 1; + this.guildOnly = false; + this.enabled = true; + } + + public async run(message: Message, args: string[]) { + try { + if (args?.length < 3) return this.client.commands.get('help').run(message, [this.name]); + if (args[2] !== '0' && args[2] !== '1') return this.error(message.channel, 'Result must be either 0 or 1.'); + const channel = this.client.util.resolveGuildChannel(args[0], this.mainGuild); + if (!channel) return this.error(message.channel, 'Channel could not be found.'); + if (channel.type !== 0) return this.error(message.channel, 'Invalid channel type.'); + let msg: Message; + try { + msg = await channel.getMessage(args[1]); + } catch { + return this.error(message.channel, 'Could not find message.'); + } + if (!msg) return this.error(message.channel, 'Message could not be found.'); + + await this.client.db.NNTrainingData.updateOne({ name: 'tc' }, { $addToSet: { data: { input: this.client.util.encode(msg.content), output: { res: Number(args[2]) } } } }); + await message.delete(); + return this.success(message.channel, 'Neural Network trained successfully.'); + } catch (err) { + return this.client.util.handleError(err, message, this); + } + } +} diff --git a/src/models/NNTrainingData.ts b/src/models/NNTrainingData.ts new file mode 100644 index 0000000..fbff45b --- /dev/null +++ b/src/models/NNTrainingData.ts @@ -0,0 +1,13 @@ +import { Document, Schema, model } from 'mongoose'; + +export interface NNTrainingDataInterface extends Document { + name: string, + data: [{ input: any, output: object }]; +} + +const NNTrainingData: Schema = new Schema({ + name: String, + data: Array, +}); + +export default model('NNTrainingData', NNTrainingData); diff --git a/src/models/index.ts b/src/models/index.ts index 4c64936..b3ef0e7 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -1,6 +1,7 @@ export { default as File, FileInterface } from './File'; export { default as Member, MemberInterface } from './Member'; export { default as Moderation, ModerationInterface } from './Moderation'; +export { default as NNTrainingData, NNTrainingDataInterface } from './NNTrainingData'; export { default as Note, NoteInterface } from './Note'; export { default as PagerNumber, PagerNumberInterface, PagerNumberRaw } from './PagerNumber'; export { default as Rank, RankInterface } from './Rank';