mirror of
https://github.com/pacnpal/markov-discord.git
synced 2025-12-20 11:01:04 -05:00
Load corpus from filesystem. Remove async markov functions. Add source-map support and npm scripts. Add myself as admin case
This commit is contained in:
33
index.ts
33
index.ts
@@ -1,4 +1,5 @@
|
|||||||
/* eslint-disable no-console */
|
/* eslint-disable no-console */
|
||||||
|
import 'source-map-support/register';
|
||||||
import * as Discord from 'discord.js';
|
import * as Discord from 'discord.js';
|
||||||
// https://discord.js.org/#/docs/main/stable/general/welcome
|
// https://discord.js.org/#/docs/main/stable/general/welcome
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
@@ -51,9 +52,6 @@ const markovOpts = {
|
|||||||
minScorePerWord: 0,
|
minScorePerWord: 0,
|
||||||
maxTries: 10000,
|
maxTries: 10000,
|
||||||
};
|
};
|
||||||
let markov: Markov;
|
|
||||||
// let markov = new Markov(markovDB, markovOpts);
|
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
function uniqueBy<Record extends { [key: string]: any }>(
|
function uniqueBy<Record extends { [key: string]: any }>(
|
||||||
arr: Record[],
|
arr: Record[],
|
||||||
@@ -101,14 +99,15 @@ function regenMarkov(): void {
|
|||||||
markovDB.splice(removeIndex, 1);
|
markovDB.splice(removeIndex, 1);
|
||||||
});
|
});
|
||||||
deletionCache = [];
|
deletionCache = [];
|
||||||
markov = new Markov(markovDB, markovOpts);
|
const markov = new Markov(markovDB, markovOpts);
|
||||||
markov.buildCorpusAsync();
|
|
||||||
fileObj.messages = markovDB;
|
fileObj.messages = markovDB;
|
||||||
// console.log("WRITING THE FOLLOWING DATA:")
|
// console.log("WRITING THE FOLLOWING DATA:")
|
||||||
// console.log(fileObj)
|
// console.log(fileObj)
|
||||||
fs.writeFileSync('config/markovDB.json', JSON.stringify(fileObj), 'utf-8');
|
fs.writeFileSync('config/markovDB.json', JSON.stringify(fileObj), 'utf-8');
|
||||||
fileObj.messages = [];
|
fileObj.messages = [];
|
||||||
messageCache = [];
|
messageCache = [];
|
||||||
|
markov.buildCorpus();
|
||||||
|
fs.writeFileSync('config/markov.json', JSON.stringify(markov));
|
||||||
console.log('Done regenerating Markov corpus.');
|
console.log('Done regenerating Markov corpus.');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,7 +152,8 @@ function isModerator(message: Discord.Message): boolean {
|
|||||||
member.hasPermission('ADMINISTRATOR') ||
|
member.hasPermission('ADMINISTRATOR') ||
|
||||||
member.hasPermission('MANAGE_CHANNELS') ||
|
member.hasPermission('MANAGE_CHANNELS') ||
|
||||||
member.hasPermission('KICK_MEMBERS') ||
|
member.hasPermission('KICK_MEMBERS') ||
|
||||||
member.hasPermission('MOVE_MEMBERS')
|
member.hasPermission('MOVE_MEMBERS') ||
|
||||||
|
member.id === '82684276755136512' // charlocharlie#8095
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -258,9 +258,14 @@ function generateResponse(
|
|||||||
};
|
};
|
||||||
options.maxTries = 5000;
|
options.maxTries = 5000;
|
||||||
}
|
}
|
||||||
markov
|
const fsMarkov = new Markov([''], markovOpts);
|
||||||
.generateAsync(options)
|
const markovFile = JSON.parse(fs.readFileSync('config/markov.json', 'utf-8')) as Markov;
|
||||||
.then(result => {
|
fsMarkov.corpus = markovFile.corpus;
|
||||||
|
fsMarkov.startWords = markovFile.startWords;
|
||||||
|
fsMarkov.endWords = markovFile.endWords;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = fsMarkov.generate(options);
|
||||||
const myResult = result as MarkbotMarkovResult;
|
const myResult = result as MarkbotMarkovResult;
|
||||||
console.log('Generated Result:', myResult);
|
console.log('Generated Result:', myResult);
|
||||||
const messageOpts: Discord.MessageOptions = { tts };
|
const messageOpts: Discord.MessageOptions = { tts };
|
||||||
@@ -268,8 +273,7 @@ function generateResponse(
|
|||||||
.filter(ref => Object.prototype.hasOwnProperty.call(ref, 'attachment'))
|
.filter(ref => Object.prototype.hasOwnProperty.call(ref, 'attachment'))
|
||||||
.map(ref => ref.attachment as string);
|
.map(ref => ref.attachment as string);
|
||||||
if (attachmentRefs.length > 0) {
|
if (attachmentRefs.length > 0) {
|
||||||
const randomRefAttachment =
|
const randomRefAttachment = attachmentRefs[Math.floor(Math.random() * attachmentRefs.length)];
|
||||||
attachmentRefs[Math.floor(Math.random() * attachmentRefs.length)];
|
|
||||||
messageOpts.files = [randomRefAttachment];
|
messageOpts.files = [randomRefAttachment];
|
||||||
} else {
|
} else {
|
||||||
const randomMessage = markovDB[Math.floor(Math.random() * markovDB.length)];
|
const randomMessage = markovDB[Math.floor(Math.random() * markovDB.length)];
|
||||||
@@ -281,14 +285,13 @@ function generateResponse(
|
|||||||
myResult.string.replace(/@everyone/g, '@everyοne'); // Replace @everyone with a homoglyph 'o'
|
myResult.string.replace(/@everyone/g, '@everyοne'); // Replace @everyone with a homoglyph 'o'
|
||||||
message.channel.send(result.string, messageOpts);
|
message.channel.send(result.string, messageOpts);
|
||||||
if (debug) message.channel.send(`\`\`\`\n${JSON.stringify(myResult, null, 2)}\n\`\`\``);
|
if (debug) message.channel.send(`\`\`\`\n${JSON.stringify(myResult, null, 2)}\n\`\`\``);
|
||||||
})
|
} catch (err) {
|
||||||
.catch(err => {
|
|
||||||
console.log(err);
|
console.log(err);
|
||||||
if (debug) message.channel.send(`\n\`\`\`\nERROR${err}\n\`\`\``);
|
if (debug) message.channel.send(`\n\`\`\`\nERROR: ${err}\n\`\`\``);
|
||||||
if (err.message.includes('Cannot build sentence with current corpus')) {
|
if (err.message.includes('Cannot build sentence with current corpus')) {
|
||||||
console.log('Not enough chat data for a response.');
|
console.log('Not enough chat data for a response.');
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
client.on('ready', () => {
|
client.on('ready', () => {
|
||||||
|
|||||||
56
package-lock.json
generated
56
package-lock.json
generated
@@ -206,6 +206,12 @@
|
|||||||
"color-convert": "^1.9.0"
|
"color-convert": "^1.9.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"arg": {
|
||||||
|
"version": "4.1.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/arg/-/arg-4.1.2.tgz",
|
||||||
|
"integrity": "sha512-+ytCkGcBtHZ3V2r2Z06AncYO8jz46UEamcspGoU8lHcEbpn6J77QK0vdWvChsclg/tM5XIJC5tnjmPp7Eq6Obg==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"argparse": {
|
"argparse": {
|
||||||
"version": "1.0.10",
|
"version": "1.0.10",
|
||||||
"resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz",
|
"resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz",
|
||||||
@@ -305,6 +311,11 @@
|
|||||||
"concat-map": "0.0.1"
|
"concat-map": "0.0.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"buffer-from": {
|
||||||
|
"version": "1.1.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz",
|
||||||
|
"integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A=="
|
||||||
|
},
|
||||||
"bufferutil": {
|
"bufferutil": {
|
||||||
"version": "4.0.1",
|
"version": "4.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/bufferutil/-/bufferutil-4.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/bufferutil/-/bufferutil-4.0.1.tgz",
|
||||||
@@ -438,6 +449,12 @@
|
|||||||
"object-keys": "^1.0.8"
|
"object-keys": "^1.0.8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"diff": {
|
||||||
|
"version": "4.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/diff/-/diff-4.0.1.tgz",
|
||||||
|
"integrity": "sha512-s2+XdvhPCOF01LRQBC8hf4vhbVmI2CGS5aZnxLJlT5FtdhPCDFq80q++zK2KlrVorVDdL5BOGZ/VfLrVtYNF+Q==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"discord.js": {
|
"discord.js": {
|
||||||
"version": "11.5.1",
|
"version": "11.5.1",
|
||||||
"resolved": "https://registry.npmjs.org/discord.js/-/discord.js-11.5.1.tgz",
|
"resolved": "https://registry.npmjs.org/discord.js/-/discord.js-11.5.1.tgz",
|
||||||
@@ -1197,6 +1214,12 @@
|
|||||||
"resolved": "https://registry.npmjs.org/long-timeout/-/long-timeout-0.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/long-timeout/-/long-timeout-0.1.1.tgz",
|
||||||
"integrity": "sha1-lyHXiLR+C8taJMLivuGg2lXatRQ="
|
"integrity": "sha1-lyHXiLR+C8taJMLivuGg2lXatRQ="
|
||||||
},
|
},
|
||||||
|
"make-error": {
|
||||||
|
"version": "1.3.5",
|
||||||
|
"resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.5.tgz",
|
||||||
|
"integrity": "sha512-c3sIjNUow0+8swNwVpqoH4YCShKNFkMaw6oH1mNS2haDZQqkeZFlHS3dhoeEbKKmJB4vXpJucU6oH75aDYeE9g==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"markov-strings": {
|
"markov-strings": {
|
||||||
"version": "2.1.0",
|
"version": "2.1.0",
|
||||||
"resolved": "https://registry.npmjs.org/markov-strings/-/markov-strings-2.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/markov-strings/-/markov-strings-2.1.0.tgz",
|
||||||
@@ -1700,6 +1723,20 @@
|
|||||||
"resolved": "https://registry.npmjs.org/sorted-array-functions/-/sorted-array-functions-1.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/sorted-array-functions/-/sorted-array-functions-1.2.0.tgz",
|
||||||
"integrity": "sha512-sWpjPhIZJtqO77GN+LD8dDsDKcWZ9GCOJNqKzi1tvtjGIzwfoyuRH8S0psunmc6Z5P+qfDqztSbwYR5X/e1UTg=="
|
"integrity": "sha512-sWpjPhIZJtqO77GN+LD8dDsDKcWZ9GCOJNqKzi1tvtjGIzwfoyuRH8S0psunmc6Z5P+qfDqztSbwYR5X/e1UTg=="
|
||||||
},
|
},
|
||||||
|
"source-map": {
|
||||||
|
"version": "0.6.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
|
||||||
|
"integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g=="
|
||||||
|
},
|
||||||
|
"source-map-support": {
|
||||||
|
"version": "0.5.16",
|
||||||
|
"resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.16.tgz",
|
||||||
|
"integrity": "sha512-efyLRJDr68D9hBBNIPWFjhpFzURh+KJykQwvMyW5UiZzYwoF6l4YMMDIJJEyFWxWCqfyxLzz6tSfUFR+kXXsVQ==",
|
||||||
|
"requires": {
|
||||||
|
"buffer-from": "^1.0.0",
|
||||||
|
"source-map": "^0.6.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"spdx-correct": {
|
"spdx-correct": {
|
||||||
"version": "3.1.0",
|
"version": "3.1.0",
|
||||||
"resolved": "https://registry.npmjs.org/spdx-correct/-/spdx-correct-3.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/spdx-correct/-/spdx-correct-3.1.0.tgz",
|
||||||
@@ -1910,6 +1947,19 @@
|
|||||||
"os-tmpdir": "~1.0.2"
|
"os-tmpdir": "~1.0.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"ts-node": {
|
||||||
|
"version": "8.5.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/ts-node/-/ts-node-8.5.4.tgz",
|
||||||
|
"integrity": "sha512-izbVCRV68EasEPQ8MSIGBNK9dc/4sYJJKYA+IarMQct1RtEot6Xp0bXuClsbUSnKpg50ho+aOAx8en5c+y4OFw==",
|
||||||
|
"dev": true,
|
||||||
|
"requires": {
|
||||||
|
"arg": "^4.1.0",
|
||||||
|
"diff": "^4.0.1",
|
||||||
|
"make-error": "^1.1.1",
|
||||||
|
"source-map-support": "^0.5.6",
|
||||||
|
"yn": "^3.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"tslib": {
|
"tslib": {
|
||||||
"version": "1.9.3",
|
"version": "1.9.3",
|
||||||
"resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.3.tgz",
|
"resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.3.tgz",
|
||||||
@@ -2014,6 +2064,12 @@
|
|||||||
"async-limiter": "~1.0.0"
|
"async-limiter": "~1.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"yn": {
|
||||||
|
"version": "3.1.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz",
|
||||||
|
"integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"zlib-sync": {
|
"zlib-sync": {
|
||||||
"version": "0.1.6",
|
"version": "0.1.6",
|
||||||
"resolved": "https://registry.npmjs.org/zlib-sync/-/zlib-sync-0.1.6.tgz",
|
"resolved": "https://registry.npmjs.org/zlib-sync/-/zlib-sync-0.1.6.tgz",
|
||||||
|
|||||||
@@ -4,7 +4,10 @@
|
|||||||
"description": "A conversational Markov chain bot for Discord",
|
"description": "A conversational Markov chain bot for Discord",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node index.js",
|
"start": "node dist/index.js",
|
||||||
|
"start:ts": "ts-node index.ts",
|
||||||
|
"build": "tsc",
|
||||||
|
"lint": "tsc --noEmit && eslint **/*.ts",
|
||||||
"docker-up": "docker run --rm -e TOKEN=abc123 -it $(docker build -q .)"
|
"docker-up": "docker run --rm -e TOKEN=abc123 -it $(docker build -q .)"
|
||||||
},
|
},
|
||||||
"repository": "https://github.com/charlocharlie/markov-discord.git",
|
"repository": "https://github.com/charlocharlie/markov-discord.git",
|
||||||
@@ -27,6 +30,7 @@
|
|||||||
"erlpack": "github:discordapp/erlpack",
|
"erlpack": "github:discordapp/erlpack",
|
||||||
"markov-strings": "^2.1.0",
|
"markov-strings": "^2.1.0",
|
||||||
"node-schedule": "^1.3.2",
|
"node-schedule": "^1.3.2",
|
||||||
|
"source-map-support": "^0.5.16",
|
||||||
"zlib-sync": "^0.1.6"
|
"zlib-sync": "^0.1.6"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
@@ -43,6 +47,7 @@
|
|||||||
"eslint-plugin-import": "^2.19.1",
|
"eslint-plugin-import": "^2.19.1",
|
||||||
"eslint-plugin-prettier": "^3.1.2",
|
"eslint-plugin-prettier": "^3.1.2",
|
||||||
"prettier": "^1.19.1",
|
"prettier": "^1.19.1",
|
||||||
|
"ts-node": "^8.5.4",
|
||||||
"typescript": "^3.7.4"
|
"typescript": "^3.7.4"
|
||||||
},
|
},
|
||||||
"eslintIgnore": [
|
"eslintIgnore": [
|
||||||
|
|||||||
Reference in New Issue
Block a user