forked from PNGEnterprises/jakebot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjakebot.rb
More file actions
194 lines (152 loc) · 4.63 KB
/
jakebot.rb
File metadata and controls
194 lines (152 loc) · 4.63 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
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
require 'cinch'
require 'twitter'
require 'yaml'
require './waaai.rb'
bot_dir = File.expand_path "~/.jakebot"
welcome_messages = {}
responses = {}
phrases = {}
channels = ["#bottest"]
VERSION = '0.2.6'
# Create the storage directory if it doesn't exist
Dir.mkdir(bot_dir) unless File.exists?(bot_dir)
if !(File.exists?("#{bot_dir}/keys") and File.exists?("#{bot_dir}/phrases"))
abort "This bot will not work until files 'phrases' and 'keys' are placed in ~/.jakebot"
end
keys = YAML.load(File.read("#{bot_dir}/keys"))
phrases = YAML.load(File.read("#{bot_dir}/phrases"))
# Load the saved welcome messages, if they exist
if File.exists?("#{bot_dir}/welcome")
welcome_messages = YAML.load_file("#{bot_dir}/welcome")
end
if File.exists? "#{bot_dir}/responses"
responses = YAML.load_file("#{bot_dir}/responses")
end
# Load twitter client
tw_client = Twitter::REST::Client.new do |config|
tw_keys = keys['twitter']
config.consumer_key = tw_keys['consumer_key']
config.consumer_secret = tw_keys['consumer_secret']
config.access_token = tw_keys['access_token']
config.access_token_secret = tw_keys['access_token_secret']
end
# Utility methods
def shorten_any_urls! string
r = /https?:\/\/(.+)/
pieces = string.split(' ')
pieces.each do |piece|
if r =~ piece
string[piece] = Waaai.shorten piece
end
end
return string
end
bot = Cinch::Bot.new do
# Configure the bot
configure do |c|
c.server = "irc.phinugamma.org"
c.channels = channels
c.nick = "jakebot"
c.user = "jakebot"
c.realname = "Jake Mk II Electric Boogaloo"
end
# Register handlers
on :message, /^(hello|hi|yo|hey|greetings|howdy|hola|salutations) jakebot/i do |m| #maybe make a new file for this?
m.reply "#{phrases['greetings'].sample} #{m.user.nick}"
end
on :message, /^!tweet (.+)/i do |m, tw|
tweet = tw_client.update tw
m.reply "#{phrases['affirmatives'].sample} It's been tweeted at #{tweet.url}"
end
on :message, /^!welcome (.+)/i do |m, message|
shorten_any_urls! message
welcome_messages[m.user.nick] = message
m.reply "#{phrases['affirmatives'].sample}"
# Save the messages
IO.write("#{bot_dir}/welcome", YAML.dump(welcome_messages))
end
on :message, /^!kill (.+)/i do |m, victim|
m.reply "Killing #{victim}"
sleep 1
m.reply "pew pew pew"
sleep 0.5
m.reply "#{victim} is dead"
end
on :message, /^!suicide/i do |m|
m.channel.kick(m.user.nick, reason = "suicide")
m.reply "#{m.user} has killed himself"
sleep 0.75
m.reply "R.I.P #{m.user.nick}, you will be missed"
end
on :message, /^!retard/i do |m|
m.reply "im retarded"
end
on :message, /^(.+)$/i do |m, message|
# Save the message or update stats w/e
end
on :join do |m|
# Case of bot joining
if m.user == bot.nick
m.reply "HELLO EVERYONE! I AM JAKEBOT v#{VERSION}"
else
m.channel.op(m.user)
if welcome_messages.key? m.user.nick
m.reply welcome_messages[m.user.nick]
else
m.reply phrases['welcomes'].sample
end
end
end
on :message, /^!respond "(.+)" "(.+)"/i do |m, trigger, response|
trigger.downcase!
if !responses.key? trigger
responses[trigger] = []
end
shorten_any_urls! response
responses[trigger].push response
m.reply "#{phrases['affirmatives'].sample}"
IO.write("#{bot_dir}/responses", YAML.dump(responses))
end
on :message, /^jakebot (.+)/i do |m, message|
message.downcase!
if responses.key? message
m.reply responses[message].sample
end
end
on :message, /^!topic ?add (.+)/i do |m, new_topic|
current_topic = m.channel.topic
shorten_any_urls! new_topic
if current_topic.empty?
m.channel.topic = new_topic
else
m.channel.topic = "#{current_topic} | #{new_topic}"
end
end
on :message, /^!topic ?rem(ove)? (.+)/i do |m, garbage, top|
# Remove an items from the topic
reg = Regexp.new(top, true) # Case insensitive regexp
current_topic = m.channel.topic
topic_segments = current_topic.split(" | ")
topic_segments.each do |t|
if reg =~ t
topic_segments.delete(t)
end
end
new_topic = topic_segments.join(" | ")
if new_topic.eql? current_topic
# If the edited topic and new topic are the same, the
# requested item to delete must not have been found
m.reply "That's not in the topic"
else
m.channel.topic = new_topic
end
end
# Start timers
Timer(3 * 59) { # Every ~3 minutes
if rand < 0.01 # 1% chance
channels.each do |chan| Channel(chan).send(phrases['jakeisms'].sample) end
end
}
end
# Start the bot
bot.start