forked from MinoMino/minqlx-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleverbot.py
More file actions
153 lines (132 loc) · 5.79 KB
/
Copy pathcleverbot.py
File metadata and controls
153 lines (132 loc) · 5.79 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# If you have any suggestions or issues/problems with this plugin you can contact me(kanzo) on irc at #minqlbot
# or alternatively you can open an issue at https://github.com/cstewart90/minqlx-plugins/issues
"""
You can talk to cleverbot using !chat and it will respond.
The bot can also respond to chat randomly if you set `qlx_cleverbotChance`.
Uses https://cleverbot.io API.
!chat - Chat to cleverbot!
!create <nick> - Create a bot.
!chance [chance] - Set chance that bot responds to chat. If you just do !chance it will output current chance.
Cvars and their default values:
qlx_cleverbotUser "" - cleverbot.io API user.
qlx_cleverbotKey "" - cleverbot.io API key.
qlx_cleverbotNick "Cleverbot" - Bot nick.
qlx_cleverbotChance "0" - Chance to respond to chat(0-1), 0.1 would be 10% of the time.
"""
import minqlx
import requests
import random
class cleverbot(minqlx.Plugin):
def __init__(self):
super().__init__()
self.add_hook("chat", self.handle_chat)
self.add_command("create", self.cmd_create, 2, usage="<nick>")
self.add_command("chat", self.cmd_chat, usage="<some text>")
self.add_command("chance", self.cmd_chance, 2, usage="[chance]")
# Get an API key at cleverbot.io
self.set_cvar_once("qlx_cleverbotUser", "")
self.set_cvar_once("qlx_cleverbotKey", "")
self.set_cvar_once("qlx_cleverbotNick", "Cleverbot")
# Percentage chance to respond to chat, float between 0 and 1.
self.set_cvar_limit_once("qlx_cleverbotChance", "0", "0", "1")
self.created = False
self.post_data("create")
def handle_chat(self, player, msg, channel):
"""Responds to chat messages
`qlx_cleverbotChance` * 100 percent of the time.
"""
if msg.startswith(self.get_cvar("qlx_commandPrefix")) or channel != "chat":
return
try:
chance = self.get_cvar("qlx_cleverbotChance", float)
except ValueError:
self.logger.warning("qlx_cleverbotChance is not a valid float.")
return
if random.random() < chance:
msg = self.clean_text(msg)
self.post_data("ask", msg, channel)
def cmd_create(self, player, msg, channel):
"""Creates the bot with the nick given.
API Doc: https://docs.cleverbot.io/docs/getting-started"""
if len(msg) == 1:
return minqlx.RET_USAGE
nick = ' '.join(msg[1:])
self.set_cvar("qlx_cleverbotNick", nick)
self.post_data("create", channel=channel)
def cmd_chat(self, player, msg, channel):
"""Responds to !chat some text
Example: !chat Just a small town girl
cleverbot: Livin' in a lonely world
API Doc: https://docs.cleverbot.io/docs/querying-cleverbot
"""
if len(msg) == 1:
return minqlx.RET_USAGE
else:
text = ' '.join(msg[1:])
if self.created:
self.post_data("ask", text, channel)
else:
channel.reply("^3You need to create the bot or set API key first.")
def cmd_chance(self, player, msg, channel):
"""Sets chance that the bot responds to chat.
If you just do !chance with no args it will output current chance."""
if len(msg) == 1:
chance = self.get_cvar("qlx_cleverbotChance")
channel.reply("Chance is currently {}".format(chance))
return minqlx.RET_STOP_ALL
elif len(msg) > 2:
return minqlx.RET_USAGE
try:
chance = float(msg[1])
if not 0 <= chance <= 1:
raise ValueError
except ValueError:
channel.reply("{} is not a valid float between 0 and 1.".format(msg[1]))
return minqlx.RET_STOP_ALL
self.set_cvar("qlx_cleverbotChance", str(chance))
channel.reply("Chance was set to {}".format(chance))
return minqlx.RET_STOP_ALL
@minqlx.thread
def post_data(self, path, text='', channel=None):
"""POSTs data to cleverbot.io
:param path: API path, either ask or create.
:param text: Text to query the the bot with.
:param channel: Channel to reply to(usually chat).
"""
user = self.get_cvar("qlx_cleverbotUser")
key = self.get_cvar("qlx_cleverbotKey")
nick = self.get_cvar("qlx_cleverbotNick")
if nick == "":
self.msg("^3Bot nick cannot be blank.")
return
if user and key:
payload = {"user": user, "key": key, "nick": nick, "text": text}
try:
r = requests.post("https://cleverbot.io/1.0/{}".format(path), data=payload)
r.raise_for_status()
self.callback(r.json(), channel)
return
except requests.exceptions.RequestException as e:
self.logger.error(e)
else:
self.msg("^3You need to set qlx_cleverbotUser and qlx_cleverbotKey")
def callback(self, response, channel=None):
"""Responds to chat with the response from the bot.
Called after data has been POSTed.
:param response: JSON data from cleverbot.io.
:param channel: Channel to reply to.
"""
nick = self.get_cvar("qlx_cleverbotNick")
if "response" in response:
channel.reply("^6{}^7: {}".format(nick, response["response"]))
else:
self.created = True
msg = "^7Bot called ^6{} ^7was created.".format(nick)
if channel:
channel.reply(msg)
else:
self.msg(msg)