1+ import Discord from 'discord.js'
2+ import Embed from '../../utils/Embed'
3+ import fetch from 'node-fetch'
4+ import child from 'child_process'
5+ import { repository } from '../../../package.json'
6+ import { MessageCommand } from "src/structures/Command" ;
7+ import { GithubCommitAPI } from 'typings/command'
8+
9+
10+ export default new MessageCommand ( {
11+ name : 'update' ,
12+ description : '최신 업데이트 내용을 확인합니다.' ,
13+ aliases : [ '업데이트' , 'djqepdlxm' , '촏차' , 'check' ] ,
14+ } , async ( client , message , args ) => {
15+ // @ts -ignore
16+ if ( ! client . dokdo . owners . includes ( message . author . id ) )
17+ return message . reply ( `해당 명령어는 ${ client . user ?. username } 의 주인이 사용할 수 있는 명령어입니다.` )
18+
19+ let LoadingEmbed = new Embed ( client , 'warn' )
20+ . setTitle ( '잠시만 기다려주십시요' )
21+ . setDescription ( '최신 업데이트 내용을 불러오는 중입니다...' )
22+
23+ let msg = await message . reply ( { embeds : [ LoadingEmbed ] } )
24+ if ( repository ?. includes ( 'Your github url' ) || ! repository ) {
25+ LoadingEmbed . setTitle ( '이런...' )
26+ . setDescription ( '업데이트 내용을 불러오는 중에 오류가 발생했습니다.\n오류 내용: package.json에 `repository` 값이 없습니다.' )
27+ . setType ( 'error' )
28+
29+ await msg . edit ( { embeds : [ LoadingEmbed ] } )
30+ }
31+
32+ let repo = repository . replaceAll ( 'https://github.com/' , '' )
33+ if ( process . env . GITHUB_TOKEN ?? client . config . githubToken !== '' ) {
34+ fetch ( `https://api.github.com/repos/${ repo } /commits` , {
35+ headers : {
36+ 'Authorization' : `token ${ client . config . githubToken } `
37+ }
38+ } ) . then ( ( res ) => {
39+ if ( ! res . ok && res . status === 404 ) {
40+ LoadingEmbed . setTitle ( '이런...' )
41+ . setDescription ( '업데이트 내용을 불러오는 중에 오류가 발생했습니다.\n오류 내용: `package.json`에 있는 `repository` 에 있는 주소를 찾을수 없습니다.' )
42+ . setType ( 'error' )
43+
44+ msg . edit ( { embeds : [ LoadingEmbed ] } )
45+ } else {
46+ res . json ( ) . then ( ( json : GithubCommitAPI [ ] ) => {
47+ if ( json [ 0 ] . sha . trim ( ) . substring ( 0 , 6 ) === client . BUILD_NUMBER ) {
48+ let SuccessEmbed = new Embed ( client , 'success' )
49+ . setTitle ( '확인 완료!' )
50+ . setDescription ( '현재 최신 버전을 이용중입니다!' )
51+ . addField ( '현재 버전' , `v${ client . VERSION } ` , true )
52+ . addField ( '현재 빌드 번호' , `${ client . BUILD_NUMBER } ` , true )
53+
54+ return msg . edit ( { embeds : [ SuccessEmbed ] } )
55+ } else {
56+ let count = 0
57+ json . forEach ( commit => {
58+
59+ count ++
60+ if ( commit . sha . trim ( ) . substring ( 0 , 6 ) === client . BUILD_NUMBER ) {
61+ let NewUpdateEmbed = new Embed ( client , 'success' )
62+ . setTitle ( '최신 업데이트가 있습니다!' )
63+ . setDescription ( `최신 업데이트된 ${ count } 개의 내용이 있습니다. 지금 업데이트 하시겠습니까?` )
64+ . addField ( '현재 버전' , `v${ client . VERSION } ` , true )
65+ . addField ( '현재 빌드 번호' , `${ client . BUILD_NUMBER } ` , true )
66+ . addField ( '최신 빌드 번호' , `${ json [ 0 ] . sha . trim ( ) . substring ( 0 , 6 ) } ` , true )
67+
68+ let buttonData = new Discord . MessageButton ( )
69+ . setStyle ( 'SUCCESS' )
70+ . setLabel ( '업데이트 하기' )
71+ . setEmoji ( '✅' )
72+ . setCustomId ( 'update.run' )
73+
74+ let components = new Discord . MessageActionRow ( )
75+ . addComponents ( buttonData )
76+
77+ let collector = msg . channel . createMessageComponentCollector ( { time : 10 * 1000 } )
78+
79+ collector . on ( 'collect' , async ( interaction ) => {
80+ if ( interaction . customId === 'update.run' ) {
81+ collector . stop ( )
82+
83+ child . execSync ( `git pull https://username:${ client . config . githubToken } @github.com/${ repo } ` )
84+ await interaction . reply ( '업데이트가 완료되었습니다!' )
85+ } else if ( interaction . user . id !== message . author . id ) {
86+ interaction . reply ( `메세지를 작성한 **${ interaction . user . username } **만 업데이트할 수 있습니다.` )
87+ }
88+ } )
89+ msg . edit ( { embeds : [ NewUpdateEmbed ] , components : [ components ] } )
90+ } else {
91+ let BranchErrorEmbed = new Embed ( client , 'error' )
92+ . setTitle ( '뭔가 잘못된거 같아요...' )
93+ . setDescription ( '업데이트를 정보를 찾을수 없습니다. 브랜치가 다른걸수도 있습니다.\n기본 브랜치를 바꿔보는건 어떨까요?' )
94+ msg . edit ( { embeds : [ BranchErrorEmbed ] } )
95+ }
96+ } )
97+ }
98+ } )
99+ }
100+ } )
101+ } else {
102+ fetch ( `https://api.github.com/repos/${ repo } /commits` ) . then ( ( res ) => {
103+ if ( ! res . ok && res . status === 404 ) {
104+ LoadingEmbed . setTitle ( '이런...' )
105+ . setDescription ( '업데이트 내용을 불러오는 중에 오류가 발생했습니다.\n오류 내용: 찾을수 없거나 비공개되어 있습니다.' )
106+ . setType ( 'error' )
107+
108+ msg . edit ( { embeds : [ LoadingEmbed ] } )
109+ } else {
110+ res . json ( ) . then ( ( json : GithubCommitAPI [ ] ) => {
111+ if ( json [ 0 ] . sha . trim ( ) . substring ( 0 , 6 ) === client . BUILD_NUMBER ) {
112+ let SuccessEmbed = new Embed ( client , 'success' )
113+ . setTitle ( '확인 완료!' )
114+ . setDescription ( '현재 최신 버전을 이용중입니다!' )
115+ . addField ( '현재 버전' , `v${ client . VERSION } ` , true )
116+ . addField ( '현재 빌드 번호' , `[${ client . BUILD_NUMBER } ](${ repository } /commit/${ client . BUILD_NUMBER } )` , true )
117+
118+ return msg . edit ( { embeds : [ SuccessEmbed ] } )
119+ } else {
120+ let count = 0
121+ json . forEach ( commit => {
122+
123+ count ++
124+ if ( commit . sha . trim ( ) . substring ( 0 , 6 ) === client . BUILD_NUMBER ) {
125+ let NewUpdateEmbed = new Embed ( client , 'success' )
126+ . setTitle ( '최신 업데이트가 있습니다!' )
127+ . setDescription ( `최신 업데이트된 ${ count } 개의 내용이 있습니다. 지금 업데이트 하시겠습니까?` )
128+ . addField ( '현재 버전' , `v${ client . VERSION } ` , true )
129+ . addField ( '현재 빌드 번호' , `[${ client . BUILD_NUMBER } ](${ repository } /commit/${ client . BUILD_NUMBER } )` , true )
130+ . addField ( '최신 빌드 번호' , `[${ json [ 0 ] . sha . trim ( ) . substring ( 0 , 6 ) } ](${ repository } /commit/${ json [ 0 ] . sha } )` , true )
131+
132+ let buttonData = new Discord . MessageButton ( )
133+ . setStyle ( 'SUCCESS' )
134+ . setLabel ( '업데이트 하기' )
135+ . setEmoji ( '✅' )
136+ . setCustomId ( 'update.run' )
137+
138+ let components = new Discord . MessageActionRow ( )
139+ . addComponents ( buttonData )
140+ let collector = msg . channel . createMessageComponentCollector ( { time : 10 * 1000 } )
141+
142+ collector . on ( 'collect' , async ( interaction ) => {
143+ if ( interaction . customId === 'update.run' ) {
144+ collector . stop ( )
145+
146+ child . execSync ( `git pull https://username:${ client . config . githubToken } @github.com/${ repo } ` )
147+ await interaction . reply ( '업데이트가 완료되었습니다!' )
148+ } else if ( interaction . user . id !== message . author . id ) {
149+ interaction . reply ( `메세지를 작성한 **${ interaction . user . username } **만 업데이트할 수 있습니다.` )
150+ }
151+ } )
152+ return msg . edit ( { embeds : [ NewUpdateEmbed ] , components : [ components ] } )
153+ } else {
154+ let BranchErrorEmbed = new Embed ( client , 'error' )
155+ . setTitle ( '뭔가 잘못된거 같아요...' )
156+ . setDescription ( '업데이트를 정보를 찾을수 없습니다. 브랜치가 다른걸수도 있습니다.\n기본 브랜치를 바꿔보는건 어떨까요?' )
157+ msg . edit ( { embeds : [ BranchErrorEmbed ] } )
158+ }
159+ } )
160+ }
161+ } )
162+ }
163+ } )
164+ }
165+ } )
0 commit comments