Giter Club home page Giter Club logo

markov-discord's Introduction

MarkBot for Discord

A Markov chain bot using markov-strings.

Usage

  1. Configure what channels you want the bot to listen/learn from:
    • User: /listen modify
    • Bot: Select which channels your would like the bot to actively listen to
  2. Train the bot in a lengthy text channel:
    • User: /train
    • Bot: Parsing past messages from 5 channel(s).
  3. Ask the bot to say something:
    • User: /mark
    • Bot: worms are not baby snakes, by the way

Training from a file

Using the json option in the /train command, you can import a list of messages. An example JSON file can be seen here.

Setup

This bot stores your Discord server's entire message history, so a public instance to invite to your server is not available due to obvious data privacy concerns. Instead, you can host it yourself.

  1. Create a Discord bot application
  2. Under the "Bot" section, enable the "Message Content Intent", and copy the token for later.
  3. Setup and configure the bot using one of the below methods:

Docker

Running this bot in Docker is the easiest way to ensure it runs as expected and can easily recieve updates.

  1. Install Docker for your OS

  2. Open a command prompt and run:

    docker run --restart unless-stopped -d -v /my/host/dir:/usr/app/config ghcr.io/claabs/markov-discord:latest

    Where /my/host/dir is a accessible path on your system. --restart=unless-stopped is recommended in case an unexpected error crashes the bot.

  3. The Docker container will create a default config file in your mounted volume (/my/host/dir). Open it and add your bot token. You may change any other values to your liking as well. Details for each configuration item can be found here: https://claabs.github.io/markov-discord/classes/AppConfig.html

  4. Run the container again and use the invite link printed to the logs.

Windows

  1. Install Node.js 16 or newer.

  2. Download this repository using git in a command prompt

    git clone https://github.com/claabs/markov-discord.git

    or by just downloading and extracting the project zip from GitHub.

  3. Open a command prompt in the markov-discord folder.

    # NPM install non-development packages
    npm ci
    # Build the Typescript
    npm run build
    # Initialize the config
    npm start
  4. The program will create a config/config.json in the project folder. Open it and add your bot token. You may change any other values to your liking as well. Details for each configuration item can be found here: https://claabs.github.io/markov-discord/classes/AppConfig.html

  5. Run the bot:

    npm start

    And use the invite link printed to the logs.

markov-discord's People

Contributors

claabs avatar dependabot[bot] avatar sokolas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

markov-discord's Issues

No permissions to create a config file

I'm new in this scenario with docker, but reading my logs, i can understand why he doesn't create the config file, see:

2023-12-29 01:47:31 Error: Invalid config
2023-12-29 01:47:31 at Object. (/usr/app/src/config/setup.ts:72:9)
2023-12-29 01:47:31 at Module._compile (node:internal/modules/cjs/loader:1155:14)
2023-12-29 01:47:31 at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
2023-12-29 01:47:31 at Module.load (node:internal/modules/cjs/loader:1033:32)
2023-12-29 01:47:31 at Function.Module._load (node:internal/modules/cjs/loader:868:12)
2023-12-29 01:47:31 at Module.require (node:internal/modules/cjs/loader:1057:19)
2023-12-29 01:47:31 at require (node:internal/modules/cjs/helpers:103:18)
2023-12-29 01:47:31 at Object. (/usr/app/src/config/index.ts:2:1)
2023-12-29 01:47:31 at Module._compile (node:internal/modules/cjs/loader:1155:14)
2023-12-29 01:47:31 at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
2023-12-29 01:48:43 Error: Invalid config
2023-12-29 01:48:43 at Object. (/usr/app/src/config/setup.ts:72:9)
2023-12-29 01:48:43 at Module._compile (node:internal/modules/cjs/loader:1155:14)
2023-12-29 01:48:43 at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
2023-12-29 01:48:43 at Module.load (node:internal/modules/cjs/loader:1033:32)
2023-12-29 01:48:43 at Function.Module._load (node:internal/modules/cjs/loader:868:12)
2023-12-29 01:48:43 at Module.require (node:internal/modules/cjs/loader:1057:19)
2023-12-29 01:48:43 at require (node:internal/modules/cjs/helpers:103:18)
2023-12-29 01:48:43 at Object. (/usr/app/src/config/index.ts:2:1)
2023-12-29 01:48:43 at Module._compile (node:internal/modules/cjs/loader:1155:14)
2023-12-29 01:48:43 at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
2023-12-29 01:47:31 [2023-12-29 04:47:31.525 +0000] WARN: No config file detected
2023-12-29 01:47:31 [2023-12-29 04:47:31.525 +0000] INFO: Creating new config file
2023-12-29 01:47:31 newConfigPath: "/usr/app/config/config.json"
2023-12-29 01:47:31 [2023-12-29 04:47:31.528 +0000] INFO: Not allowed to create new config. Continuing...
2023-12-29 01:47:31 err: {
2023-12-29 01:47:31 "type": "Error",
2023-12-29 01:47:31 "message": "EACCES: permission denied, open '/usr/app/config/config.json'",
2023-12-29 01:47:31 "stack":
2023-12-29 01:47:31 Error: EACCES: permission denied, open '/usr/app/config/config.json'
2023-12-29 01:47:31 at Object.openSync (node:fs:590:3)
2023-12-29 01:47:31 at Object.writeFileSync (node:fs:2202:35)
2023-12-29 01:47:31 at Object.writeFileSync (/usr/app/node_modules/jsonfile/index.js:78:13)
2023-12-29 01:47:31 at Object. (/usr/app/src/config/setup.ts:54:8)
2023-12-29 01:47:31 at Module._compile (node:internal/modules/cjs/loader:1155:14)
2023-12-29 01:47:31 at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
2023-12-29 01:47:31 at Module.load (node:internal/modules/cjs/loader:1033:32)
2023-12-29 01:47:31 at Function.Module._load (node:internal/modules/cjs/loader:868:12)
2023-12-29 01:47:31 at Module.require (node:internal/modules/cjs/loader:1057:19)
2023-12-29 01:47:31 at require (node:internal/modules/cjs/helpers:103:18)
2023-12-29 01:47:31 "errno": -13,
2023-12-29 01:47:31 "syscall": "open",
2023-12-29 01:47:31 "code": "EACCES",
2023-12-29 01:47:31 "path": "/usr/app/config/config.json"
2023-12-29 01:47:31 }
2023-12-29 01:47:31 [2023-12-29 04:47:31.543 +0000] ERROR: Validation error(s)
2023-12-29 01:47:31 errors: [
2023-12-29 01:47:31 {
2023-12-29 01:47:31 "value": "",
2023-12-29 01:47:31 "property": "token",
2023-12-29 01:47:31 "children": [],
2023-12-29 01:47:31 "constraints": {
2023-12-29 01:47:31 "isNotEmpty": "token should not be empty"
2023-12-29 01:47:31 }
2023-12-29 01:47:31 }
2023-12-29 01:47:31 ]
2023-12-29 01:48:43 [2023-12-29 04:48:43.128 +0000] WARN: No config file detected
2023-12-29 01:48:43 [2023-12-29 04:48:43.129 +0000] INFO: Creating new config file
2023-12-29 01:48:43 newConfigPath: "/usr/app/config/config.json"
2023-12-29 01:48:43 [2023-12-29 04:48:43.134 +0000] INFO: Not allowed to create new config. Continuing...
2023-12-29 01:48:43 err: {
2023-12-29 01:48:43 "type": "Error",
2023-12-29 01:48:43 "message": "EACCES: permission denied, open '/usr/app/config/config.json'",
2023-12-29 01:48:43 "stack":
2023-12-29 01:48:43 Error: EACCES: permission denied, open '/usr/app/config/config.json'
2023-12-29 01:48:43 at Object.openSync (node:fs:590:3)

It can't create the config file and I can't run to troubleshoot because it stops when the error occurs. Please, I need help to troubleshoot this

unknown channel error

when i do /listen list it throws this error

2022-01-22 02:18:58.849 +0000] INFO: Recieved slash command
command: "listen"

/usr/app/node_modules/discord.js/src/rest/RequestHandler.js:350
throw new DiscordAPIError(data, res.status, request);
^
DiscordAPIError: Unknown Channel
at RequestHandler.execute (/usr/app/node_modules/discord.js/src/rest/RequestHandler.js:350:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at RequestHandler.push (/usr/app/node_modules/discord.js/src/rest/RequestHandler.js:51:14)
at GuildChannelManager.fetch (/usr/app/node_modules/discord.js/src/managers/GuildChannelManager.js:195:20)
at async Promise.all (index 0)
at getValidChannels (/usr/app/src/index.ts:102:5)
at listValidChannels (/usr/app/src/index.ts:518:20)
at Client. (/usr/app/src/index.ts:758:23)

Getting ZodError when attempting to start bot

When I attempt to run this bot using docker I get a strange ZodError, could you possibly help?

ZodError: [
{
"validation": "regex",
"code": "invalid_string",
"message": "Invalid",
"path": []
}
]
at new ZodError (/usr/app/node_modules/zod/src/ZodError.ts:140:5)
at handleResult (/usr/app/node_modules/zod/src/types.ts:72:19)
at ZodString.ZodType.safeParse (/usr/app/node_modules/zod/src/types.ts:184:12)
at ZodString.ZodType.parse (/usr/app/node_modules/zod/src/types.ts:162:25)
at X (/usr/app/node_modules/@discordjs/builders/src/interactions/slashCommands/Assertions.ts:15:16)
at MixedClass.setName (/usr/app/node_modules/@discordjs/builders/src/interactions/slashCommands/mixins/NameAndDescription.ts:14:3)
at Object. (/usr/app/src/deploy-commands.ts:18:4)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Module.load (node:internal/modules/cjs/loader:981:32)

Docker issue

i have tried multiple things all day, when I copy and paste docker run --restart unless-stopped --rm -ti -v /my/host/dir:/usr/app/config ghcr.io/claabs/markov-discord:latest
in the terminal I get this error
docker: Conflicting options: --restart and --rm.

Bot always crashes at midnight

I cannot provide a log right now, but it always crashes at midnight while trying to execute the node-schedule part of the code

Always fails to build a sentence

i have trained it with alot of data and have checked the db and it is saving it just if i do like "/mark hello" it doesnt work and always throws a failed to build error

Use SQLite database

The current solution uses 2 JSON files for storing the message history and corpus. This requires the entire thing to be loaded into memory when generating a message, or regenerating the corpus. This fork implements markov-strings as a SQL database; so adding it and storing the messages in a SQL database would significantly reduce memory usage (instant turnaround), remove corpus generation batch job, and maybe improve response time.

Retaining messages

I have multiple servers that I want to train the bot in, however, it only seems to save chats from one server at a time. I'm fine with excessive processing. Does it require a simple change in the code in order to retain messages across servers?

Bot unable to train from existing messages

After updating to the latest commit (updating dependencies and all) the bot is now unable to train from existing channels using the !mark train command. It responds to every other command but will not log anything in the console after !mark train is issued. Since the corpus isn't large enough to create new messages, I get this error after running !mark.

Listening...
Responding...
Error: Corpus is empty. There is either no data, or the data is not sufficient to create markov chains.
    at Markov.generate (/home/flatline/builds/markov-discord/node_modules/markov-strings/dist/index.js:145:19)
    at generateResponse (/home/flatline/builds/markov-discord/index.ts:281:31)
    at Client.<anonymous> (/home/flatline/builds/markov-discord/index.ts:386:7)
    at Client.emit (events.js:400:28)
    at MessageCreateAction.handle (/home/flatline/builds/markov-discord/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
    at Object.module.exports (/home/flatline/builds/markov-discord/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/home/flatline/builds/markov-discord/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (/home/flatline/builds/markov-discord/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (/home/flatline/builds/markov-discord/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
    at WebSocket.onMessage (/home/flatline/builds/markov-discord/node_modules/ws/lib/event-target.js:132:16)
Listening...

Bot crash error when the message is instantly deleted

I have auto-moderation running on my server, which deletes bad messages, and it turns out that when the bot is pinged and auto-moderation instantly deletes the message with ping, an error is caused and the bot crashes

[2022-04-14 20:23:54.678 +0000] INFO: Generated response text
    string: "Эй котик где твой \"господин\"? Просто я его"

/usr/app/node_modules/discord.js/src/rest/RequestHandler.js:350
      throw new DiscordAPIError(data, res.status, request);
            ^
DiscordAPIError: Invalid Form Body
message_reference: Unknown message
    at RequestHandler.execute (/usr/app/node_modules/discord.js/src/rest/RequestHandler.js:350:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at RequestHandler.push (/usr/app/node_modules/discord.js/src/rest/RequestHandler.js:51:14)
    at TextChannel.send (/usr/app/node_modules/discord.js/src/structures/interfaces/TextBasedChannel.js:175:15)
    at handleResponseMessage (/usr/app/src/index.ts:612:34)
    at Client.<anonymous> (/usr/app/src/index.ts:699:9)

Chokes on 3,000,000 msg dataset

Hi, I don't know if there's anything to be done about this, but I tried running this in a docker container against 3 million + messages in a single discord chat, and the bot just straight up hung. Would not respond to commands, percent indexed didn't change, etc. I doubt this is a high priority, but I figured I should let you know.

Docker Error: Not allowed to create new config.

2024-04-17 15:05:07 [2024-04-17 20:05:07.558 +0000] WARN: No config file detected
2024-04-17 15:05:07 [2024-04-17 20:05:07.558 +0000] INFO: Creating new config file
2024-04-17 15:05:07 newConfigPath: "/usr/app/config/config.json"
2024-04-17 15:05:07 [2024-04-17 20:05:07.560 +0000] INFO: Not allowed to create new config. Continuing...
2024-04-17 15:05:07 err: {
2024-04-17 15:05:07 "type": "Error",
2024-04-17 15:05:07 "message": "EACCES: permission denied, open '/usr/app/config/config.json'",
2024-04-17 15:05:07 "stack":
2024-04-17 15:05:07 Error: EACCES: permission denied, open '/usr/app/config/config.json'
2024-04-17 15:05:07 at Object.openSync (node:fs:590:3)
2024-04-17 15:05:07 at Object.writeFileSync (node:fs:2202:35)
2024-04-17 15:05:07 at Object.writeFileSync (/usr/app/node_modules/jsonfile/index.js:78:13)
2024-04-17 15:05:07 at Object. (/usr/app/src/config/setup.ts:54:8)
2024-04-17 15:05:07 at Module._compile (node:internal/modules/cjs/loader:1155:14)
2024-04-17 15:05:07 at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
2024-04-17 15:05:07 at Module.load (node:internal/modules/cjs/loader:1033:32)
2024-04-17 15:05:07 at Function.Module._load (node:internal/modules/cjs/loader:868:12)
2024-04-17 15:05:07 at Module.require (node:internal/modules/cjs/loader:1057:19)
2024-04-17 15:05:07 at require (node:internal/modules/cjs/helpers:103:18)
2024-04-17 15:05:07 "errno": -13,
2024-04-17 15:05:07 "syscall": "open",
2024-04-17 15:05:07 "code": "EACCES",
2024-04-17 15:05:07 "path": "/usr/app/config/config.json"
2024-04-17 15:05:07 }
2024-04-17 15:05:07 [2024-04-17 20:05:07.568 +0000] ERROR: Validation error(s)
2024-04-17 15:05:07 errors: [
2024-04-17 15:05:07 {
2024-04-17 15:05:07 "value": "",
2024-04-17 15:05:07 "property": "token",
2024-04-17 15:05:07 "children": [],
2024-04-17 15:05:07 "constraints": {
2024-04-17 15:05:07 "isNotEmpty": "token should not be empty"
2024-04-17 15:05:07 }
2024-04-17 15:05:07 }
2024-04-17 15:05:07 ]

I can add a config file (maybe that'll fix the problem), but I don't know where "usr/app/config" should be.

cant deploy the bot to heroku

i get this error

heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/ host=markovdiscordbottest.herokuapp.com fwd="my ip" dyno= queue= wait= connect= service= status=503 bytes=
heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/favicon.ico host=markovdiscordbottest.herokuapp.com fwd="my ip" dyno= queue= wait= connect= service= status=503 bytes=

Bot messages

Hi, can the bot send messages randomly or do we have to ping it (or use !mark) every time?

Frequency/Auto Settings

Is there any chance there can be some sort of Auto setting? A previous markov bot I was aware of allowed you to set a % chance for every message that was typed by a user in a listened channel to trigger a message from the bot. It created a lot of very fun chaos because the bot would chime in at random times and you never knew when or what was gonna happen. Additionally, if we can get % chance setting for how often it embeds an image instead of it randomly choosing to embed an image alongside its post that would also be wonderful!

Problem with Dockerfile

Docker: Docker version 20.10.12, build e91ed5707e

# docker build -t lev145/markov-discord-bot .
...
npm ERR! code ERR_SOCKET_TIMEOUT
npm ERR! network Socket timeout
npm ERR! network This is a problem related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network 
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly.  See: 'npm help config'

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2022-04-08T19_44_08_051Z-debug-0.log
The command '/bin/sh -c apk add --no-cache make gcc g++ python3     && npm ci --only=production' returned a non-zero code: 

Not pulling from recent messages

Hi, I am having trouble managing to get any recent messages into the corpus. When I train, it seems like it it only pulling from older messages closer to the beginning of the text channel. I have tried spamming specific words that do not show up when running !mark.

Bot fails to attach files.

Whenever the bot is trying to attach a file - it always attaching a file with 36 bytes size, which, if opened in Notepad, just has "This content is no longer available." in it. I believe it has something to do with that one change in Discord which grants only temporary access to the files, and when the access time is over - the bot wraps "This content is no longer available." text into whatever file type it tries to access and attaching it to the message.
изображение

Feature request: delete bot message if it receives N reactions

Hey there, been using the bot since the beginning of the year and have to say it's been a pretty solid experience. Thanks for your work!

The server I'm running it for has a long history, and occasionally our bot will dig some undesirable things up (deadnames, exes' names, etc). We're all aware that this is just part of the nature of running something like this, but also agree it would be nice to have functionality allowing us to remove said messages without needing an admin around.

Essentially what I'm asking for is to have the bot listen to reactions and if one of its own messages gets a number of specific (possibly configurable) reactions or more (ideally configurable), it'll delete that message.

My TypeScript is pretty rusty, but if you're open to PRs, I'd be potentially willing to submit one for this. I'd appreciate your thoughts; thanks for your time!

When trying to train, get this error message:

(node:10620) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'id' of undefined
    at fetchMessages (C:\markov-discord\index.ts:241:38)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:10620) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

Windows 10 Powershell in Admin mode. Latest markov-discord, latest Node.js with build tools . Had to npm install -g typescript to get npm run build to work.

program not creating config file

im literally brand new to trying stuff like this so im about as lost as i possibly can be.

Error: Invalid config
04:50:23 0|index | at Object. (C:\Users\yungf\markov-discord\src\config\setup.ts:73:9)
04:50:23 0|index | at Module._compile (node:internal/modules/cjs/loader:1103:14)
04:50:23 0|index | at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
04:50:23 0|index | at Module.load (node:internal/modules/cjs/loader:981:32)
04:50:23 0|index | at Function.Module._load (node:internal/modules/cjs/loader:822:12)
04:50:23 0|index | at Module.require (node:internal/modules/cjs/loader:1005:19)
04:50:23 0|index | at Module.Hook._require.Module.require (C:\Users\yungf\markov-discord\node_modules\require-in-the-middle\index.js:80:39)
04:50:23 0|index | at require (node:internal/modules/cjs/helpers:102:18)
04:50:23 0|index | at Object. (C:\Users\yungf\markov-discord\src\config\index.ts:2:1)
04:50:23 0|index | at Module._compile (node:internal/modules/cjs/loader:1103:14)
04:50:23 0|index | [2022-02-18 04:50:22.990 -0600] WARN: No config file detected
2022-02-18T04:50:23: PM2 log: App [index:0] exited with code [1] via signal [SIGINT]
04:50:23 PM2 | App [index:0] exited with code [1] via signal [SIGINT]
2022-02-18T04:50:23: PM2 log: App [index:0] starting in -fork mode-
04:50:23 PM2 | App [index:0] starting in -fork mode-
2022-02-18T04:50:23: PM2 log: App [index:0] online
04:50:23 PM2 | App [index:0] online

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.