/[sudobot]/trunk/src/commands/information/ProfileCommand.ts
ViewVC logotype

Annotation of /trunk/src/commands/information/ProfileCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 264 - (hide annotations)
Mon Jul 29 17:29:16 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 8089 byte(s)
fix: show user status properly (#59)
1 rakin 264 import { ColorResolvable, CommandInteraction, GuildMember, Message, User, UserFlags } from 'discord.js';
2 rakin 51 import BaseCommand from '../../utils/structures/BaseCommand';
3     import DiscordClient from '../../client/Client';
4     import CommandOptions from '../../types/CommandOptions';
5     import InteractionOptions from '../../types/InteractionOptions';
6     import MessageEmbed from '../../client/MessageEmbed';
7     import e from 'express';
8     import getUser from '../../utils/getUser';
9     import getMember from '../../utils/getMember';
10     import { timeSince } from '../../utils/util';
11     import { roleMention } from '@discordjs/builders';
12    
13     export default class ProfileCommand extends BaseCommand {
14     supportsInteractions: boolean = true;
15    
16     constructor() {
17     super('profile', 'information', []);
18     }
19    
20     async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
21     let user: GuildMember | null = null;
22    
23     if (msg instanceof CommandInteraction && options.isInteraction) {
24     if (options.options.getMember('user'))
25     user = <GuildMember> await options.options.getMember('user');
26     else
27     user = <GuildMember> msg.member!;
28     }
29     else if (msg instanceof Message && !options.isInteraction) {
30     if (options.normalArgs[0]) {
31     try {
32     const tempMember = await getMember(msg, options);
33    
34     if (!tempMember)
35     throw new Error();
36    
37     user = tempMember;
38     }
39     catch (e) {
40     console.log(e);
41    
42     await msg.reply({
43     embeds: [
44     new MessageEmbed()
45     .setColor('#f14a60')
46     .setDescription(':x: The user doesn\'t exist or not a member of this server.')
47     ]
48     });
49    
50     return;
51     }
52     }
53     else {
54     user = msg.member!;
55     }
56     }
57    
58 rakin 64 const status = (s: 'idle' | 'online' | 'dnd' | 'invisible' | null | undefined): string => {
59     if (s === 'idle')
60     return 'Idle';
61     else if (s === 'dnd')
62     return 'Do not disturb';
63     else if (s === 'online')
64     return 'Online';
65     else if (s === undefined || s === null || s === 'invisible')
66     return 'Offline/Invisible';
67    
68     return s;
69     };
70    
71     const statusText = '' + ((user?.presence?.clientStatus?.desktop ? 'Desktop (' + status(user?.presence?.clientStatus?.desktop) + ')\n' : '') + (user?.presence?.clientStatus?.web ? 'Web (' + status(user?.presence?.clientStatus?.web) + ')\n' : '') + (user?.presence?.clientStatus?.mobile ? 'Mobile (' + status(user?.presence?.clientStatus?.mobile) + ')' : ''));
72 rakin 264 // const state = user?.presence?.activities.find(a => a.type === 'CUSTOM')?.state;
73     let activities: string[] | string = [];
74 rakin 64
75 rakin 264 if (user?.presence) {
76     for (const a of user?.presence?.activities.values()!) {
77     console.log(a);
78    
79     if (a.type === 'CUSTOM') {
80     activities.push(`${a.emoji?.toString()} ${a.state}`);
81     }
82     else if (a.type === 'LISTENING') {
83     if (a.name === 'Spotify') {
84     activities.push(`:notes: Listening to **Spotify**: **${a.state?.replace(/\;/, ',')} - ${a.details}**`);
85     continue;
86     }
87    
88     activities.push(`:musical_note: Listening to **${a.name}**`);
89     }
90     else if (a.type === 'COMPETING') {
91     activities.push(`:fire: Competing **${a.name}**`);
92     }
93     else if (a.type === 'PLAYING') {
94     activities.push(`:video_game: Playing **${a.name}**`);
95     }
96     else if (a.type === 'STREAMING') {
97     activities.push(`:video_camera: Streaming **${a.name}**`);
98     }
99     else if (a.type === 'WATCHING') {
100     activities.push(`:tv: Watching **${a.name}**`);
101     }
102     }
103     }
104    
105     activities = activities.join('\n');
106    
107     const { FLAGS } = UserFlags;
108    
109     const getUserBadges = (user: User) => {
110     const badges = [];
111    
112     if (user.flags?.has(FLAGS.BUGHUNTER_LEVEL_1))
113     badges.push('Bughunter Level 1');
114     if (user.flags?.has(FLAGS.BUGHUNTER_LEVEL_2))
115     badges.push('Bughunter Level 2');
116     if (user.flags?.has(FLAGS.DISCORD_CERTIFIED_MODERATOR))
117     badges.push('Discord Certified Moderator');
118     if (user.flags?.has(FLAGS.DISCORD_EMPLOYEE))
119     badges.push('Discord Staff');
120     if (user.flags?.has(FLAGS.EARLY_SUPPORTER))
121     badges.push('Early Nitro Supporter');
122     if (user.flags?.has(FLAGS.EARLY_VERIFIED_BOT_DEVELOPER))
123     badges.push('Early Verified Bot Developer');
124     if (user.flags?.has(FLAGS.HOUSE_BALANCE))
125     badges.push('HypeSquad Balance');
126     if (user.flags?.has(FLAGS.HOUSE_BRILLIANCE))
127     badges.push('HypeSquad Brilliance');
128     if (user.flags?.has(FLAGS.HOUSE_BRAVERY))
129     badges.push('HypeSquad Bravery');
130     if (user.flags?.has(FLAGS.HYPESQUAD_EVENTS))
131     badges.push('HypeSquad Events');
132     if (user.flags?.has(FLAGS.PARTNERED_SERVER_OWNER))
133     badges.push('Partnered Server Owner');
134     if (user.flags?.has(FLAGS.BOT_HTTP_INTERACTIONS))
135     badges.push('Supports Interactions');
136     if (user.flags?.has(FLAGS.VERIFIED_BOT))
137     badges.push('Verified Bot');
138    
139     return badges.map(b => `🔵 ${b}`);
140     };
141    
142     const fields = [
143     {
144     name: "Nickname",
145     value: `${user!.nickname?.replace(/\*\<\>\@\_\~\|/g, '') ?? '*Nickname not set*'}`
146     },
147     {
148     name: "Account Created",
149     value: `${user!.user.createdAt.toLocaleDateString('en-US')} (${timeSince(user!.user.createdTimestamp)})`
150     },
151     {
152     name: "Joined at",
153     value: `${user!.joinedAt!.toLocaleDateString('en-US')} (${timeSince(user!.joinedTimestamp!)})`
154     },
155     {
156     name: 'Active Devices',
157     value: `${statusText === '' ? 'Offline/Invisible' : statusText}`
158     },
159     {
160     name: 'Status',
161     value: `${activities?.trim() === '' ? '*No status set*' : activities}`
162     },
163     {
164     name: 'Roles',
165     value: user?.roles.cache.filter(role => role.id !== msg.guild!.id).sort((role1, role2) => {
166     return role2.position - role1.position;
167     }).reduce((acc, value) => `${acc} ${roleMention(value.id)}`, '')!.trim()!
168     }
169     ];
170    
171     const badges = getUserBadges(user!.user);
172    
173     if (badges.length > 0) {
174     fields.push({
175     name: 'Badges',
176     value: badges.join("\n")
177     });
178     }
179    
180 rakin 51 await msg.reply({
181     embeds: [
182     new MessageEmbed()
183     .setColor(user!.user!.hexAccentColor ? user!.user!.hexAccentColor! : '#007bff')
184     .setAuthor({
185     name: user?.user.tag!,
186 rakin 64 iconURL: user!.user.displayAvatarURL()
187 rakin 51 })
188 rakin 264 .setThumbnail(user!.displayAvatarURL({
189 rakin 51 size: 4096
190     }))
191 rakin 264 .setFields(fields)
192     .setFooter({
193     text: `${user!.id} - ${user?.user.bot ? 'Bot' : 'User'}`
194     })
195 rakin 51 ]
196     });
197     }
198     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26