10
10
from karma .karma_rate import KarmaRateLimiter
11
11
from functions .welcome_machine import WelcomeMachine
12
12
from functions .dice_roller import DiceRoller
13
+ from functions .reddit import RedditManager
13
14
14
15
class IRCBot (irc .IRCClient ):
15
16
"""Python Twisted IRC BOT. irc.IRCClient specialization."""
@@ -32,6 +33,7 @@ def connectionMade(self):
32
33
)
33
34
self .karma_manager = KarmaManager (self .logger )
34
35
self .karmrator = KarmaRateLimiter ()
36
+ self .reddit = RedditManager ()
35
37
# Singleton WelcomeMachine class
36
38
self .welcome_machine = WelcomeMachine (self .factory .cm .greetings_file_path )
37
39
@@ -78,7 +80,10 @@ def privmsg(self, user, channel, msg):
78
80
79
81
# Check if you are talking with BOT
80
82
if nickname_pattern .match (msg ):
81
- self .msg (channel , "%s: I am BOT, do not waste your time!" % user )
83
+ #self.msg(channel, "%s: I am BOT, do not waste your time!" % user)
84
+ deferred_reddit = threads .deferToThread (self .reddit .retrieve_hot , rand = True , nick = user )
85
+ deferred_reddit .addCallback (self .threadSafeMsg )
86
+
82
87
elif msg .startswith ('!' ):
83
88
self .evaluate_command (user , channel , msg )
84
89
elif re .match (re .compile ('\w+\+\+$|\w+--$' ), msg ):
@@ -102,6 +107,7 @@ def evaluate_command(self, user, channel, msg):
102
107
# dice command !roll NdF with Faces = 3|4|6|8|10|20
103
108
dice_pattern = re .compile ("!roll\s\d+d([3468]|10|20)$" , flags = re .IGNORECASE )
104
109
rand_pattern = re .compile ("!rand\s\d+$" , flags = re .IGNORECASE )
110
+ reddit_pattern = re .compile ("!reddit\s?(\d+|\d+\s\w+)?$" , flags = re .IGNORECASE )
105
111
msg_splits = msg .split ()
106
112
107
113
# check for commands starting with bang!
@@ -123,6 +129,19 @@ def evaluate_command(self, user, channel, msg):
123
129
deferred_roll = threads .deferToThread (DiceRoller .roll , msg )
124
130
deferred_roll .addCallback (self .threadSafeMsg )
125
131
132
+ elif reddit_pattern .match (msg ):
133
+ deferred_reddit = None
134
+ if len (msg_splits ) == 1 :
135
+ deferred_reddit = threads .deferToThread (self .reddit .retrieve_hot )
136
+ elif len (msg_splits ) == 2 :
137
+ deferred_reddit = threads .deferToThread (self .reddit .retrieve_hot , num_entries = int (msg_splits [1 ]))
138
+ elif len (msg_splits ) == 3 :
139
+ deferred_reddit = threads .deferToThread (self .reddit .retrieve_hot , num_entries = int (msg_splits [1 ]), subject = msg_splits [2 ])
140
+
141
+ if deferred_reddit is not None :
142
+ deferred_reddit .addCallback (self .threadSafeMsg )
143
+
144
+
126
145
elif rand_pattern .match (msg ):
127
146
if len (msg_splits ) == 2 :
128
147
self .msg (channel , "Random number: %d" % (random .randint (0 , int (msg_splits [1 ]) )) )
@@ -193,7 +212,12 @@ def threadSafeMsg(self, message):
193
212
194
213
# see https://github.com/zencoders/pyircbot/issues/3
195
214
chan = "#" + self .factory .channel
196
- reactor .callFromThread (self .msg , chan , message )
215
+ if type (message ) is str :
216
+ reactor .callFromThread (self .msg , chan , message )
217
+ elif type (message ) is list :
218
+ for el in message :
219
+ reactor .callFromThread (self .msg , chan , str (el ))
220
+
197
221
198
222
def get_current_timestamp (self ):
199
223
return time .asctime (time .localtime (time .time ()))
@@ -202,22 +226,25 @@ def _help_command(self, command=None):
202
226
203
227
"""This method returns the help message"""
204
228
205
- help_msg = "Valid commands: !help <command>, !commands, !karma [user], !roll Nd(3|4|6|8|10|20), !rand arg, !randtime"
229
+ help_msg = "Valid commands: !help <command>, !commands, !karma [user], !roll Nd(3|4|6|8|10|20), !rand arg, !randtime, !reddit [entries] [subject] "
206
230
if command is not None :
207
231
208
232
if command == "karma" :
209
233
help_msg = "!karma [user]: returns user's karma score. "
210
234
help_msg += "<user>++ or <user>-- to modify karma score of the specified user."
211
235
212
236
elif command == "roll" :
213
- help_msg = "!roll NdN: roll dice (like D&D: d2 , d4, d6, d8, d10, d20)"
237
+ help_msg = "!roll NdN: roll dice (like D&D: d3 , d4, d6, d8, d10, d20)"
214
238
215
239
elif command == "rand" :
216
240
help_msg = "!rand returns randint(0, arg)"
217
241
218
242
elif command == "randtime" :
219
243
help_msg = "!randtime returns a random 24h (HH:MM) timestamp"
220
244
245
+ elif command == "reddit" :
246
+ help_msg = "!reddit [NUMBER] [SUBJECT] to retrieve the latest NUMBER hot reddit news about SUBJECT, otherwise list the last 3 about computer programming"
247
+
221
248
else :
222
249
help_msg = "%s is not a valid command!" % command
223
250
0 commit comments