1
- import { container } from '@sapphire/framework' ;
1
+ import { SapphireClient , container } from '@sapphire/framework' ;
2
+ import { MessageEmbed } from 'discord.js' ;
3
+ import { DEFAULT_EMBED_COLOUR } from '../../utils/embeds' ;
2
4
import {
3
5
CodeyCommandDetails ,
4
6
CodeyCommandOptionType ,
5
7
SapphireMessageExecuteType ,
6
8
SapphireMessageResponse ,
7
9
} from '../../codeyCommand' ;
8
- import { getEmployeesByCompanyId } from '../../components/company' ;
10
+ import {
11
+ CompanyPersonDetails ,
12
+ CrunchbaseCompanyProperties ,
13
+ getCompanyInfo ,
14
+ getEmployeesByCompanyId ,
15
+ } from '../../components/company' ;
9
16
10
17
const CRUNCHBASE_IMAGE_CDN =
11
18
'https://res.cloudinary.com/crunchbase-production/image/upload/c_lpad,h_170,w_170,f_auto,b_white,q_auto:eco,dpr_1' ;
19
+
20
+ const getCompanyFindEmbed = async (
21
+ client : SapphireClient < boolean > ,
22
+ companyId : string ,
23
+ companyInfo : CrunchbaseCompanyProperties ,
24
+ companyUsers : CompanyPersonDetails [ ] ,
25
+ ) => {
26
+ const formattedUsers = await Promise . all (
27
+ companyUsers . map ( async ( user ) => {
28
+ return {
29
+ ...user ,
30
+ tag : ( await client . users . fetch ( user . user_id ) ) . tag ,
31
+ } ;
32
+ } ) ,
33
+ ) ;
34
+ const companyEmbed = new MessageEmbed ( )
35
+ . setColor ( DEFAULT_EMBED_COLOUR )
36
+ . setTitle ( companyInfo . name )
37
+ . setDescription ( companyInfo . description ) ;
38
+ companyEmbed . setThumbnail ( `${ CRUNCHBASE_IMAGE_CDN } /${ companyInfo . image_id } ` ) ;
39
+ companyEmbed . addField (
40
+ 'Previous Employees' ,
41
+ formattedUsers . map ( ( user ) => `${ user . tag } - ${ user . role } ` ) . join ( ', ' ) ,
42
+ ) ;
43
+
44
+ return companyEmbed ;
45
+ } ;
46
+
12
47
const companyFindExecuteCommand : SapphireMessageExecuteType = async (
13
48
client ,
14
49
_messageFromUser ,
15
50
args ,
16
51
) : Promise < SapphireMessageResponse > => {
17
52
const company_id = args [ 'company_id' ] ;
18
53
if ( ! company_id ) {
19
- throw new Error ( 'please enter a valid user mention or ID for balance adjustment.' ) ;
54
+ throw new Error ( 'Please enter a company id.' ) ;
55
+ }
56
+ const companyInfo = await getCompanyInfo ( < string > company_id ) ;
57
+ if ( ! companyInfo ) {
58
+ throw new Error ( 'This company does not exist in the server!' ) ;
20
59
}
21
60
const companyUsers = await getEmployeesByCompanyId ( < string > company_id ) ;
22
61
if ( ! companyUsers ) {
23
- return 'No one works at this company in the server!' ;
62
+ throw new Error ( 'No one works at this company in the server!' ) ;
24
63
}
25
- const formattedUsers = await Promise . all (
26
- companyUsers . map ( async ( user ) => {
27
- return {
28
- ...user ,
29
- tag : ( await client . users . fetch ( user . user_id ) ) . tag ,
30
- } ;
31
- } ) ,
32
- ) ;
33
- return `employees are: ${ JSON . stringify ( formattedUsers ) } ` ;
64
+ return {
65
+ embeds : [ await getCompanyFindEmbed ( client , < string > company_id , companyInfo , companyUsers ) ] ,
66
+ } ;
34
67
} ;
35
68
36
69
export const companyFindCommandDetails : CodeyCommandDetails = {
@@ -40,7 +73,7 @@ export const companyFindCommandDetails: CodeyCommandDetails = {
40
73
detailedDescription : `**Examples:**
41
74
\`${ container . botPrefix } company find https://www.crunchbase.com/organization/microsoft\`
42
75
\`${ container . botPrefix } company f microsoft\`` ,
43
- messageWhenExecutingCommand : 'Enrolling company...' ,
76
+ messageWhenExecutingCommand : 'Finding company...' ,
44
77
executeCommand : companyFindExecuteCommand ,
45
78
46
79
options : [
0 commit comments