This repository has been archived by the owner on Aug 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
179 lines (153 loc) · 7.17 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
const fetch = require('node-fetch');
const
appID =
{
youtube : '880218394199220334',
poker : '755827207812677713',
betrayal : '773336526917861400',
fishing : '814288819477020702',
chess : '832012774040141894',
letterLeague : '879863686565621790',
wordSnack : '879863976006127627',
doodleCrew : '878067389634314250',
awkword : '879863881349087252',
spellCast : '852509694341283871',
checkers : '832013003968348200',
sketchHeads : '902271654783242291',
blazing8s: '832025144389533716',
landIO: '903769130790969345',
puttParty: '945737671223947305',
bobbleLeague: '947957217959759964',
askAway: '976052223358406656',
kwim: '950505761862189096'
},
appNames =
[
'youtube',
'poker',
'betrayal',
'fishing',
'chess',
'letterLeague',
'wordSnack',
'awkword',
'doodleCrew',
'spellCast',
'checkers',
'sketchHeads',
'blazing8s',
'landIO',
'puttParty',
'bobbleLeague',
'askAway',
'kwim'
],
errors =
{
no :
{
token : 'No token was provided !\n',
vc : 'No Voice Channel was provided !\n',
gameName : 'No Game name was provided !\n',
time : 'No time limit was given to the game event !\n'
},
invalid :
{
token : 'Invalid bot token !\n',
vc : 'Invalid Voice Chnanel !\n',
gameName : 'Invalid game name. Game name can only be any of the these\n' + appNames.join(' ') + '\n',
time : 'The time limit should be more than 0 and be in the form of a number in hours !\n'
},
exccedTime : 'The time limit cannot be more than a week, the maximum time limit is \'168 hours\'\nIf you don\'t want your link to get expired, you can set the \'neverExpire\' parameter to true in the options of the constructor\n',
missingPerms : 'The bot doesn\'t have the permission to create invites to the channel.\n'
};
class DiscordGames
{
/**
* @typedef {'youtube' | 'poker' | 'betrayal' | 'fishing' | 'chess' | 'letterLeague' | 'wordSnack' | 'awkword' | 'doodleCrew' | 'spellCast' | 'checkers' | 'sketchHeads' | 'blazing8s' | 'landIO' | 'puttParty' | 'bobbleLeague' | 'askAway' | 'kwim'} NameOfTheGame
* @typedef { {code: String, inviteLink: String, createdAt: Date, validTill: Date | String, guild: { ID: String, name: String }, channel: { ID: String, name: String }, inviter: { ID: String, name: String }, app: { ID: String, name: String, description: String, summary: String, maxMembers: String, icon: String}} } result
*/
/**
* @param {String} BotToken Your Discord Bot's Token goes in here
* @param {NameOfTheGame} GameName Name of the game goes in here
* @param {Number} MaxDuration Maximum duration (number in hours) of the game event goes in here
* @param {{neverExpire: Boolean}=} Options
*/
constructor(BotToken, GameName, MaxDuration, Options)
{
this.BotToken = BotToken;
this.GameName = GameName;
this.MaxDuration = MaxDuration;
if (Options) { this.neverExpire = Options.neverExpire; }
else { this.neverExpire = null; }
};
/**
* @returns {String} A list of the available games
*/
gameNames() { return appNames.join('\n'); };
/**
* Creates an invite to play the game in a voice channel
* @param {Discord.VoiceChannel} VoiceChannel
* @returns {Promise<result>} A parsed JSON object { . . . . . }
*/
play(VoiceChannel)
{
return new Promise((result) =>
{
if (this.BotToken === undefined) throw new Error(errors.no.token);
if (typeof(this.BotToken) !== 'string') throw new Error(errors.invalid.token);
if (this.GameName === undefined) throw new Error(errors.no.gameName);
if (this.GameName && appNames.includes(this.GameName) === false) throw new Error(errors.invalid.gameName);
if (this.MaxDuration === undefined) throw new Error(errors.no.time);
if (typeof(this.MaxDuration) === 'number' && this.MaxDuration > 168) throw new Error(errors.exccedTime);
if (typeof(this.MaxDuration) === 'number' && this.MaxDuration === 0) throw new Error(errors.invalid.time);
if (typeof(this.MaxDuration) !== 'number') throw new Error(errors.invalid.time);
if ([undefined, null].includes(VoiceChannel)) throw new Error(errors.no.vc);
if (VoiceChannel.id === undefined) throw new Error(errors.invalid.vc);
let time = this.MaxDuration;
if (this.neverExpire === true) { time = 0; }
const body_data = { max_age: time * 3600, target_application_id: appID[this.GameName], target_type: 2, temporary: false };
const body = JSON.stringify(body_data);
const headers = { authorization: 'Bot ' + this.BotToken, 'content-type' : 'application/json' };
const URL = 'https://discord.com/api/v10/channels/' + VoiceChannel.id + '/invites';
fetch.default(URL, { method: 'POST', body: body, headers: headers }).then(x => x.json()).then((data) =>
{
if (data.message === '401: Unauthorized') throw new Error(data.message + '. ' + errors.invalid.token);
if (data.message === 'Missing Permissions') throw new Error(data.message + '. ' + errors.missingPerms);
const finalResult =
{
code : data.code,
inviteLink : 'https://discord.gg/' + data.code,
createdAt : new Date(data.created_at),
validTill : time === 0 ? 'This invite link will not expire automatically, because `neverExpire` was set to `true`' : new Date(data.expires_at),
guild :
{
ID : data.guild.id,
name : data.guild.name
},
channel :
{
ID : data.channel.id,
name : data.channel.name
},
inviter :
{
ID : data.inviter.id,
name : data.inviter.username
},
app :
{
ID : data.target_application.id,
name : data.target_application.name,
description : data.target_application.description,
summary : data.target_application.summary,
maxMembers : data.target_application.max_participants === null ? 'null' : data.target_application.max_participants,
icon : data.target_application.icon
}
};
result(finalResult);
});
});
};
};
module.exports = DiscordGames;