forked from feabell/statsbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statsbot.py
257 lines (212 loc) · 10.7 KB
/
statsbot.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
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
import os, threading, sys, sqlite3
import eveapi
import zkbapi
import fleetapi
import slackapi
import recruitment
import yaml
import os
import logging
from time import gmtime, strftime
config = yaml.load(open('plugins/stats/statsbot.conf', 'r'))
token = config["SLACK_TOKEN"]
api_client = slackapi.init(token)
#logging.basicConfig(filename='/tmp/statsbot.log', level=logging.INFO)
outputs = []
crontable = []
#poll for new kills every minute
#crontable.append([60, "autokill"])
#poll for new recruits every 1minutes
crontable.append([60, "autorec"])
#poll for members approaching the end of their trial, every 24hours
crontable.append([86400, "autotrial"])
#poll for full bookmarks, every 10 minutes
crontable.append([3600, "autobms"])
#poll for new members in the last 24 hours
#crontable.append([86400, "autonew"])
killChannelId = "C04MCGR8Y"
testChannelId = "C04N5P17B"
commsChannelId = "G6HD049T4"
recruitChannelId = "G04NGUDHF"
generalChannelId = "C04L6NKQ3"
adminChannelId = "GAN5WV672"
logging.info('Statsbot started')
def process_message(data):
#stop the bot from barfing out for events without a text, channel or user chunk (event_ts chunks happen with unfurled links)
if not 'text' in data:
return
if not 'user' in data:
return
if not 'channel' in data:
return
channel = data["channel"]
user = data["user"]
username = slackapi.getFullname(user)
text = data["text"].lower()
unmodified_text = data["text"]
#print data
#if channel.startswith("D"): # or channel.startswith("C"):
if text.startswith("!sb") or text.startswith("!statsbot"):
blob = text.split()
unmodified_blob = unmodified_text.split()
if len(blob) < 2:
outputs.append([channel, "Not enough arguments supplied, type !sb help to see a list of commands."])
return
command = blob[1]
if command.startswith("help"):
logging.info('help command received from ' + username)
outputs.append([channel, "!sb lastkill || shows the lastkill reported in #kills\r\n"+
"!sb events || list events on the WDS in-game calendar\r\n"+
"!sb srp || view the current status of the WDS SRP wallet\r\n"+
"!sb rr <your message here> || post a request for a fleet or forump message to #rapid-response"])
elif command.startswith("lastkill"):
logging.info('lastkill command received from ' + username)
outputs.append([channel, zkbapi.getLastKill()])
elif command.startswith("events"):
logging.info('events command received from ' + username)
outputs.append([channel, eveapi.getEvents()])
elif command.startswith("srp"):
logging.info('srp command received from ' + username)
outputs.append([channel, eveapi.getSRP()])
elif command.startswith("fleet"):
logging.info('fleet command received from ' + username)
if not channel.startswith("D"):
outputs.append([channel, "This command cannot be ran in this channel"])
return
subcomm = blob[2]
if subcomm.startswith("new"):
#grab the description by compounding together all entries after 2
description = ' '.join(blob[3:])
outputs.append([channel, fleetapi.newFleet(username, description)])
elif command.startswith("testme"):
logging.info('testme v2 command received from ' + username)
#slackapi.sendMessage()
#slackapi.sendPM("did this work?", user)
elif command.startswith("admin"):
logging.info('admin command received from ' + username)
if not (channel.startswith(adminChannelId) or slackapi.userInChannel(adminChannelId, user)):
outputs.append([channel, "This command cannot be ran in this channel"])
return
subcomm = blob[2]
if subcomm == "auth":
#code provided
if len(blob) >=4:
slackapi.sendToChannel(eveapi.end_auth(unmodified_blob[3]), channel)
else:
slackapi.sendToChannel(eveapi.start_auth(), channel)
elif command.startswith("recruit"):
logging.info('recruit command received from ' + username)
if not (channel.startswith(recruitChannelId) or slackapi.userInChannel(recruitChannelId, user)):
outputs.append([channel, "This command cannot be ran in this channel"])
return
subcomm = blob[2]
if subcomm == "help":
helptext = "!sb recruit list\r\n"
helptext+= "!sb recruit list <recruits|invited|inducted|rejected|trial|name> <full>\r\n"
helptext+= "!sb recruit list <id> <full>\r\n"
helptext+= "!sb recruit list name <pilot full name> <full>\r\n"
helptext+= "!sb recruit <invite|induct|reject> <id> <reason>\r\n"
outputs.append([channel, helptext])
return
if subcomm == "list":
if len(blob) == 3:
targetcomm = "recruits"
else:
targetcomm = blob[3]
showFull=False
if len(blob) >= 5:
if blob[4] == "full":
showFull=True
if targetcomm.isdigit():
#list a specific recruit
slackapi.sendToChannel(recruitment.list(recid=targetcomm, showfull=showFull), channel)
elif targetcomm == "recruits":
#list all waiting recruits
slackapi.sendToChannel(recruitment.list(recruits=True, showfull=showFull), channel)
elif targetcomm == "invited":
#list invited
slackapi.sendToChannel(recruitment.list(invited=True, showfull=showFull), channel)
elif targetcomm == "inducted":
#list invited
slackapi.sendToChannel(recruitment.list(inducted=True, showfull=showFull), channel)
elif targetcomm == "rejected":
#list invited
slackapi.sendToChannel(recruitment.list(rejected=True, showfull=showFull), channel)
elif targetcomm == "trial":
#list trial users
slackapi.sendToChannel(recruitment.list(trial=True), channel)
elif targetcomm == "endtrial":
#list trial users
slackapi.sendToChannel(recruitment.list(endOfTrial=True), channel)
elif targetcomm == "name":
#list users by name
slackapi.sendToChannel(recruitment.list(findByName=True,showfull=showFull,searchString=' '.join(blob[4:])),channel)
elif subcomm.startswith("induct"):
#mark selected recruits as inducted and needing an invite
if recruitment.update(1, blob[3], username, blob[4:]):
slackapi.sendToChannel('Recruit(s) '+ ''.join(blob[3]) +' marked as inducted by ' + username, recruitChannelId)
else:
slackapi.sendToChannel('Pilot ' + ''.join(blob[3]) + ' does not exist in the recruits database.', recruitChannelId)
elif subcomm.startswith("invite"):
#mark selected recruits as invited
if recruitment.update(2, blob[3], username, blob[4:]):
slackapi.sendToChannel('Recruit(s) '+ ''.join(blob[3]) +' marked as invited by ' + username, recruitChannelId)
else:
slackapi.sendToChannel('Pilot ' + ''.join(blob[3]) + ' does not exist in the recruits database.', recruitChannelId)
elif subcomm.startswith("reject"):
#mark selected recruits as rejected
if recruitment.update(3, blob[3], username, blob[4:]):
slackapi.sendToChannel('Recruit(s) '+ ''.join(blob[3]) +' marked as rejected by ' + username, recruitChannelId)
else:
slackapi.sendToChannel('Pilot ' + ''.join(blob[3]) + ' does not exist in the recruits database.', recruitChannelId)
else:
outputs.append([channel, "Unknown command"])
elif command.startswith("rr"):
logging.info('rr command received from ' + username)
if not channel.startswith("D"):
slackapi.sendPM("You can't send rapid responses from channels, try copying and pasting the message (starting '!sb rr...') in here instead!",
user)
return
currtime = strftime('%H:%M %d-%m', gmtime())
rr = ' '.join(blob[2:])
message = 'Rapid Response by '+username+' at '+currtime+' (eve time):\r\n'+rr
slackapi.sendRR(message)
elif command.startswith("bm"):
logging.info('bm command received from ' + username)
outputs.append([channel, "Current bookmark count:" +str( eveapi.getBookmarkCount() )])
def autokill():
#logging.info("autokill: polling for new kills")
kills = zkbapi.getNewKills()
for kill in kills:
outputs.append([killChannelId, zkbapi.parseKill(kill)])
def autorec():
#logging.info("autorec: polling for new recruits")
dir_path = os.path.dirname(os.path.abspath(__file__))
con = sqlite3.connect(os.path.join(dir_path, 'statsbot.db'))
with con:
cur = con.cursor()
cur.execute('select id from lastrecruitid')
lastRecId = str(cur.fetchone()[0])
recruits = recruitment.getNew(lastRecId)
if len(recruits) >=1:
slackapi.sendToChannel("New recruits!", recruitChannelId)
for recruit in recruits:
recIdInt = str(recruit['id'])
slackapi.sendToChannel(recruitment.list(recid=recIdInt), recruitChannelId)
logging.info("autorec: updating latest recruit to " + recIdInt)
cur.execute('update lastrecruitid set id = '+recIdInt)
con.commit()
def autotrial():
logging.info("autotrial: polling for members nearing the end of their trial period")
slackapi.sendToChannel(recruitment.list(endOfTrial=True), recruitChannelId)
def autonew():
logging.info("autonew: polling for new members in the last 24hours")
new = recruitment.newMembers()
if new:
slackapi.sendToChannel(new,generalChannelId)
def autobms():
logging.info("autobms: checking bookmark levels")
count = eveapi.getBookmarkCount()
#logging.info("count: " +str(count))
if count > 475:
slackapi.sendToChannel("<!here> bookmarks approaching limit ("+str(count)+"/500)", commsChannelId)