-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
138 lines (124 loc) · 3.5 KB
/
utils.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
/**
* @author Raul Novelo <[email protected]>
*/
const ObjectsToCsv = require('objects-to-csv')
// Import the native fs module
const fs = require('fs')
const { GUILD_ID } = require('./constants')
/**
*
* @param {Client} client Discord Client instance
*/
function getAllMembers (client) {
// Get the Guild and store it under the variable "list"
const guild = client.guilds.cache.get(GUILD_ID)
const list = []
// Iterate through the collection of GuildMembers from the Guild getting the username property of each member
// Fetch a single member without checking cache
return new Promise((res, rej) => {
guild.members.fetch({ force: true }).then(members => {
members.forEach(member => {
// console.log(member.nickname || member.displayName)
const { nickname, displayName, id } = member
const roles = member.roles.cache.map(role => role.name)
list.push({
id,
avatar: member.user.displayAvatarURL(),
name: nickname || displayName,
username: member.user.username,
dateJoined: new Date(member.joinedTimestamp).toLocaleDateString(),
roles
})
})
res(
list
.sort((a, b) => compare(b.roles.length, a.roles.length))
.filter(m => !m.roles.includes('Bot'))
)
})
})
}
function getAllRoles (client) {
// Get the Guild and store it under the variable "list"
return new Promise((resolve, reject) => {
client.guilds.fetch(GUILD_ID, { force: true }).then(guild => {
// Iterate through the collection of GuildMembers from the Guild getting the username property of each member
const roles = guild.roles.cache.sort((a, b) => compare(a.id, b.id))
resolve(
roles.map(r => {
return {
name: r.name,
color: r.hexColor,
position: r.position,
rawPosition: r.rawPosition
}
})
)
})
})
}
function kickMembers (client, user) {
return new Promise((resolve, reject) => {
client.guilds.fetch(GUILD_ID, { force: true }).then(guild => {
guild.members
.fetch({
user
})
.then(members => {
const promises = members.map(m => m.kick())
Promise.all(promises).then(res => resolve(res))
})
.catch(console.error)
})
})
}
/**
* Get user nickname (apodo)
* @param {Client} client Discord Client instance
* @param {User} user
*/
function getNickname (client, user) {
let guild = client.guilds.cache.get(GUILD_ID)
member = guild.member(user)
return member ? member.displayName : user.username
}
function compare (a, b) {
if (a > b) return 1
if (a < b) return -1
// a must be equal to b
return 0
}
function longDate (date) {
var event = new Date(date)
const options = {
weekday: 'short',
year: 'numeric',
month: 'short',
day: 'numeric',
hour12: false
}
return event.toLocaleString('es-MX', options)
}
/**
*
* @param {Array} elements
*/
async function createCSV (elements, fileName) {
// If you use "await", code must be inside an asynchronous function:
const csv = new ObjectsToCsv(elements)
// Save to file:
await csv.toDisk(fileName) // , { append: true }
// Return the CSV file as string:
const text = await csv.toString()
// Get the buffer from the 'asistencia.csv', assuming that the file exists
const buffer = fs.readFileSync(fileName)
return buffer
}
module.exports = {
longDate,
createCSV,
getNickname,
getAllMembers,
getAllRoles,
kickMembers
}