1- const discord = require ( 'discord.js' )
1+ /**
2+ * @name FilteringAPI
3+ * @description 이 API는 웹 전용 REST API입니다
4+ */
5+
6+ 'use strict' // 엄격모드
7+
8+ /** Application Port */
9+ const PORT = 8080
10+
11+ /** Loging Module */
12+ const markup = require ( 'chalk' )
13+
14+ /** Rest API Module */
15+ const express = require ( 'express' )
16+
17+ /** Dialogflow Module */
18+ process . env . GOOGLE_APPLICATION_CREDENTIALS = './lib/BadWordsFilter-e34ed9d4dd5b.json'
19+ const dialogflowId = 'badwordsfilter-esqgxe'
20+ const dialogflow = require ( 'dialogflow' )
21+
22+ /** Nano ID Module */
23+ const nanoid = require ( 'nanoid' )
24+
25+ /** Hangul Module */
26+ const hangul = require ( 'hangul-js' )
27+
28+ /** English Module */
29+ const isEnglish = require ( 'is-alphabetical' )
30+
31+ /** Dialogflow Client */
32+ const dialogflowClient = new dialogflow . SessionsClient ( )
33+
34+ /** Rest API Router */
35+ const app = express ( )
36+
37+ /** Bad Words DataBase */
38+ const DB = require ( '../../public/BadWords.json' )
39+
40+ /** Memory */
41+ let temp = { }
42+
43+ app . get ( '/' , ( req , res ) => {
44+ console . log ( markup . cyan . underline ( 'REQUEST' ) + ' ' + markup . cyan ( req . ip + ' ' + req . protocol + ' ' + req . path ) )
45+ temp = {
46+ name : 'FilteringAPI' ,
47+ description : '이 API는 웹 전용 REST API입니다' ,
48+ uses : '/check/<문장>'
49+ }
50+ res . send ( temp )
51+ console . log ( markup . green . underline ( 'RESPONSE' ) + ' ' + markup . green ( JSON . stringify ( temp ) ) )
52+ } )
53+
54+ app . get ( '/check/:query' , ( req , res ) => {
55+ console . log ( markup . cyan . underline ( 'REQUEST' ) + ' ' + markup . cyan ( req . ip + ' ' + req . protocol + ' ' + req . path ) )
56+
57+ /** @type {String } */
58+ let query = req . params . query
59+ let queryArr = query . split ( ' ' )
60+
61+ proc ( query , ( result ) => {
62+ temp = {
63+ query : {
64+ sentense : query ,
65+ length : query . length ,
66+ splitBySpace : queryArr
67+ } ,
68+ result : result
69+ }
70+ res . send ( temp )
71+ console . log ( markup . gray ( '---------\n' ) + markup . green . underline ( 'RESPONSE' ) + ' ' + markup . green ( JSON . stringify ( temp ) ) )
72+ } )
73+
74+ } )
75+
76+ app . listen ( PORT )
77+ console . log ( markup . hex ( '#7289DA' ) . bold ( 'Application is Booted! App on at http://localhost:' + PORT + '/' ) )
78+
79+ /* --------------------- */
80+ /**
81+ * 욕설 체크
82+ * @param {String } query 욕설인지 체크할 문자열
83+ * @param {function(boolean) } cb 욕설 여/부 콜백
84+ */
85+ function check ( query , cb ) {
86+ console . log ( markup . yellow . underline ( 'PROCESS' ) + ' ' + markup . yellow ( dialogflowId ) + ': ' + markup . red ( query ) )
87+ let dialogflowPath = dialogflowClient . sessionPath ( dialogflowId , nanoid ( ) )
88+
89+ let dialogflowRequest = {
90+ session : dialogflowPath ,
91+ queryInput : {
92+ text : {
93+ text : query ,
94+ languageCode : 'ko-KR'
95+ }
96+ }
97+ }
98+
99+ dialogflowClient . detectIntent ( dialogflowRequest ) . then ( ( dialogflowResponse ) => {
100+ let dialogflowResponseText = dialogflowResponse [ 0 ] . queryResult . fulfillmentText
101+
102+ console . log ( markup . magenta . underline ( 'COMPLETE' ) + ' ' + markup . magenta ( dialogflowResponseText ) )
103+
104+ if ( dialogflowResponseText . startsWith ( 'badword: ' ) ) {
105+ if ( eval ( dialogflowResponseText . split ( ' ' ) [ 1 ] ) === true ) {
106+ cb ( true )
107+ } else {
108+ cb ( false )
109+ }
110+ }
111+ } )
112+ }
113+
114+ function proc ( query , cb ) {
115+ let isHangul = false
116+ query . split ( '' ) . forEach ( ( v , i ) => {
117+ if ( hangul . isHangul ( v ) ) {
118+ isHangul = true
119+ }
120+ } )
121+ if ( isHangul ) {
122+ console . log ( markup . gray ( '---------KR-P1' ) )
123+ check ( query , ( first ) => {
124+ if ( first ) {
125+ cb ( true )
126+ } else {
127+ console . log ( markup . gray ( '---------KR-P2' ) )
128+ let onlyKorean = ''
129+ query . split ( '' ) . forEach ( ( v , i ) => {
130+ if ( hangul . isHangul ( v ) ) {
131+ onlyKorean += v
132+ }
133+ } )
134+ check ( onlyKorean , ( second ) => {
135+ if ( second ) {
136+ cb ( true )
137+ } else {
138+ console . log ( markup . gray ( '---------KR-P3' ) )
139+ let hangulArr = hangul . disassemble ( onlyKorean )
140+ let toEng = ''
141+ hangulArr . forEach ( ( v , i ) => {
142+ if ( DB . hanYongKey . koreans . includes ( v ) ) {
143+ toEng += DB . hanYongKey . englishs [ DB . hanYongKey . koreans . indexOf ( v ) ]
144+ }
145+ } )
146+ check ( toEng , ( third ) => {
147+ if ( third ) {
148+ cb ( true )
149+ } else {
150+ cb ( false )
151+ }
152+ } )
153+ }
154+ } )
155+ }
156+ } )
157+ } else {
158+ console . log ( markup . gray ( '---------EN-P1' ) )
159+ check ( query , ( first ) => {
160+ if ( first ) {
161+ cb ( true )
162+ } else {
163+ console . log ( markup . gray ( '---------EN-P2' ) )
164+ let onlyEnglish = ''
165+ query . split ( '' ) . forEach ( ( v ) => {
166+ if ( isEnglish ( v ) ) {
167+ onlyEnglish += v
168+ }
169+ } )
170+ check ( onlyEnglish , ( second ) => {
171+ if ( second ) {
172+ cb ( true )
173+ } else {
174+ console . log ( markup . gray ( '---------EN-P3' ) )
175+ let toKor = ''
176+ onlyEnglish . split ( '' ) . forEach ( ( v ) => {
177+ if ( DB . hanYongKey . englishs . includes ( v ) ) {
178+ toKor += DB . hanYongKey . koreans [ DB . hanYongKey . englishs . indexOf ( v ) ]
179+ }
180+ } )
181+ toKor = hangul . assemble ( toKor )
182+ check ( toKor , ( third ) => {
183+ if ( third ) {
184+ cb ( true )
185+ } else {
186+ cb ( false )
187+ }
188+ } )
189+ }
190+ } )
191+ }
192+ } )
193+ }
194+ }
0 commit comments