/
ebooks.py
59 lines (47 loc) · 1.58 KB
/
ebooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import discord, asyncio, json, markovify, sys, random, re
class WithSymbols(markovify.Text):
yikes_re = re.compile(r'kill')
def test_sentence_input(self, sentence):
return not re.search(self.yikes_re, sentence)
def word_split(self, sentence):
return [
i.strip("'()[]\"")
for i in re.split(self.word_split_pattern, sentence)
]
class NewlineTextWithSymbols(WithSymbols, markovify.NewlineText):
pass
async def collect(async_iter):
ret = []
async for i in async_iter:
ret.append(i)
return ret
client = discord.Client()
model = None
@client.event
async def on_ready():
global model
model = NewlineTextWithSymbols(await collect(content_for('nightpool')), retain_original=False)
open('model', 'w').write(model.to_json())
# model = NewlineTextWithSymbols.from_json(open('model').read())
print('ready')
@client.event
async def on_message(message):
if should_respond_to(message):
await client.send_message(message.channel, content=model.make_sentence())
def should_respond_to(message):
if client.user in message.mentions: return True
if message.author.bot: return False
if message.channel.is_private: return True
if random.random() < 0.01: return True
async def content_for(user):
for channel in client.get_all_channels():
print("reading {}".format(channel))
try:
async for message in client.logs_from(channel, limit=sys.maxsize):
if message.author.name == user: yield message.clean_content
except discord.errors.Forbidden:
pass
token = ''
with open('token') as t:
token = t.read().strip()
client.run(token)