This repository has been archived by the owner on Nov 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MyClient.js
179 lines (157 loc) · 3.65 KB
/
MyClient.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 isNullOrUndefined = require('util').isNullOrUndefined;
const discord = require('discord.js');
const fs = require('fs')
const inquirer = require('inquirer');
const request = require('request');
const ConfigManager = require('./ConfigManager').ConfigManager;
module.exports = class MyClient {
static getInstance() {
if (isNullOrUndefined(MyClient.instance)) {
MyClient.instance = new MyClient();
}
return MyClient.instance;
}
constructor() {
this.ready = false;
this.client = new discord.Client({ syncGuilds: true });
ConfigManager.getToken().then((token) => {
this.client.login(token).catch((err) => {
console.log("No token setted or Bad token ! Please login with the option --login or set-token");
process.exit();
});
});
// process.on("exit", this.client.destroy);
// process.on("SIGINT", this.client.destroy);
}
async onReady() {
return new Promise((resolve, reject) => {
if (this.ready)
resolve();
else {
this.client.once('ready', () => {
console.log('Connected')
this.ready = true;
this.client.syncGuilds();
resolve();
});
}
});
}
getServers() {
let servList = [];
for (let item of this.client.guilds) {
servList.push(item[1]);
}
return servList;
}
async getUsersByServer(server) {
await server.fetchMembers();
let list = [];
for (let user of server.members) {
user = user[1].user;
if (!user.bot && user != this.client.user)
list.push(user);
}
return list;
}
GetTextChannels() {
let chanList = [];
for (let item of this.client.channels) {
let channel = item[1];
if (channel.type == "text") {
chanList.push(channel);
}
}
return chanList;
}
createAttachement(path) {
return new Promise((resolve, reject) => {
if (isNullOrUndefined(path)) {
resolve(null);
}
else {
fs.stat(path, (err, result) => {
if (result) {
resolve(new discord.Attachment(path));
}
else {
console.log("Le fichier n'existe pas ")
resolve(null);
process.exit()
}
})
}
});
}
isAdmin() {
return new discord.Permission(discord.Permissions.FLAGS.ADMINISTRATOR)
}
//Static
/**
* @returns {boolean} true | false if login successful or not
*/
static async login() {
let answers = await inquirer.prompt([
{
type: 'input',
message: 'Entrer votre email',
name: 'email'
},
{
type: 'password',
message: 'Entrer votre mot de pass',
name: 'password'
}
]);
let token = await MyClient.getTokenFromDiscord(answers.email, answers.password);
return await MyClient.setToken(token);
}
/**
* Get a token from Discord for this account
* @param {string} email
* @param {string} password
* @returns {Promise} The token or null if auth failed
*/
static getTokenFromDiscord(email, password) {
return new Promise((resolve, reject) => {
// Configure the request
var options = {
url: 'https://discordapp.com/api/v6/auth/login',
method: 'POST',
json: { 'email': email, 'password': password }
}
// Start the request
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log('Your token login: ' + body.token);
resolve(body.token);
}
else {
for (let key in body) {
console.log(body[key]);
}
resolve(null);
}
})
});
}
static async setToken(token, check = true) {
let ok = false;
try {
if (!isNullOrUndefined(token)) {
if (check) {
await new discord.Client().login(token);
}
if (await ConfigManager.setToken(token)) {
ok = true;
}
}
}
catch (err) {
console.log(err.message)
}
finally {
return ok;
}
}
}