56 lines
1.3 KiB
Python
56 lines
1.3 KiB
Python
|
#!/usr/bin/env python3
|
||
|
|
||
|
import discord
|
||
|
import json
|
||
|
import random
|
||
|
import sys
|
||
|
|
||
|
|
||
|
START = "__START"
|
||
|
END = "__END"
|
||
|
|
||
|
with open('markov.json') as f:
|
||
|
markov = 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)
|
||
|
|
||
|
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 or self.user not in message.mentions:
|
||
|
return
|
||
|
|
||
|
await message.channel.send(gen_sentence())
|
||
|
|
||
|
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(3600)
|
||
|
|
||
|
client = MyClient()
|
||
|
with open('token') as f:
|
||
|
token = f.read().strip()
|
||
|
client.run(token)
|