-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
394 lines (289 loc) · 12 KB
/
bot.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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# bot.py
import os
import time
import discord
from dotenv import load_dotenv
import requests
from requests.exceptions import HTTPError, Timeout
import json
from datetime import datetime
import boto3
load_dotenv()
BOOST_TABLE_NAME = os.getenv('GAMESERVER_BOOST_TABLE_NAME')
DB_REGION = 'us-west-2'
# Helper class to send Discord API requests
class DiscordHelper:
TOKEN = os.getenv('DISCORD_TOKEN')
BOT_CLIENT_ID = os.getenv('BOT_CLIENT_ID')
CHANNEL_ID = os.getenv('DISCORD_CHANNEL_ID')
DISCORD_BASE_URL = os.getenv('DISCORD_BASE_URL')
DISCORD_MESSAGE_HISTORY = os.getenv('DISCORD_MESSAGE_HISTORY')
DISCORD_CREATE_MESSAGE = os.getenv('DISCORD_CREATE_MESSAGE')
DISCORD_EDIT_MESSAGE = os.getenv('DISCORD_EDIT_MESSAGE')
def __init__(self):
if self.TOKEN is None:
raise Exception('Missing Discord Token')
elif self.BOT_CLIENT_ID is None:
raise Exception('Missing Bot Client ID')
elif self.CHANNEL_ID is None:
raise Exception('Missing Discord Channel ID')
elif self.DISCORD_BASE_URL is None:
raise Exception('Missing Discord Base URL')
elif self.DISCORD_MESSAGE_HISTORY is None:
raise Exception('Missing Discord Message History Path')
elif self.DISCORD_CREATE_MESSAGE is None:
raise Exception('Missing Discord Create Message Path')
elif self.DISCORD_EDIT_MESSAGE is None:
raise Exception('Missing Discord Edit Message Path')
# Send a Discord API request
def sendDiscordRequest(self, action, url, *params):
# Do request here
auth_token = f'Bot {self.TOKEN}'
json_body = None
if params is not None and params:
json_body = params[0]
try:
if action == 'GET':
response = requests.get(url, timeout=5, headers={"Authorization": auth_token})
elif action == 'POST':
response = requests.post(url, timeout=5, headers={"Authorization": auth_token}, json=json_body)
elif action == 'PATCH':
response = requests.patch(url, timeout=5, headers={"Authorization": auth_token}, json=json_body)
else:
raise Exception(f'Attempting to send request with unknown action: {action}')
response.raise_for_status()
except HTTPError as http_err:
print(f'HTTP error occurred for {id}: {http_err}')
except Timeout as timeout:
print(f'HTTP timeout occurred for {id}: {timeout}')
except Exception as err:
print(f'Other error occurred for {id}: {err}')
else:
return response.json()
return None
# Get message ID of most recent message sent by bot
def getLatestMessageID(self):
# Make request for message history
# Parse message ID
url = self.DISCORD_BASE_URL + self.DISCORD_MESSAGE_HISTORY.replace(':channel_id', self.CHANNEL_ID, 1)
response = self.sendDiscordRequest('GET', url)
if response is None:
return None
for message in response:
if message["author"]["id"] != self.BOT_CLIENT_ID:
continue
return message["id"]
return None
# Edit an existing Rich Embed message
def editMessage(self, message_id, embed):
# Make request to edit message
url = self.DISCORD_BASE_URL + self.DISCORD_EDIT_MESSAGE.replace(':channel_id', self.CHANNEL_ID, 1).replace(
':message_id', message_id, 1)
response = self.sendDiscordRequest('PATCH', url, {"embed": embed})
if response is None:
return None
return response
# Create a new Rich Embed message
def createMessage(self, embed):
# Make request to edit message
url = self.DISCORD_BASE_URL + self.DISCORD_CREATE_MESSAGE.replace(':channel_id', self.CHANNEL_ID, 1)
response = self.sendDiscordRequest('POST', url, {"embed": embed})
if response is None:
return None
return response
def intialConnection(self):
client = discord.Client()
@client.event
async def on_ready():
print('Closing connection')
await client.close()
return None
client.run(self.TOKEN)
class NitradoHelper:
NITRAPI_BASE_URL = None
NITRAPI_GAMESERVER_BOOST_HISTORY = None
def __init__(self):
self.NITRAPI_BASE_URL = os.getenv('NITRAPI_BASE_URL')
self.NITRAPI_GAMESERVER_BOOST_HISTORY = os.getenv('NITRAPI_GAMESERVER_BOOST_HISTORY')
if self.NITRAPI_BASE_URL is None:
raise Exception('Missing NITRAPI_BASE_URL')
if self.NITRAPI_GAMESERVER_BOOST_HISTORY is None:
raise Exception('Missing NITRAPI_GAMESERVER_BOOST_HISTORY')
def getBoostHistory(self, gameserver_id, token):
url = self.NITRAPI_BASE_URL + self.NITRAPI_GAMESERVER_BOOST_HISTORY.replace(':id', gameserver_id, 1)
auth_token = f'Bearer {token}'
try:
response = requests.get(url, timeout=3, headers={"Authorization": auth_token})
response.raise_for_status()
except HTTPError as http_err:
print(f'HTTP error occurred for {id}: {http_err}')
except Timeout as timeout:
print(f'HTTP timeout occurred for {id}: {timeout}')
except Exception as err:
print(f'Other error occurred for {id}: {err}')
else:
return response.json()
return None
class DBHelper:
DB = None
def __init__(self, db_type, db_region):
self.DB = boto3.resource(db_type, region_name=db_region)
def getDocument(self, table_name, key, val):
table = self.DB.Table(table_name)
item = table.get_item(Key={key: val})
if not item:
return None
if 'Item' not in item:
return None
return item['Item']
def createDocument(self, table_name, doc):
table = self.DB.Table(table_name)
response = table.put_item(Item=doc)
return response
def updateDocument(self, table_name, key_dict, update_expression, expression_attribute_values):
table = self.DB.Table(table_name)
response = table.update_item(Key=key_dict, UpdateExpression=update_expression, ExpressionAttributeValues=expression_attribute_values, ReturnValues="UPDATED_NEW")
return response
# Handler for AWS Lambda to run the application
def handler(event, context):
nitrado_helper = NitradoHelper()
discord_helper = DiscordHelper()
db_helper = DBHelper('dynamodb', 'us-west-2')
if event == "initial-connection":
discord_helper.intialConnection()
return {
'message': "Successfully initialized connection"
}
with open('nitrapi_account_config.json') as json_file:
nitrapi_account_config = json.load(json_file)
with open('gameserver_colors.json') as json_file:
gameserver_colors = json.load(json_file)
nitrapi_config = json.loads(nitrapi_account_config)
boost_history_all_accounts = []
for account in nitrapi_config['nitrado_accounts']:
auth_token = account["auth_token"]
for gameserver in account["gameservers"]:
if not gameserver["enabled"]:
continue
gameserver_name = gameserver["gameserver_name"]
gameserver_id = gameserver['gameserver_id']
boost_history = nitrado_helper.getBoostHistory(gameserver_id, auth_token)
parsed_boost_history = parseBoostHistory(boost_history)
boost_history_all_accounts.append({
"gameserver_name": gameserver_name,
"gameserver_id": gameserver_id,
"boosts": parsed_boost_history
})
total_new_boosts = 0
for gameserver_boost in boost_history_all_accounts:
gameserver_id = gameserver_boost["gameserver_id"]
gameserver_name = gameserver_boost["gameserver_name"]
item = db_helper.getDocument(BOOST_TABLE_NAME, "gameserver_id", int(gameserver_id))
if not gameserver_boost['boosts']:
continue
db_boosts = []
if item and 'boosts' in item:
db_boosts = json.loads(item['boosts'])
new_gameserver_boosts = []
for boost in gameserver_boost['boosts']:
if not boostInList(boost, db_boosts):
new_gameserver_boosts.append(boost)
if not new_gameserver_boosts:
continue
new_db_boosts = []
for boost in new_gameserver_boosts:
embed = generateEmbed(gameserver_name, boost, gameserver_colors)
dict_embed = embed.to_dict()
if event == "slow-mode":
time.sleep(5)
response = discord_helper.createMessage(dict_embed)
total_new_boosts += 1
if not response:
continue
new_db_boosts.append(boost)
if not new_db_boosts:
continue
if not item:
doc = {
"gameserver_id": int(gameserver_id),
"boosts": json.dumps(new_db_boosts)
}
db_helper.createDocument(BOOST_TABLE_NAME, doc)
elif 'boosts' not in item or not item["boosts"]:
db_helper.updateDocument(
BOOST_TABLE_NAME,
{"gameserver_id": int(gameserver_id)},
"set boosts = :b",
{":b": json.dumps(new_db_boosts)}
)
elif item["boosts"]:
combined_db_boosts = db_boosts + new_db_boosts
db_helper.updateDocument(
BOOST_TABLE_NAME,
{"gameserver_id": int(gameserver_id)},
"set boosts = :b",
{":b": json.dumps(combined_db_boosts)}
)
else:
print("Unsure how to save boost data")
if event == "slow-mode":
print('Exiting for slow mode')
return {
'message': "Success in slow mode"
}
return {
'message': "Success",
'new_boosts': total_new_boosts
}
def parseBoostHistory(boost_history):
if boost_history is None:
return None
if boost_history['status'] != 'success':
return None
if not boost_history['data']:
return None
if not boost_history['data']['boosts']:
return None
return boost_history['data']['boosts']
def boostInList(boost, db_boosts):
for db_boost in db_boosts:
if boost['username'] == db_boost['username'] and boost['boosted_at'] == db_boost['boosted_at']:
return True
return False
def generateEmbed(gameserver_name, boost, gameserver_colors):
booster = boost['username']
boosted_at = datetime.strptime(boost['boosted_at'], "%Y-%m-%dT%H:%M:%S")
boost_message = None
if boost['message']:
boost_message = boost['message']
day_in_seconds = 86400
days_boosted = round(boost['extended_for'] / day_in_seconds, 1)
day_text = 'day'
if days_boosted >= 2:
day_text = 'days'
color = getGameserverColor(gameserver_name, gameserver_colors)
server_icon = os.getenv('SERVER_ICON')
embed = discord.Embed(title=f'{gameserver_name} BOOSTED!',
colour=discord.Colour(color),
description=f'{gameserver_name} has been boosted by **{booster}** for **{days_boosted} {day_text}**!\n\n')
embed.set_footer(text="Boosted",
icon_url=server_icon)
if boost_message:
embed.add_field(name='Boost Message', value=boost_message, inline=True)
embed.__setattr__('timestamp', boosted_at)
return embed
def getGameserverColor(gameserver_name, gameserver_colors):
default_color = "0x4a90e2"
if not gameserver_colors:
return int(default_color, base=16)
color_string = ""
if gameserver_name in gameserver_colors:
color_string = gameserver_colors[gameserver_name]
if not color_string:
return int(default_color, base=16)
hex_int = int(color_string, base=16)
return hex_int
# FOR TESTING
# handler(None, None)
# handler('initial-connection', None)
# handler('slow-mode', None)