-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.rb
309 lines (284 loc) · 8.59 KB
/
bot.rb
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
require 'cinch'
require 'net/http'
require 'json'
require 'time'
require 'forecast_io'
require 'timezone'
# Calculate differences in times
class Numeric
def duration
sec = self.to_i
min = sec / 60
hr = min / 60
day = hr / 24
if day > 0
"#{day} days and #{hr % 24} hours"
elsif hr > 0
"#{hr} hours and #{min % 60} minutes"
elsif min > 0
"#{min} minutes and #{sec % 60} seconds"
elsif sec >= 0
"#{sec} seconds"
else
"It's here!"
end
end
end
# Basic plugin definition
class Hello
# Pull in dem dependencies
include Cinch::Plugin
# Define string patterns and their corresponding methods
match(/help$/, method: :help)
match(/ping$/, method: :ping)
# Define the help method
def help(m)
# Open the current file in the current directory
pwd = File.dirname( File.expand_path(__FILE__))
file = pwd + "/helptext.txt"
help_text = File.open(file, "r")
m.user.send "Hi, #{m.user.name}! I'm a helpful IRC bot coded by Eric Lujan on behalf of the MIT Class of 2019!"
# Send the help text line by line
help_text.each_line do |line|
m.user.send line
end
help_text.close
end
# Define the test method for ping
def ping(m)
m.reply "Tim the Beaver here, reporting for duty!"
end
end
# Class for GitHub querying
class GitHub
include Cinch::Plugin
# Set listeners
match(/gitstatus$/, method: :commit_latest)
# Define the MITBot repository
BaseURL = "api.github.com"
User = "ericluwolf"
Repo = "mitbot"
# Define a way to search for the lastest commit in a repository
def commit_latest(m)
uri = "/repos/#{User}/#{Repo}/commits"
res = request(uri, Net::HTTP::Get)
m.reply "The latest commit on #{User}/#{Repo} is #{res[0]["sha"]}"
commit_search(m, Repo, res[0]["sha"])
end
# Define a way to search for Git commits by ID
def commit_search(m, repo, id)
uri = "/repos/#{User}/#{repo}/commits/#{id}"
# Request the commit from GitHub and store the info
res = request(uri, Net::HTTP::Get)
m.reply "Git commit query for commit #{id} on #{User}/#{repo}"
m.reply "Commit author: #{res["commit"]["author"]["name"]} <#{res["commit"]["author"]["email"]}>"
m.reply "Commit date: #{res["commit"]["author"]["date"]}"
m.reply "Commit message: #{res["commit"]["message"]}"
m.reply "Modified file listing:"
# Iterate through all file statistics
res["files"].each do |file|
m.reply "#{file["filename"]} - #{file["changes"]} changes (#{file["additions"]}+, #{file["deletions"]}-)"
end
end
# Define a generic method to communicate with GitHub's API
private
def request(uri, method, data = nil)
uri = URI("https://#{BaseURL}#{uri}")
# Create an HTTP requst
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
req = method.new(uri.request_uri)
req.body = data
# Do the request and read the JSON into a variable
resp = http.request(req)
# Parse the JSON and return it as an object
return JSON.parse(resp.body)
end
end
end
class Zone
include Cinch::Plugin
# Set listeners
match(/time ([^ ]+)$/, method: :localtime)
match(/time$/, method: :mittime)
# Query the Geolocation API for time zone
def get_timezone(host)
uri = URI("https://freegeoip.net/json/#{host}")
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
req = Net::HTTP::Get.new(uri.request_uri)
req.body = nil
resp = http.request(req)
resp = JSON.parse(resp.body)
return resp["time_zone"]
end
end
# Find the local time for a chat user
def localtime(m, username)
# Get the user's hostname
target = User(username)
host = target.host
# Find timezone of the user based on a geolocation API
zone_name = get_timezone(host)
# Calculate local time in that particular zone
zone = Timezone::Zone.new zone: zone_name
time_there = zone.time(Time.now)
pretty_time = time_there.strftime("%l:%M %p")
# Send that information to the IRC room
m.reply "It is currently #{pretty_time} in #{username}'s time zone (#{zone_name})."
end
# Find the time at MIT
def mittime(m)
zone = Timezone::Zone.new zone: 'America/New_York'
mit_time = zone.time(Time.now)
pretty_time = mit_time.strftime("%-m/%-d/%y %-H:%-M")
m.reply "It is currently #{pretty_time} at MIT."
end
end
# Class for MIT-specific stuff
class MIT
# Moar dependencies
include Cinch::Plugin
# Set listeners
match(/illuminati$/, method: :illuminati)
match(/fact$/, method: :fact)
match(/cpw$/, method: :cpw)
match(/weather$/, method: :weather)
match(/course (.+)$/, method: :course)
# Confirm that the illuminati exists
def illuminati(m)
m.reply "MIT has three letters. Illuminati has three I's. Illuminati confirmed."
end
# Display a random MIT fact
def fact(m)
# Open the current file in the current directory
pwd = File.dirname( File.expand_path(__FILE__))
file = pwd + "/mitfacts.txt"
fact = File.readlines(file).sample
m.reply fact
end
# Show time until CPW
def cpw(m)
cpwtime = 1429142400
now = Time.now.to_i
diff = (cpwtime - now).duration
m.reply "There are #{diff} until the first day of MIT CPW."
end
# Get weather in Cambridge
def weather(m)
ForecastIO.api_key = "8826650e770499ac02d3d72d17afd3c8"
forecast = ForecastIO.forecast(42.3598, -71.0921)
humidity = (forecast.currently.humidity) * 100
response = forecast.currently.summary + ", with a temperature of " + forecast.currently.temperature.to_s + " and humidity of " + humidity.to_s + "%."
m.reply "Current weather at MIT (Kendall/MIT, Cambridge, MA):"
m.reply response
end
def course(m, number)
reply = ""
number = number.downcase
case number
when "1"
reply = "Civil and Environmental Engineering"
when "2"
reply = "Mechanical Engineering"
when "3"
reply = "Materials Science"
when "4"
reply = "Architecture"
when "5"
reply = "Chemistry"
when "6"
reply = "Electrical Engineering and Computer Science"
when "6-1"
reply = "Electrical Science and Engineering"
when "6-2"
reply = "Electrical Engineering and Computer Science"
when "6-3"
reply = "Computer Science and Engineering"
when "7"
reply = "Biology"
when "8"
reply = "Physics"
when "9"
reply = "Brain and Cognitive Sciences"
when "10"
reply = "Chemical Engineering"
when "10b"
reply = "Chemical-Biological Engineering"
when "11"
reply = "Urban Studies and Planning"
when "12"
reply = "Earth, Atmospheric, and Planetary Sciences"
when "14"
reply = "Economics"
when "15"
reply = "Management"
when "16"
reply = "AeroAstro"
when "17"
reply = "Political Science"
when "18"
reply = "Mathematics"
when "18c"
reply = "Mathematics with Computer Science"
when "20"
reply = "Biological Engineering"
when "21"
reply = "Humanities"
when "21a"
reply = "Anthropology"
when "21f"
reply = "Global Studies and Languages"
when "21h"
reply = "History"
when "21l"
reply = "Literature"
when "21m"
reply = "Music and Theater Arts"
when "21w"
reply = "Writing"
when "22"
reply = "Nuclear Science and Engineering"
when "24"
reply = "Linguistics and Philosophy"
when "cms"
reply = "Comparative Media Studies"
when "csb"
reply = "Computational and Systems Biology"
when "esd"
reply = "Engineering Systems"
when "hst"
reply = "Health Sciences and Technology"
when "mas"
reply = "Media Arts and Studies"
when "sts"
reply = "Science, Technology, and Society"
else
reply = "I don't recognize that course number or acronym"
end
m.reply reply
end
end
# Set some basic configuration and define the bot object.
bot = Cinch::Bot.new do
configure do |c|
pwd = File.dirname( File.expand_path(__FILE__))
cred = File.open(pwd + "/config/credfile", &:readline)
c.server = "irc.freenode.net"
c.channels = ["#mit2019"]
c.nick = "mitbot"
c.realname = "Tim the Beaver"
c.user = "mit"
c.password = cred
c.plugins.plugins = [Hello, MIT, GitHub, Zone]
end
on :message do |m|
unless m.user.nick.start_with?('mitbot')
msg = m.message.downcase
words = ['illuminati', 'triangle', 'conspiracy', 'three', 'confirmed', 'secret', 'society', 'chris', 'peterson']
if (words.any? { |word| msg.include? word })
m.reply "Did you hear that?!!!!! Illuminati confirmed by #{m.user.name}!"
end
end
end
end
# Let it run!
bot.start