Showing
1 changed file
with
260 additions
and
0 deletions
app.js
0 → 100644
| 1 | +const {prefix, token, youtubeAPI} = require('./config.json'); | ||
| 2 | +const Discord = require("discord.js") // npm install discord.js 필요 | ||
| 3 | +const {MessageEmbed} = require('discord.js'); | ||
| 4 | +const client = new Discord.Client() | ||
| 5 | +const fs = require('fs'); // 파일 입출력 모듈 | ||
| 6 | +const internal = require("stream"); | ||
| 7 | +var now = new Date(); // 현재날짜 및 시간 객체 | ||
| 8 | +const moment = require('moment') // npm install --save moment 필요 (디데이 출력 모듈) | ||
| 9 | +const Youtube = require('simple-youtube-api'); // npm install simple-youtube-api | ||
| 10 | +const youtube = new Youtube(youtubeAPI); | ||
| 11 | +const ytdl = require('ytdl-core'); // npm install ytdl-core | ||
| 12 | +const { getVideoID } = require('ytdl-core'); | ||
| 13 | + | ||
| 14 | +//npm install discord.js @discord/opus 필요 | ||
| 15 | +//npm install --save ffmpeg-binaries 필요 | ||
| 16 | + | ||
| 17 | + | ||
| 18 | + | ||
| 19 | +//로그인 콘솔 출력 | ||
| 20 | +client.on("ready", () => { | ||
| 21 | + console.log(`Logged in as ${client.user.tag}!`) | ||
| 22 | + client.user.setPresence({ game: { name: "챗봇 상태메시지 적는곳"}, status: "online"}) | ||
| 23 | +}); | ||
| 24 | + | ||
| 25 | + | ||
| 26 | +//명령어 인식 | ||
| 27 | +client.on("message", msg => { | ||
| 28 | + console.log(msg.author.id, msg.content); //채팅을 로그에 띄우기 | ||
| 29 | + | ||
| 30 | + | ||
| 31 | + | ||
| 32 | + //prefix로 시작하지 않는경우 통과 를 위한 명령어 | ||
| 33 | + if (!msg.content.startsWith(prefix) || msg.author.bot) return; | ||
| 34 | + const args=msg.content.slice(prefix.length).split(" "); | ||
| 35 | + const command=args.shift().toLowerCase(); | ||
| 36 | + //prefix로 시작하지 않는경우 통과 를 위한 명령어 end | ||
| 37 | + | ||
| 38 | + | ||
| 39 | + | ||
| 40 | + | ||
| 41 | + //테스트 조건문 | ||
| 42 | + if (command === "ping") { | ||
| 43 | + msg.reply("Pong!") | ||
| 44 | + } | ||
| 45 | + if (command === "현재시간") { | ||
| 46 | + msg.reply(now.getFullYear() + "년 " + (now.getMonth()+1) + "월 " +now.getDate() + "일 " + now.getHours() + "시 " + now.getMinutes() + "분"); | ||
| 47 | + } // 테스트 조건문 end | ||
| 48 | + | ||
| 49 | + | ||
| 50 | + | ||
| 51 | + | ||
| 52 | + | ||
| 53 | + //공부시작 시간 체크 | ||
| 54 | + if (command === "공부시작") { | ||
| 55 | + var data = String(now.getHours()) +"."+ String(now.getMinutes()); | ||
| 56 | + console.log(msg.author.id); | ||
| 57 | + | ||
| 58 | + var fileName = "./data/stopWatch/" + msg.author.id + ".txt"; | ||
| 59 | + fs.writeFileSync(fileName, data, 'utf8', function(error){ // 파일에 data내용 저장 | ||
| 60 | + console.log('studyStart write end'); | ||
| 61 | + }); | ||
| 62 | + | ||
| 63 | + msg.reply("공부시작! 열공~ ⁽⁽◝( ˙ ꒳ ˙ )◜⁾⁾"); | ||
| 64 | + } //공부시작 시간 체크 end | ||
| 65 | + | ||
| 66 | + | ||
| 67 | + | ||
| 68 | + | ||
| 69 | + | ||
| 70 | + //공부끝 시간 체크 | ||
| 71 | + if (command === "공부끝") { | ||
| 72 | + var fileName = "data/stopWatch/" + msg.author.id + ".txt"; | ||
| 73 | + | ||
| 74 | + try { | ||
| 75 | + // 파일 있는지 확인. 없으면 catch | ||
| 76 | + | ||
| 77 | + //(공부시작을 한 경우) : 공부시간 계산 | ||
| 78 | + fs.readFile(fileName, 'utf8', function(err, data) { | ||
| 79 | + console.log('find'); | ||
| 80 | + var studyData = data.toString().split('.'); | ||
| 81 | + var studyHours = now.getHours() - Number(studyData[0]); | ||
| 82 | + if (now.getMinutes() - Number(studyData[1]) < 0) { | ||
| 83 | + var studyMinutes = 60 + now.getMinutes() - Number(studyData[1]); | ||
| 84 | + } | ||
| 85 | + else { | ||
| 86 | + var studyMinutes = now.getMinutes() - Number(studyData[1]); | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + //공부시간 출력 | ||
| 90 | + msg.reply(studyHours + "시간 " + studyMinutes + "분 공부하였습니다."); | ||
| 91 | + console.log(studyHours + "h " + studyMinutes + "m"); | ||
| 92 | + }); | ||
| 93 | + //공부시간 출력하였으면 공부시작 적은 파일 삭제. | ||
| 94 | + try { | ||
| 95 | + fs.unlinkSync(fileName) | ||
| 96 | + } catch (error) { | ||
| 97 | + if(err.code == 'ENOENT'){ | ||
| 98 | + console.log("file delete error"); | ||
| 99 | + } | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + } catch (error) { | ||
| 103 | + //(공부시작을 하지 않은 경우) | ||
| 104 | + if (error.code === "ENOENT") { | ||
| 105 | + console.log("user no start"); | ||
| 106 | + msg.reply("아직 공부를 시작하지 않았습니다."); | ||
| 107 | + } | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + } //공부끝 시간 체크 end | ||
| 111 | + | ||
| 112 | + | ||
| 113 | + | ||
| 114 | + | ||
| 115 | + | ||
| 116 | + | ||
| 117 | + | ||
| 118 | + | ||
| 119 | + | ||
| 120 | + //디데이부분 수정 필요 | ||
| 121 | + //디데이 설정 | ||
| 122 | + if (msg.content.startsWith("~디데이설정")) { | ||
| 123 | + console.log("dDaySetStart"); | ||
| 124 | + try { | ||
| 125 | + var dDayData = msg.toString().split(" "); | ||
| 126 | + var dDayTitle = dDayData[1]; | ||
| 127 | + var dDayWhen = dDayData[2].toString().split('/'); | ||
| 128 | + var fileName = "data/dDay/" + dDayTitle + ".txt"; | ||
| 129 | + | ||
| 130 | + fs.writeFileSync(fileName, dDayData[2], 'utf8', function(error){ // 파일에 data내용 저장 | ||
| 131 | + console.log('dDaySet write end'); | ||
| 132 | + }); | ||
| 133 | + console.log(dDayWhen[0] + "월 " + dDayWhen[1] + "일에 " + dDayTitle + "이(가) 설정되었습니다."); | ||
| 134 | + msg.reply(dDayWhen[0] + "월 " + dDayWhen[1] + "일에 " + dDayTitle + "이(가) 설정되었습니다."); | ||
| 135 | + | ||
| 136 | + } catch { | ||
| 137 | + msg.reply("양식이 올바르지 않습니다. 예) ~디데이설정 기말고사 12/15"); | ||
| 138 | + } | ||
| 139 | + | ||
| 140 | + | ||
| 141 | + } //디데이 설정 end | ||
| 142 | + | ||
| 143 | + | ||
| 144 | + | ||
| 145 | + | ||
| 146 | + | ||
| 147 | + //디데이 달력 보기 | ||
| 148 | + if (command === "디데이보기") { | ||
| 149 | + fs.readdir('./data/dDay', (err, file_list) => { //폴더열기 | ||
| 150 | + var fileArr = file_list.toString().split(','); //dDay 배열 | ||
| 151 | + | ||
| 152 | + fileArr.forEach((el,i) => { | ||
| 153 | + fs.readFile("./data/dDay/"+el, 'utf8', function(err, data) { | ||
| 154 | + var dDayWhen = data.toString().split('/'); | ||
| 155 | + var t1 = moment(); //현재 날짜 | ||
| 156 | + var t2 = moment(String(now.getFullYear()) + "-" + dDayWhen[0] + "-" + dDayWhen[1] , 'YYYY-MM-DD'); // 저장된 날짜 | ||
| 157 | + | ||
| 158 | + msg.reply(el.replace('.txt','') + "까지 D - "+ (Number(t2.diff(t1,'days')) + 2)); //dDay 답장 | ||
| 159 | + }); | ||
| 160 | + }); | ||
| 161 | + }); | ||
| 162 | + } //디데이 달력 보기 end | ||
| 163 | + | ||
| 164 | + | ||
| 165 | + //디데이 삭제 | ||
| 166 | + if (msg.content.startsWith("~디데이삭제")) { | ||
| 167 | + var dDayData = msg.toString().split(" "); | ||
| 168 | + var fileName = "data/dDay/" + dDayData[1] + ".txt"; | ||
| 169 | + try { | ||
| 170 | + | ||
| 171 | + fs.statSync(fileName); //파일 존재 확인 | ||
| 172 | + try { | ||
| 173 | + fs.unlinkSync(fileName) // 파일 존재시 삭제 | ||
| 174 | + msg.reply("해당 이벤트가 삭제되었습니다."); | ||
| 175 | + } catch (error) { | ||
| 176 | + if(err.code == 'ENOENT'){ | ||
| 177 | + console.log("file delete error"); | ||
| 178 | + } | ||
| 179 | + } | ||
| 180 | + | ||
| 181 | + } catch (error) { | ||
| 182 | + | ||
| 183 | + //파일이 없다면 에러 발생 | ||
| 184 | + msg.reply("설정되지 않은 이벤트입니다."); | ||
| 185 | + if (error.code === "ENOENT") { | ||
| 186 | + console.log("파일이 존재하지 않습니다."); | ||
| 187 | + } | ||
| 188 | + } | ||
| 189 | + | ||
| 190 | + | ||
| 191 | + | ||
| 192 | + } //디데이 삭제 end | ||
| 193 | + | ||
| 194 | + | ||
| 195 | + | ||
| 196 | + | ||
| 197 | + | ||
| 198 | + | ||
| 199 | + // 음악재생 | ||
| 200 | + if (command === "음악") { | ||
| 201 | + if (msg.member.voice.channel) { | ||
| 202 | + msg.member.voice.channel.join() | ||
| 203 | + .then(connection => { | ||
| 204 | + msg.reply("재생한다!"); | ||
| 205 | + const dispatcher = connection.play("music/comfortable.mp3"); | ||
| 206 | + dispatcher.on("end", end => {}); | ||
| 207 | + }) | ||
| 208 | + .catch(console.log); | ||
| 209 | + } else { | ||
| 210 | + msg.reply("먼저 보이스채널에 입장해주세요."); | ||
| 211 | + } | ||
| 212 | + } // 음악재생 end | ||
| 213 | + | ||
| 214 | + | ||
| 215 | + // 보이스채널 나가기 | ||
| 216 | + if (command === "나가") { | ||
| 217 | + | ||
| 218 | + if (msg.member.voice.channel) { | ||
| 219 | + msg.member.voice.channel.leave(); | ||
| 220 | + msg.reply('bye!'); | ||
| 221 | + } else { | ||
| 222 | + msg.reply('이미 나왔어요.'); | ||
| 223 | + } | ||
| 224 | + } // 보이스채널 나가기 end | ||
| 225 | + | ||
| 226 | + | ||
| 227 | + | ||
| 228 | + | ||
| 229 | + | ||
| 230 | + //유튜브 음악 재생 | ||
| 231 | + if (msg.content.startsWith(prefix+"재생")) { | ||
| 232 | + var msgData = msg.toString().split(" "); | ||
| 233 | + if (msg.member.voice.channel) { | ||
| 234 | + msg.member.voice.channel.join() | ||
| 235 | + .then(connection => { | ||
| 236 | + youtube.searchVideos(msgData[1]).then(results => { // 유튜브에 msgData[1] 검색 | ||
| 237 | + const play = connection.play(ytdl("https://www.youtube.com/watch?v="+results[0].id)); | ||
| 238 | + play.on('start', () => { | ||
| 239 | + //내용 추가 필요 | ||
| 240 | + }) | ||
| 241 | + console.log(results[0].title); | ||
| 242 | + msg.reply(results[0].title + " 을 재생한다!"); | ||
| 243 | + }) | ||
| 244 | + | ||
| 245 | + }) | ||
| 246 | + .catch(console.log); | ||
| 247 | + } else { | ||
| 248 | + msg.reply("먼저 보이스채널에 입장해주세요."); | ||
| 249 | + } | ||
| 250 | + } // 유튜브 음악 재생 end | ||
| 251 | + | ||
| 252 | + | ||
| 253 | + | ||
| 254 | + | ||
| 255 | + | ||
| 256 | +}) | ||
| 257 | + | ||
| 258 | + | ||
| 259 | +//디스코드 봇 토큰 | ||
| 260 | +client.login(token); | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment