From 0d6ba5e8a85dd2d90778cb4420b818a889962a3b Mon Sep 17 00:00:00 2001 From: xenofem Date: Thu, 14 May 2020 15:26:15 -0400 Subject: [PATCH] move all configuration options into json file --- .gitignore | 2 +- config.json.example | 9 +++++++++ markov-bot.py | 20 ++++++++++---------- 3 files changed, 20 insertions(+), 11 deletions(-) create mode 100644 config.json.example diff --git a/.gitignore b/.gitignore index 54beaf3..9855e19 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ -token *~ corpus.txt markov.json +config.json env/ diff --git a/config.json.example b/config.json.example new file mode 100644 index 0000000..018c8a8 --- /dev/null +++ b/config.json.example @@ -0,0 +1,9 @@ +{ + "mode": "reply", + "reply_invfreq": 50, + "reply_delay_min": 10, + "reply_delay_max": 180, + "timed_interval_min": 7200, + "timed_interval_max": 14400, + "token": "tOk3N" +} diff --git a/markov-bot.py b/markov-bot.py index 5d9b8d6..7b174e9 100755 --- a/markov-bot.py +++ b/markov-bot.py @@ -6,9 +6,8 @@ import json import random import sys -TIMED = 0 -REPLY = 1 -mode = REPLY +TIMED = "timed" +REPLY = "reply" START = "__START" END = "__END" @@ -16,6 +15,9 @@ END = "__END" with open('markov.json') as f: markov = json.load(f) +with open('config.json') as f: + config = json.load(f) + def gen_sentence(): cur = START output = [] @@ -31,7 +33,7 @@ class MyClient(discord.Client): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - if mode == TIMED: + if config['mode'] == TIMED: self.bg_task = self.loop.create_task(self.background()) async def on_ready(self): @@ -45,8 +47,8 @@ class MyClient(discord.Client): await message.channel.send(gen_sentence()) return - if mode == REPLY and random.randrange(50) == 0: - await asyncio.sleep(random.randrange(180)) + if config['mode'] == REPLY and random.randrange(config['reply_invfreq']) == 0: + await asyncio.sleep(random.randrange(config['reply_delay_min'], config['reply_delay_max'])) await message.channel.send(gen_sentence()) return @@ -59,11 +61,9 @@ class MyClient(discord.Client): await channel.send(gen_sentence()) except: pass - await asyncio.sleep((2700 + random.randrange(1800)) // min(max(len(self.guilds), 1), 100)) + await asyncio.sleep(max(30, random.randrange(config['timed_interval_min'], config['timed_interval_max']) // max(1, len(self.guilds)))) if self.is_closed(): break client = MyClient() -with open('token') as f: - token = f.read().strip() -client.run(token) +client.run(config['token'])