/
main.py
89 lines (69 loc) · 2.36 KB
/
main.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from discord.ext import commands
import discord
import asyncio
import json
import Chat
import traceback
startup = True
with open('config.json') as f:
config = json.load(f)
bot = commands.Bot(command_prefix=config['prefix'], self_bot=False)
# gets chat history
async def get_hist(chan, lim):
history = []
hist_itr = chan.history(limit=lim)
async for m in hist_itr:
history.insert(0, m.content)
print(m.content)
print(history)
return history
# train the chatbot if user is sudo
@bot.command()
async def train(ctx, n):
if "sudoers file" in [y.name for y in ctx.author.roles]:
await ctx.send('This operation takes a while, please do not run it too frequently')
if not n:
n = config['history backlog']
print('getting history')
h = await get_hist(ctx.channel, int(n))
print('generating model')
Chat.generate_model(h)
await ctx.send('Done!')
else:
await ctx.send(f'`{ctx.author.display_name} is not in the sudoers file. This incident will be reported.`')
# get response
@bot.command()
async def chat(ctx, *, message):
async with ctx.channel.typing():
print('getting response')
response = Chat.get_response(message)
await ctx.send(response)
# triggers an error, for debugging
@bot.command()
async def whoops(ctxs):
raise Exception('you fool. you absolute buffoon. you think you can challenge me in my own realm? you think you can\
rebel against my authority? you dare come into my house and upturn my dining chairs and spill coffee grounds in my\
Keurig? you thought you were safe in your chain mail armor behind that screen of yours. I will take these laminate\
wood floor boards and destroy you. I didn’t want war. but i didn’t start it.')
# tells you when you cause an error. because it's all your fault
@bot.event
async def on_command_error(ctx, error):
await ctx.send(f'oopsie woopsie! i made a fucky wucky!\n{error}')
raise error
# tell you when the bot is ready
@bot.event
async def on_ready():
global startup
if startup:
startup = False
print('Logged in')
# main function to start the bot
def main():
loop = asyncio.get_event_loop()
loop.create_task(bot.start(config['token']))
try:
loop.run_forever()
finally:
loop.stop()
if __name__ == '__main__':
main()