-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
297 lines (252 loc) · 10.5 KB
/
index.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
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
// first thing is to get my config.json file up and ready
const {discordToken, riotAPIToken, sqlDBStuff, LOLPlayer} = require('./config.json')
// this is for discord stuff
const Discord = require('discord.js');
const client = new Discord.Client();
// this is for league stuff
const _kayn = require('kayn')
const Kayn = _kayn.Kayn
const REGIONS = _kayn.REGIONS
const kayn = Kayn(riotAPIToken)({
region: REGIONS.EUROPE_WEST,
apiURLPrefix: 'https://%s.api.riotgames.com',
locale: 'en_US',
debugOptions: {
isEnabled: true,
showKey: false,
},
requestOptions: {
shouldRetry: true,
numberOfRetriesBeforeAbort: 3,
delayBeforeRetry: 1000,
burst: false,
shouldExitOn403: false,
},
cacheOptions: {
cache: null,
timeToLives: {
useDefault: false,
byGroup: {},
byMethod: {},
},
},
})
var mysql = require('mysql');
var pool = mysql.createPool({
host : sqlDBStuff.host,
user : sqlDBStuff.user,
password : sqlDBStuff.password,
database : sqlDBStuff.database
});
// smaple code i wi;ll use later as an example on hoq to do it myself
// having some problems with connection.connect() and connection.end()
// connection.connect();
// connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
// if (error) throw error;
// console.log('The solution is: ', results[0].solution);
// });
// connection.end();
pool.query('DROP TABLE IF EXISTS related', (error) => {
if (error) throw error;
});
pool.query(`CREATE TABLE related(
leagueID VARCHAR(60),
discordSnowflake VARCHAR(20),
PRIMARY KEY(leagueID)
)`, (error) => {
if (error) throw error;
});
pool.query('DROP TABLE IF EXISTS reports', error => {
if (error) throw error;
})
pool.query(`CREATE TABLE reports(
discordSnowflake VARCHAR(20),
reported INT,
times INT
PRIMARY KEY(discordSnowflake)
)`)
// start hte bot part
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', async msg => {
let input = msg.content.split(" ")
let command = input.shift()
if (command === 'ping') {
msg.reply('Pong!');
}else if (command === '!move'){
let memberList = []
memberList.push(msg.member)
msg.guild.channels.create('new-voice', {
type: 'voice'
}).then(voiceChannel => {
// console.log('yass')
for (let member of memberList){
member.voice.setChannel(voiceChannel)
}
})
// var yass = msg.member
// yass.voice.setChannel(otherChannelID)
}else if (command === "!register"){
// important to note that whats in use is id and not accountid. id is returned by the spectator api so this is what i use. also id is region specific and therefore someone cna have the same id in a different region
const leagueName = input[0]
const summObj = await kayn.Summoner.by.name(leagueName)
const leagueID = summObj.id
console.log(`${leagueName} has leagueID of ${leagueID}`)
// sql entry into db
pool.query(`INSERT INTO related VALUES ( ? , ? );
INSERT INTO reports VALUES ( ?, ?, ?)`, [leagueID, msg.author.id, msg.author.id, 0, 0],
(error) => {
if (error){
if (error.code = "ER_DUP_ENTRY"){
msg.member.send(`\`${leagueName}\` is already associated with a discord account`)
}else{
console.log(error)
}
}else{
msg.member.send(`You have registered \`${leagueName}\` with your account.`)
}
}
)
}else if(command === "!deregister"){
const leagueName = input[0]
const summObj = await kayn.Summoner.by.name(leagueName)
const leagueID = summObj.id
pool.query(`SELECT discordSnowflake FROM related
WHERE leagueID = ?`,
[leagueID],
(error, results, fields) => {
console.log(results.length)
if (error){
console.log(error)
}else if (results.length != 0){
console.log(results[0].discordSnowflake)
if(results[0].discordSnowflake === msg.author.id){
pool.query(`DELETE FROM related
WHERE leagueID = ?`, [leagueID], (error) => {
if (error){
console.log(error)
}else{
msg.member.send(`\`${leagueName}\` has been deregistered from this discord account`)
}
})
}else{
msg.member.send(`You cannot register \`${leagueName}\` from their discord account`)
}
}
})
}else if(command === "!voice"){
console.log(msg.member.voice)
}else if (command === "!match"){
if(msg.member.voice.sessionID === undefined){
msg.member.send('You must first join a lobby voice channel before using this command')
return
}
pool.query(`SELECT leagueID FROM related WHERE discordSnowflake = ?`, msg.member.id, (error, results, fields) => {
if (results[0] === undefined){
msg.member.send('You must first register your League of Legends username to your discord account')
return
}
kayn.CurrentGame.by.summonerID(results[0].leagueID).then(matchData => {
// first step is check in case the vc has already been created
for (let vc of msg.guild.channels){
if (vc[1].name === matchData.gameId){
msg.member.voice.setChannel(vc[0])
return
}
}
// this occurs if a vc for the game hasnt been created
console.log(matchData)
let msgersLeagueID = results[0].leagueID
let teamLeagueIDList= []
let teamID;
for (let player of matchData.participants){
if (player.summonerId === msgersLeagueID){
teamID = player.teamId
break
}
}
for (let player of matchData.participants){
if(player.teamId === teamID && player.summonerId != msgersLeagueID){
teamLeagueIDList.push(player.summonerId)
}
}
pool.query(`SELECT discordSnowflake FROM related WHERE leaugeID="` + teamLeagueIDList.join(`" OR leagueID="`) + `"`, (error, results, fields) => {
if (error){
console.log(error)
return
}
console.log(results, fields)
if (results.length === 0) {
msg.member.send('Noone else from your game is registered with this service')
return
}
msg.guild.channels.create(matchData.gameId,{
type: 'voice'
}).then(voiceChannel => {
let reformattedResults = []
for (let discordSnowflakeContainer of results){
reformattedResults.push(discordSnowflakeContainer.discordSnowflake)
}
let otherMembers = msg.guild.members.fetch(reformattedResults).then( (first, second) => {
console.log(otherMembers)
for (let member of first){
if(member.voice.sessionID){
member.voice.setChannel(voiceChannel)
}
}
msg.member.voice.setChannel(voiceChannel)
pool.query(`UPDATE reports SET times = times + 1 WHERE discordSnowflake = "` + msg.member.id + `" OR discordSnowflake = "` + reformattedResults.join(`" OR discordSnowflake = "`) + `"`,
(error) => {
if (error){
console.log(error)
}
})
})
})
})
}).catch(thing => {
if (JSON.parse(thing.error.response.body).status.message === "Data not found"){
msg.member.send('It does not appear you are in a game at the moment')
}else{
console.log(thing)
}
})
})
}else if(command === "!cache"){
for(let channel of msg.guild.channels.cache){
console.log(channel[1].name)
}
}else if (command === "!report"){
let reportee = input[0]
let gameId = msg.member.voice.channel.name
kayn.Match.get(gameId).then(mathData => {
for (let player of mathData.participants){
if (reportee === player.name){
pool.query(`SELECT discordSnowflake FROM related WHERE leagueID = ?`, [player.summonerId], (error, results, fields) => {
if (error){
console.log(error)
return
}
if (results[0] == undefined){
msg.member.send(`It seems that \`${reportee}\` was not in a game with you`)
return
}
pool.query(`UPDATE reports SET reported = reported + 1 WHERE discordSnowflake = ?`, [results[0].discordSnowflake], (error, results, fields) => {
if (error){
console.log(error)
return
}
msg.member.send(`Thank you for reporting \`${reportee}\``)
})
})
return
}
}
msg.member.send('You cannot report someone who is not or was not in the voice channel you are currently in')
}).catch(error => {
console.log(error)
})
}
});
client.login(discordToken);