-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathao-killbot.js
252 lines (223 loc) · 8.17 KB
/
ao-killbot.js
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
/*
* @Author: Mark Arneman
* @Date: 2017-08-18 11:12:18
* @Last Modified by: Mark Arneman
* @Last Modified time: 2019-06-11
*/
// Define static constants
const config = require('./config.json');
// Require modules
const Discord = require('discord.js');
const request = require('request');
if (typeof Discord !== 'undefined') {
const client = new Discord.Client();
}
var lastRecordedKill = -1;
if (typeof config !== 'undefined') {
// Convert all player names to lowercase
var playerNames = [];
for (var i = 0; i < config.players.length; i++) {
playerNames.push(config.players[i].toLowerCase())
}
}
/**
* Fetch recent kills from the Gameinfo API
* @param {Number} limit [max kills to get]
* @param {Number} offset [offset for first kill]
* @return {json} [json array of events]
*/
function fetchKills(limit = 51, offset = 0) {
request({
uri: 'https://gameinfo.albiononline.com/api/gameinfo/events?limit=' + limit + '&offset=' + offset,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
parseKills(body);
} else {
console.log('Error: ', error); // Log the error
}
});
}
/**
* Parse returned JSON from Gameinfo to
* find alliance members on the killboard
* @param {json} events
*/
function parseKills(events) {
var count = 0;
var breaker = lastRecordedKill;
events.some(function (kill, index) {
// Save the most recent kill for tracking
if (index == 0) {
lastRecordedKill = kill.EventId;
}
// Don't process data for the breaker KILL
if (kill.EventId != breaker) {
if (kill.Killer.AllianceName.toLowerCase() == config.allianceName.toLowerCase() || kill.Victim.AllianceName.toLowerCase() == config.allianceName.toLowerCase()) {
// Alliance KILL
postKill(kill);
} else if (kill.Killer.GuildName.toLowerCase() == config.guildName.toLowerCase() || kill.Victim.GuildName.toLowerCase() == config.guildName.toLowerCase()) {
// Guild Kill
postKill(kill);
} else if (playerNames.includes(kill.Killer.Name.toLowerCase()) || playerNames.includes(kill.Victim.Name.toLowerCase())) {
// Player kill
postKill(kill);
}
} else {
count++;
}
return kill.EventId == breaker;
});
// console.log('- Skipped ' + count + ' kills');
}
function postKill(kill, channel = config.botChannel) {
//quick fix to not post kills with 0 fame (like arena kills after the patch)
if (kill.TotalVictimKillFame == 0) {
return;
}
var victory = false;
if (kill.Killer.AllianceName.toLowerCase() == config.allianceName.toLowerCase() ||
kill.Killer.GuildName.toLowerCase() == config.guildName.toLowerCase() ||
config.players.includes(kill.Killer.Name.toLowerCase())) {
victory = true;
}
var assistedBy = "";
if (kill.numberOfParticipants == 1) {
var soloKill = [
'All on their own',
'Without assitance from anyone',
'All by himself',
'SOLO KILL'
];
assistedBy = soloKill[Math.floor(Math.random() * soloKill.length)];
} else {
var assists = [];
kill.Participants.forEach(function (participant) {
if (participant.Name != kill.Killer.Name) {
assists.push(participant.Name);
}
})
assistedBy = "Assisted By: " + assists.join(', ');
}
itemCount = 0;
kill.Victim.Inventory.forEach(function (inventory) {
if (inventory !== null) {
itemCount++;
}
});
var itemsDestroyedText = "";
if (itemCount > 0) {
itemsDestroyedText = " destroying " + itemCount + " items";
}
var embed = {
color: victory ? 0x008000 : 0x800000,
author: {
name: kill.Killer.Name + " killed " + kill.Victim.Name,
icon_url: victory ? 'https://i.imgur.com/CeqX0CY.png' : 'https://albiononline.com/assets/images/killboard/kill__date.png',
url: 'https://albiononline.com/en/killboard/kill/' + kill.EventId
},
title: assistedBy + itemsDestroyedText,
description: 'Gaining ' + kill.TotalVictimKillFame + ' fame',
thumbnail: {
url: (kill.Killer.Equipment.MainHand.Type ? 'https://gameinfo.albiononline.com/api/gameinfo/items/' + kill.Killer.Equipment.MainHand.Type + '.png' : 'https://albiononline.com/assets/images/killboard/kill__date.png')
},
timestamp: kill.TimeStamp,
fields: [{
name: "Killer Guild",
value: (kill.Killer.AllianceName ? "[" + kill.Killer.AllianceName + "] " : '') + (kill.Killer.GuildName ? kill.Killer.GuildName : '<none>'),
inline: true
},
{
name: "Victim Guild",
value: (kill.Victim.AllianceName ? "[" + kill.Victim.AllianceName + "] " : '') + (kill.Victim.GuildName ? kill.Victim.GuildName : '<none>'),
inline: true
},
{
name: "Killer IP",
value: kill.Killer.AverageItemPower.toFixed(2),
inline: true
},
{
name: "Victim IP",
value: kill.Victim.AverageItemPower.toFixed(2),
inline: true
},
],
footer: {
text: "Kill #" + kill.EventId
}
};
console.log(embed);
client.channels.get(channel).send({
embed: embed
});
}
/**
* Wait until ready and logged in
* If we do not wait for the ready event
* All commands will process before we are authorized
*/
if (typeof client !== 'undefined') {
client.on('ready', () => {
console.log('Ready and waiting!');
// If the config.username differs, change it
if (client.user.username != config.username) {
client.user.setUsername(config.username);
}
// Set 'Playing Game' in discord
client.user.setActivity(config.playingGame); // broken due to discord API changes
fetchKills();
// Fetch kills every 30s
var timer = setInterval(function () {
fetchKills();
}, 30000);
});
}
/**
* On receive message
*/
if (typeof client !== 'undefined') {
client.on('message', message => {
if (message.content.indexOf(config.cmdPrefix) !== 0 || message.author.bot) return;
else { // Execute command!
var args = message.content.slice(config.cmdPrefix.length).trim().split(/ +/g);
var command = args.shift().toLowerCase();
// Test Command - !ping
if (command === 'ping') {
message.reply('pong');
} else if (command === 'kbinfo') {
request({
json: true,
uri: 'https://gameinfo.albiononline.com/api/gameinfo/events/' + args[0]
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
postKill(body, message.channel.id);
} else {
console.log('Error: ', error); // Log the error
}
});
}
// [ADMIN] - clear config.botChannel messages
else if (command === 'kbclear') {
if (config.admins.includes(message.author.id) && message.channel.id == config.botChannel) {
message.channel.send('Clearing Killboard').then(msg => {
msg.channel.fetchMessages().then(messages => {
message.channel.bulkDelete(messages);
console.log("[ADMIN] " + message.author.username + " cleared Killboard");
})
})
}
}
}
});
}
if (typeof config !== 'undefined') {
if (config.token) {
client.login(config.token);
} else {
console.log("ERROR: No bot token defined")
}
} else {
console.log("ERROR: No config file")
console.log("execute: cp config.json.example config.json")
}