Last active 1693292823

yes

hmm.js Raw
1module.exports = async function(fastify) {
2
3 fastify.route({
4 url: '/fetch/:userId',
5 method: ['GET'],
6 config: {
7 rateLimit: {
8 max: 100,
9 timeWindow: 10000,
10 errorResponseBuilder: function(request, context) {
11 return {
12 code: 429,
13 error: 'Enhance your calm',
14 message: `Rate limited, please try again in: ${this.timeWindow}ms`,
15 date: Date.now(),
16 expiresIn: this.timeWindow
17 }
18 },
19 onExceeding: function(request, context) {
20 return {
21 code: 429,
22 error: 'Enhance your calm',
23 message: `Rate limited, please try again in: ${this.timeWindow}ms`,
24 date: Date.now(),
25 expiresIn: this.timeWindow,
26 }
27 },
28 onExceeded: function (request, context) {
29 return {
30 code: 420,
31 error: 'Enhance your calm',
32 message: `Whoops, looks like you have been rate limited. Please wait: ${this.timeWindow}ms`,
33 date: Date.now(),
34 expiresIn: this.timeWindow
35 }
36 }
37 }
38 },
39 schema: {
40 summary: 'Get a CordX user\'s Discord Profile',
41 description: 'Returns a given cordx user\'s discord id, global name etc',
42 tags: ['Users'],
43 params: {
44 type: 'object',
45 properties: {
46 userId: {
47 type: 'string',
48 description: 'A valid discord user id/snowflake'
49 }
50 }
51 },
52 response: {
53 200: {
54 description: 'Successful request',
55 type: 'object',
56 properties: {
57 id: { type: 'string'},
58 globalName: { type: 'string'},
59 displayName: { type: 'string'},
60 userName: { type: 'string'}
61 }
62 },
63 404: {
64 description: 'User not found',
65 type: 'object',
66 properties: {
67 code: { type: 'string'},
68 message: { type: 'string'},
69 status: { type: 'number'},
70 }
71 },
72 429: {
73 description: 'Rate limited',
74 type: 'object',
75 properties: {
76 code: { type: 'number'},
77 error: { type: 'string'},
78 message: { type: 'string'},
79 date: { type: 'string'},
80 expiresIn: { type: 'number'}
81 }
82 }
83 }
84 },
85 preHandler: async (request, reply) => {
86 const { userId } = request.params
87 let cordx_user = await request.client.users.fetch(userId);
88
89 if (!cordx_user) return reply.code(404).send({
90 code: 'USER_NOT_FOUND',
91 message: `Our discord client searched far and wide but was unable to locate a user with the ID: ${userId}. If needed you can use the #{/v3/users/discord/:userId} route to fetch a user using the discord api!`,
92 status: 404
93 });
94
95 if (!userId) {
96 reply.code(404).send({
97 code: 'USER_NOT_FOUND',
98 message: `The provided user ${userId} was not found!`,
99 status: 404
100 })
101 return null
102 }
103 },
104 handler: async (request, reply) => {
105
106 let user = request.params.userId;
107
108 let cordx_user = await request.client.users.fetch(user);
109
110 return reply.code(200).send({
111 id: cordx_user.id,
112 globalName: cordx_user.globalName,
113 displayName: cordx_user.displayName,
114 userName: cordx_user.username
115 })
116 }
117 });
118}