diff --git a/README.md b/README.md index 31251cd..42ee41d 100644 --- a/README.md +++ b/README.md @@ -19,11 +19,6 @@ Current features: Minimal needed is an env var called `TOKEN` that contains the bot's token. -Cogs may have additional options, these variables **should** be prefix with the -cog's name - -ex: Cog `Math` may have `Math.TEMP_UNIT=fahrenheit`. - ## Cog Docs ### [Markov](src/bingus/cogs/markov.py) @@ -38,18 +33,18 @@ based on previous knowledge of what tokens the current token has been proceeded - `/markov`: Make bingus try and reply to a prompt passed, use this to bypass the 80% change that bingus usually has to reply - `/scan_history`*: Scan the history of the current channel and add it to the chain. Since Bingus only learns - from *new\* messages while he's active, you may need to do this when restarting him. This command can take a while depending on the number of messages. + from*new\* messages while he's active, you may need to do this when restarting him. This command can take a while depending on the number of messages. - `/dump_chain`\*: Dumps the entire underlying markov chain as a JSON file and sends it - `/load_chain`\*: Loads a markov chain JSON (as generated by `/dump_chain`) additively - `/weights`: Dump the weights of the specified token to other tokens #### Markov Config -- `Markov.REPLY_CHANNELS`: A _comma-delimited_ list of channel IDs that the bot should have +- `REPLY_CHANNELS`: A _comma-delimited_ list of channel IDs that the bot should have have a chance to reply to messages in. The bot still learns from all channels in realtime, but these channels it'll have an 80% of replying to any message -- `Markov.BRAIN_FILE`: Path to file where the chain will be persisted. This file will automatically be created +- `BRAIN_FILE`: Path to file where the chain will be persisted. This file will automatically be created if it doesn't exist already. The file itself is msgpack compressed with brotli, so it's recommended to give it a `msgpackz` extension. By default it will be set to `$PWD/brain.msgpackz` @@ -89,8 +84,7 @@ way we can easily disable it if needed and nothing will break. #### Cog Config -For simplicity we'll just use env vars for config. Prefix any env vars your cog will -read with the cog's name and a `.` (example: `Markov` cog can have a var called `Markov.REPLY_CHANNELS`). +For simplicity we'll just use env vars for config. Try to documents these options within this README file under the [The individual cogs docs](#cog-docs). Create a third-level heading with your cog's name, and a link to its source code. diff --git a/flake.nix b/flake.nix index 091a261..77a265a 100644 --- a/flake.nix +++ b/flake.nix @@ -23,8 +23,8 @@ }; }; - outputs = inputs @ { flakelight, ... }: - flakelight ./. ({ lib, ... }: { + outputs = inputs @ {flakelight, ...}: + flakelight ./. ({lib, ...}: { inherit inputs; systems = lib.systems.flakeExposed; pname = "bingus"; diff --git a/nix/nixosModule.nix b/nix/nixosModule.nix new file mode 100644 index 0000000..443dc36 --- /dev/null +++ b/nix/nixosModule.nix @@ -0,0 +1,54 @@ +{ + pkgs, + lib, + config, + ... +}: let + cfg = config.services.bingus-bot; +in { + options.services.bingus-bot = { + enable = lib.mkOption { + type = lib.types.bool; + default = false; + example = true; + description = "Whether to enable Bingus, a Discord bot that uses Markov Chains."; + }; + + brainFile = lib.mkOption { + default = "/var/lib/bingus/brain.msgpackz"; + type = lib.types.path; + description = "The path to save Bingus' brain to."; + }; + + tokenFile = lib.mkOption { + default = "/var/lib/bingus/token"; + type = lib.types.path; + description = "The path to load the Discord bot token to auth to the gateway with"; + }; + + replyChannels = lib.mkOption { + default = []; + type = lib.types.listOf lib.types.number; + description = "List of channel IDs that the bot should have a chance to reply in"; + }; + }; + + config = lib.mkIf cfg.enable { + systemd.services.bingus = let + replyChannelsStr = lib.strings.concatStrings (lib.strings.intersperse "," (builtins.map builtins.toString cfg.replyChannels)); + in { + wantedBy = ["multi-user.target"]; + after = ["network.target"]; + environment = { + "REPLY_CHANNELS" = replyChannelsStr; + "BRAIN_FILE" = cfg.brainFile; + }; + serviceConfig.ExecStart = pkgs.writeScript "bingus-exec" '' + #!/bin/sh + + mkdir -p $(dirname ${cfg.brainFile}) + TOKEN=$(cat ${cfg.tokenFile}) ${pkgs.bingus}/bin/bingus + ''; + }; + }; +} diff --git a/nix/packages/bingus-env.nix b/nix/packages/bingus.nix similarity index 100% rename from nix/packages/bingus-env.nix rename to nix/packages/bingus.nix diff --git a/src/bingus/cogs/markov.py b/src/bingus/cogs/markov.py index 0843854..00dc637 100644 --- a/src/bingus/cogs/markov.py +++ b/src/bingus/cogs/markov.py @@ -16,9 +16,9 @@ class Markov(commands.Cog): def __init__(self, bot: discord.bot.Bot): self.bot = bot self.reply_channels = [ - int(x) for x in os.getenv("Markov.REPLY_CHANNELS", "0").split(",") + int(x) for x in os.getenv("REPLY_CHANNELS", "0").split(",") ] - self.chain_file = Path(os.getenv("Markov.BRAIN_FILE", "brain.msgpackz")) + self.chain_file = Path(os.getenv("BRAIN_FILE", "brain.msgpackz")) if self.chain_file.is_file(): print(f"Attempting load from {self.chain_file}...") try: