move all configuration options into json file

master
xenofem 2020-05-14 15:26:15 -04:00
parent ffc81bf4ce
commit 0d6ba5e8a8
3 changed files with 20 additions and 11 deletions

2
.gitignore vendored
View File

@ -1,5 +1,5 @@
token
*~ *~
corpus.txt corpus.txt
markov.json markov.json
config.json
env/ env/

9
config.json.example Normal file
View File

@ -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"
}

View File

@ -6,9 +6,8 @@ import json
import random import random
import sys import sys
TIMED = 0 TIMED = "timed"
REPLY = 1 REPLY = "reply"
mode = REPLY
START = "__START" START = "__START"
END = "__END" END = "__END"
@ -16,6 +15,9 @@ END = "__END"
with open('markov.json') as f: with open('markov.json') as f:
markov = json.load(f) markov = json.load(f)
with open('config.json') as f:
config = json.load(f)
def gen_sentence(): def gen_sentence():
cur = START cur = START
output = [] output = []
@ -31,7 +33,7 @@ class MyClient(discord.Client):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
if mode == TIMED: if config['mode'] == TIMED:
self.bg_task = self.loop.create_task(self.background()) self.bg_task = self.loop.create_task(self.background())
async def on_ready(self): async def on_ready(self):
@ -45,8 +47,8 @@ class MyClient(discord.Client):
await message.channel.send(gen_sentence()) await message.channel.send(gen_sentence())
return return
if mode == REPLY and random.randrange(50) == 0: if config['mode'] == REPLY and random.randrange(config['reply_invfreq']) == 0:
await asyncio.sleep(random.randrange(180)) await asyncio.sleep(random.randrange(config['reply_delay_min'], config['reply_delay_max']))
await message.channel.send(gen_sentence()) await message.channel.send(gen_sentence())
return return
@ -59,11 +61,9 @@ class MyClient(discord.Client):
await channel.send(gen_sentence()) await channel.send(gen_sentence())
except: except:
pass 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(): if self.is_closed():
break break
client = MyClient() client = MyClient()
with open('token') as f: client.run(config['token'])
token = f.read().strip()
client.run(token)