#!/usr/bin/env python3 import asyncio import discord import json import random import sys TIMED = "timed" REPLY = "reply" START = "__START" 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 = [] while True: cur = random.choice(markov[cur]) if cur == END: break output.append(cur) return ' '.join(output) class MyClient(discord.Client): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if config['mode'] == TIMED: self.bg_task = self.loop.create_task(self.background()) async def on_ready(self): print('logged in as {0.user}'.format(self), file=sys.stderr) async def on_message(self, message): if message.author == self.user: return if self.user in message.mentions or self.user in [m for role in message.role_mentions for m in role.members]: await message.channel.send(gen_sentence()) return 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 async def background(self): await self.wait_until_ready() while not self.is_closed(): for guild in self.guilds: try: channel = random.choice(guild.text_channels) await channel.send(gen_sentence()) except: pass 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() client.run(config['token'])