반응형
반응형
반응형

최근 유행했던 주사위 게임을 디스코드 봇으로 만들어 보았다.

 


https://discord.com/api/oauth2/authorize?client_id=671993218438791169&permissions=34880&scope=bot

 

Discord - A New Way to Chat with Friends & Communities

Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.

discord.com

제 봇을 추가해서 테스트해 볼 수 있습니다.

 

명령어: >야추

 


 

 

 

정확히는 디스코드랑 상호작용하는 부분만 만들고 실제로 주사위를 굴리거나 점수를 반환하는 실질적인 게임 부분은 깃허브에서 찾아서 사용했다.

 

사용한 API 코드:

 

https://github.com/dkpark95/yahtzee

 

dkpark95/yahtzee

Code for a simple game of Yahtzee built with JS/jQuery on top of a simple Express API. - dkpark95/yahtzee

github.com

위 API를 돌려서 값만 주고 받아서 사용하는 거다.

 


구조

 

project/

├── bot/

│ ├── app.js (봇)

│ └── emojiCharacters.js (이모지 모음)

└── yahtzee/ (https://github.com/dkpark95/yahtzee)

 

 

emojiCharacters.js 파일은 아래 링크를 참조하여 생성하시면 됩니다.

https://discordjs.guide/popular-topics/miscellaneous-examples.html#emoji-characters

 

Discord.js Guide

A guide made by the community of discord.js for its users.

discordjs.guide


코드 작성하기

 

 

개발환경 : UBUNTU 18.04.3

개발언어: NODE JS V12.13.0

모듈 : discord.js v12

텍스트 에디터: ATOM (편하신거 사용하시면 됩니다.)

 

nodejs와 discordjs의 버전을 확인해주세요. 버전이 다를 경우 문제가 생길 수 있습니다.

discord.js의 버전은 무조건 V12이어야 합니다.

 

 

 

app.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Extract the required classes from the discord.js module
const Discord = require('discord.js');
const rp = require('request-promise');
const emojiCharacters = require('./emojiCharacters');
 
// Create an instance of a Discord client
const client = new Discord.Client();
 
const botName = 'M4ndU';
const devURL = 'https://mandu-mandu.tistory.com';
const yatchURL = 'http://127.0.0.1:3400';
 
const token = 'TOKEN_HERE';
 
const PREFIX = '?';
 
function showScore(game, i){
  if (game.scorecard[i].locked === true) {
    return game.scorecard[i].points;
  } else {
    var score = '0';
    if (game.scorecard[i].points===null) {
      score = 0;
    }else {
      score = game.scorecard[i].points;
    }
    return "0(+"+score+")";
  }
}
 
function showScoreEmoji(game, i){
  if (game.scorecard[i].locked === true) {
    return ' ✅ ';
  } else {
    return ' ❌ ';
  }
}
 
function showBonusScore(game){
  if (game.scorecard[7].points===null) {
    return `0(${game.scorecard[6].points}/63)(+35)`;
  } else {
    return `${game.scorecard[7].points===null?0:game.scorecard[7].points}(${game.scorecard[6].points===null?0:game.scorecard[6].points}/63)`;
  }
}
 
function showBonusEmoji(game){
  if (game.scorecard[7].points===null) {
    return ' ❌ ';
  } else if (game.scorecard[7].points=== 0) {
    return ' ⛔️ ';
  } else {
    return ' ✅ ';
  }
}
 
function embedScoreMsg(newGame, r, log, turn){
  var dices = "";
  for (var i = 0; i < 5; i++) {
    dices += emojiCharacters[`${newGame.round.dice[i].value}`] +' ';
  }
 
  var gameBoardEmbed = new Discord.MessageEmbed()
    .setColor('#0099ff')
    .setTitle(`야추! | 턴 ${turn}/13`)
    .setDescription('입력시간제한 : 1분')
    .setAuthor(botName, '''http://mandu-mandu.tistory.com/')
    .addFields(
      { name'주사위 🎲 ', value: `${dices}`, inline: true },
      { name'남은 횟수 🤹‍ ', value: `${r}`, inline: true },
      { name'점수 합계 🏆 ', value: `${newGame.scorecard[15].points===null?0:newGame.scorecard[15].points}`, inline: true },
      { name'-----------', value: '점수판', inline: false },
      { name: `#1: Ones${showScoreEmoji(newGame, 0)}`, value: `점수: ${ showScore(newGame, 0)}`, inline: true },
      { name: `#2: Twos${showScoreEmoji(newGame, 1)}`, value: `점수: ${ showScore(newGame, 1)}`, inline: true },
      { name: `#3: Threes${showScoreEmoji(newGame, 2)}`, value: `점수: ${ showScore(newGame, 2)}`, inline: true },
      { name: `#4: Fours${showScoreEmoji(newGame, 3)}`, value: `점수: ${ showScore(newGame, 3)}`, inline: true },
      { name: `#5: Fives${showScoreEmoji(newGame, 4)}`, value: `점수: ${ showScore(newGame, 4)}`, inline: true },
      { name: `#6: Sixs${showScoreEmoji(newGame, 5)}`, value: `점수: ${ showScore(newGame, 5)}`, inline: true },
      { name: `1-6 보너스!${showBonusEmoji(newGame)}`, value: `점수:${ showBonusScore(newGame)}`, inline: false },
      { name: `#7: Three of a Kind${showScoreEmoji(newGame, 8)}`, value: `점수: ${ showScore(newGame, 8)}`, inline: true },
      { name: `#8: Four of a Kind${showScoreEmoji(newGame, 9)}`, value: `점수: ${ showScore(newGame, 9)}`, inline: true },
      { name: `#9: Full House${showScoreEmoji(newGame, 10)}`, value: `점수: ${ showScore(newGame, 10)}`, inline: true },
      { name: `#10: Small Straight${showScoreEmoji(newGame, 11)}`, value: `점수: ${ showScore(newGame, 11)}`, inline: true },
      { name: `#11: Large Straight${showScoreEmoji(newGame, 12)}`, value: `점수: ${ showScore(newGame, 12)}`, inline: true },
      { name: `#12: Chance${showScoreEmoji(newGame, 13)}`, value: `점수: ${ showScore(newGame, 13)}`, inline: true },
      { name: `#13: Yahtzee${showScoreEmoji(newGame, 14)}`, value: `점수: ${ showScore(newGame, 14)}`, inline: true },
      { name'기록 📃', value: `${log}`, inline: false },
    )
    .setFooter('Made by M4ndU''');
 
    return gameBoardEmbed;
}
 
 
client.on('ready', async () => {
  console.log(`Logged in as ${client.user.tag}!`);
});
 
client.on('message', async msg => {
  if (msg.content.startsWith(PREFIX)) {
        const input = msg.content.slice(PREFIX.length).split(' ');
        var command = input.shift();
        var commandArgs = input;
 
    if (command === '야추') {
      //게임 시작
      const newGame = await rp({uri:yatchURL + '/api/game'}).then(function (repos) {
        return JSON.parse(repos);
      });
 
      var gameBoardEmbed = embedScoreMsg(newGame, 2,'.'1);
 
      var yachtmsg = await msg.channel.send(gameBoardEmbed).then(msg =>{
        return msg;
      });
 
      Promise.all([
            yachtmsg.react(emojiCharacters['1']),
            yachtmsg.react(emojiCharacters['2']),
            yachtmsg.react(emojiCharacters['3']),
        yachtmsg.react(emojiCharacters['4']),
        yachtmsg.react(emojiCharacters['5']),
        yachtmsg.react('🔁'), //주사위 굴리기
        yachtmsg.react('🔴'), //점수 바로 등록하기
        ]);
 
      //init
      var yachtGame = newGame;
      const filter = (reaction, user) => {
        return ['🔁','🔴'].includes(reaction.emoji.name&& user.id === msg.author.id;
      };
      var stopFlag = false;
      var exit = false;
      var options = {
          method: 'POST',
          uri:yatchURL + '/api/game/roll',
          body: yachtGame,
          json: true // Automatically stringifies the body to JSON
      };
      const filterm = response => {
        return ['1','2','3','4','5','6','7','8','9','10','11','12','13'].includes(response.content&& response.author.id === msg.author.id;
      };
      var choose = '0';
      var userReactions = new Object();
      //init
 
 
      //13턴 돌립니다.
      for (var gameTurn = 1; gameTurn < 14; gameTurn++) {
        //주사위 3라운드 굴리기
        for (var g = 2; g > 0; g--) {
          await yachtmsg.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
            .then(async function(collected) {
              const reaction = collected.first();
              if (reaction.emoji.name === '🔁') {
                for (var yrdl = 0; yrdl < 5; yrdl++) {
                  yachtGame.round.dice[yrdl].locked = yachtmsg.reactions.cache.get(emojiCharacters[`${yrdl+1}`]).users.cache.has(msg.author.id);
                }
                //roll
                  } else {
                stopFlag = true;
                g = 1;
                  }
            }).catch(async function(collected) {
              gameBoardEmbed = embedScoreMsg(yachtGame, 0,'시간초과.', gameTurn);
              yachtmsg.edit(gameBoardEmbed);
              stopFlag = true;
              exit = true;
            });
          options = {
              method: 'POST',
              uri:yatchURL + '/api/game/roll',
              body: yachtGame,
              json: true // Automatically stringifies the body to JSON
          };
          if (exit) {
            break;
          }
          if (!stopFlag) {
            yachtGame = await rp(options).then(function (repos) {
              return repos;
            });
          }
 
          if (g === 1) {
            gameBoardEmbed = embedScoreMsg(yachtGame, g-1,'점수로 등록할 번호를 입력하세요.', gameTurn);
            await yachtmsg.edit(gameBoardEmbed);
            break;
          } else {
            gameBoardEmbed = embedScoreMsg(yachtGame, g-1,'잠시 기다려주세요', gameTurn);
          }
 
          await yachtmsg.edit(gameBoardEmbed);
 
          //2 react remove
          await yachtmsg.reactions.resolve('🔁').users.remove(msg.author.id);
          await yachtmsg.reactions.resolve('🔴').users.remove(msg.author.id);
 
          gameBoardEmbed = embedScoreMsg(yachtGame, g-1,'아래 이모지를 클릭해주세요.', gameTurn);
          await yachtmsg.edit(gameBoardEmbed);
        }
        if (exit) {
          break;
        }
 
        //점수 등록과정
        choose = '0';
        stopFlag = false;
        for (var rcc = 0; rcc < 5; rcc++) {
          await msg.channel.awaitMessages(filterm, { max: 1, time: 60000, errors: ['time'] })
                .then(async collected => {
              var resmsg = collected.first();
                    if (isNaN(resmsg.content|| Number(resmsg.content> 14 || Number(resmsg.content< 1 || !Number.isInteger(Number(resmsg.content))) {
                gameBoardEmbed = embedScoreMsg(yachtGame, 0,'1에서 14사이의 정수를 입력.');
                yachtmsg.edit(gameBoardEmbed);
              }else {
                if (resmsg.content < 7) {
                  choose = Number(resmsg.content-1;
                } else {
                  choose = Number(resmsg.content+1;
                }
                if (yachtGame.scorecard[Number(choose)].locked) {
                  gameBoardEmbed = embedScoreMsg(yachtGame, 0,'이미 등록된 번호. 다른번호로 입력.', gameTurn);
                  yachtmsg.edit(gameBoardEmbed);
                }else {
                  options = {
                      method: 'PUT',
                      uri:yatchURL + '/api/game/select/' + yachtGame.scorecard[choose].id,
                      body: yachtGame,
                      json: true // Automatically stringifies the body to JSON
                  };
                  yachtGame = await rp(options).then(function (repos) {
                    return repos;
                  });
                  stopFlag = true;
                }
              }
              resmsg.delete();
                })
                .catch(collected => {
              gameBoardEmbed = embedScoreMsg(yachtGame, 0,'시간초과.', gameTurn);
              yachtmsg.edit(gameBoardEmbed);
              stopFlag = true;
              exit = true;
                });
          if (stopFlag || exit) {
            break;
          }
        }
 
        stopFlag = false;
        if (exit) {
          break;
        }
 
        if (gameTurn === 13) {
          gameBoardEmbed = embedScoreMsg(yachtGame, 0,'게임종료', gameTurn);
          break;
        }else {
          gameBoardEmbed = embedScoreMsg(yachtGame, 2,'기다려주세요', gameTurn+1);
        }
 
        await yachtmsg.edit(gameBoardEmbed);
        //all react remove
        await yachtmsg.reactions.resolve(emojiCharacters['1']).users.remove(msg.author.id);
        await yachtmsg.reactions.resolve(emojiCharacters['2']).users.remove(msg.author.id);
        await yachtmsg.reactions.resolve(emojiCharacters['3']).users.remove(msg.author.id);
        await yachtmsg.reactions.resolve(emojiCharacters['4']).users.remove(msg.author.id);
        await yachtmsg.reactions.resolve(emojiCharacters['5']).users.remove(msg.author.id);
        await yachtmsg.reactions.resolve('🔁').users.remove(msg.author.id);
        await yachtmsg.reactions.resolve('🔴').users.remove(msg.author.id);
 
        gameBoardEmbed = embedScoreMsg(yachtGame, 2,'아래 이모지를 클릭!', gameTurn+1);
        await yachtmsg.edit(gameBoardEmbed);
      }
 
    }
  }
});
 
client.login(token);
 
cs

 


https://discord.com/api/oauth2/authorize?client_id=671993218438791169&permissions=34880&scope=bot

 

Discord - A New Way to Chat with Friends & Communities

Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.

discord.com

제 봇을 추가해서 테스트해 볼 수 있습니다.

 

명령어: >야추

반응형
반응형

직접 MBTI를 검사하는 봇은 아니고, 외부 사이트 등에서 mbti를 검사하고서 그 결과를 등록한 뒤, 디스코드 구성원들과 궁합을 보는 봇이다.

 

인터넷에 떠돌아 다니는 MBTI 궁합 차트를 기반으로 했다.

 

물론 아무런 근거 없는 궁합이기 때문에 재미로만 보자.

 

 


명령어별 기능

 

 

mbti

명령어 모음 (help 명령어와 동일)

 

1. mbti 검사

MBTI를 검사할 수 있는 링크를 출력한다.

 

2. mbti 보기 [MBTI]

MBTI 유형을 입력해주면, 해당 유형의 내용을 볼 수 있는 링크를 출력한다.

 

3. mbti 등록 [MBTI]

자신의 MBTI 유형을 등록한다. (기존에 등록했어도 덮어쓰기 가능.)

 

4. mbti 등록현황

mbti 등록 현황을 출력한다.

 

5. mbti 궁합 @A @B

@자리에 원하는 사람을 언급해주면 된다.

@A만 언급하면 A와 나 자신의 궁합 결과를 출력한다.

@A @B 모두 언급해주면, @A와 @B의 궁합 결과를 표시해준다.

 

 


개발환경 : UBUNTU 18.04.3

개발언어: NODE JS V12

모듈 : discord.js v12

텍스트 에디터: ATOM (편하신거 사용하시면 됩니다.)

 

봇 생성하는 것은 아래 포스트를 보고 오자.

https://mandu-mandu.tistory.com/385?category=693080

 

노드자스로 디스코드 봇 만들기(1) - 봇 생성부터 테스트까지

이 글만을 통해서 기본적으로 텍스트를 출력하는 디스코드 봇을 만들 수 있습니다. 1. 봇 생성하고 초대하기 2. 코드 작성하기 3. 테스트 하기 봇 생성하고 초대하기 먼저 봇 계정을 생성해 주어��

mandu-mandu.tistory.com

 

 

 

일단 위 궁합표를 배열로 만들었다. (노가다다 노가다...)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//16가지 mbti 유형
const mbti = new Array("INFP","ENFP","INFJ","ENFJ""INTJ""ENTJ""INTP""ENTP""ISFP""ESFP""ISTP""ESTP""ISFJ""ESFJ""ISTJ""ESTJ");
 
//Relation : A, B, C, D, F
var mbtiR = new Array(16);
mbtiR[0]  = ["A"];
mbtiR[1]  = ["A""A"];
mbtiR[2]  = ["A""B""A"];
mbtiR[3]  = ["B""A""A""A"];
mbtiR[4]  = ["A""B""A""A""A"];
mbtiR[5]  = ["B""A""A""A""A""A"];
mbtiR[6]  = ["A""A""A""A""A""B""A"];
mbtiR[7]  = ["A""A""B""A""B""A""A""A"];
mbtiR[8]  = ["F""F""F""B""C""C""C""C""D"];
mbtiR[9]  = ["F""F""F""F""C""C""C""C""D""D"];
mbtiR[10= ["F""F""F""F""C""C""C""C""D""D""D"];
mbtiR[11= ["F""F""F""F""C""C""C""C""D""D""D""D"];
mbtiR[12= ["F""F""F""F""D""C""D""D""C""B""C""B""A"];
mbtiR[13= ["F""F""F""F""D""C""D""D""B""C""B""C""A""A"];
mbtiR[14= ["F""F""F""F""D""C""D""D""C""B""C""B""A""A""A"];
mbtiR[15= ["F""F""F""F""D""C""B""D""B""C""B""C""A""A""A""A"];
 
cs

A, B, C, D, F 5단계이며, 위 표 기준으로 A가 파랑, F가 빨강이다.

 

 

 

 

 

 

 

이제 명령어 별로 6가지의 기능을 구현해야 하는데, 1번과 2번은 단술 입출력이기 때문에 아래 포스트를 보고 구현이 가능하다.

https://mandu-mandu.tistory.com/385?category=693080

 

노드자스로 디스코드 봇 만들기(1) - 봇 생성부터 테스트까지

이 글만을 통해서 기본적으로 텍스트를 출력하는 디스코드 봇을 만들 수 있습니다. 1. 봇 생성하고 초대하기 2. 코드 작성하기 3. 테스트 하기 봇 생성하고 초대하기 먼저 봇 계정을 생성해 주어��

mandu-mandu.tistory.com

https://mandu-mandu.tistory.com/386?category=693080

 

노드자스로 디스코드 봇 만들기(2) - 노래/사진/상태/삭제

이번에 추가할 기능은 아래와 같습니다. [ 로컬 노래파일 재생, 로컬 사진 전송, 상태메세지 설정, 메세지 지우기 ] 개발환경 : UBUNTU 18.04.3 개발언어: NODE JS V8 모듈 : discord.js v11 텍스트 에디터: ATOM

mandu-mandu.tistory.com

 

 

 


 

3번 4번 5번의 핵심 기능은 동일하다.

 

 

 

배열을 하나 만들어서

유저의 id (고유값), username, mbti 유형값을 받아서 저장하고

json으로 만들어서 외부 파일에 저장해둔다.

 

저장된 유저의 mbti값을 가져와서 궁합 값을 출력해주면 된다.

 

 

간단하다.

 

 

 

 

봇의 핵심인 3번과 5번 명령에 해당되는 코드:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
if (msg.content.startsWith('mbti 등록 ')) {
    var reg_mbti = msg.content.split(' ');
 
    //type
    var type = 16;
    var input_mbti = reg_mbti[2].toUpperCase();
    for(var i=0; i<16; i++) {
      if (mbti[i] == input_mbti) {
        type = i;
        break;
      }
    }
    if(type == 16){
      msg.channel.send("실패!");
    }else {
 
      //json to Array
      var data = fs.readFileSync('./user_list.json''utf8');
      var user_list_arr = JSON.parse(data);
 
      //search
      var j = -1;
      for(var k=0; k<user_list_arr.length; k++) {
        if (user_list_arr[k].user_id == msg.member.user.id) {
          j = k;
          break;
        }
      }
 
      //patch or push
      if (j == -1) {
        var user_list = new Object();
        user_list.user_id = msg.member.user.id;
        user_list.username = msg.member.user.username;
        user_list.mbti  = type;
        user_list_arr.push(user_list);
      } else {
        user_list_arr[j].username = msg.member.user.username;
        user_list_arr[j].mbti  = type;
      }
 
      //Array to json
      data = JSON.stringify(user_list_arr);
      fs.writeFileSync('./user_list.json', data, 'utf8');
 
      msg.channel.send("성공!");
    }
  }
 
  if (msg.content.startsWith('mbti 궁합')) {
    var comp_mbti = msg.content.split(' ');
 
    //json to Array
    var data = fs.readFileSync('./user_list.json''utf8');
    var user_list_arr = JSON.parse(data);
 
    //search user 1
    var user_1 = "";
    if (comp_mbti[2=== undefined) {
      user_1 = "";
    } else {
      user_1 = comp_mbti[2].replace(/[^0-9]/g,'');
    }
 
    var user1type = 16;
    var user1name = "";
    for(var k=0; k<user_list_arr.length; k++) {
      if (user_list_arr[k].user_id == user_1) {
        user1type = user_list_arr[k].mbti;
        user1name = user_list_arr[k].username;
        break;
      }
    }
 
    //search user 2
    var user_2 = "";
    if (comp_mbti[3=== undefined) {
      user_2 = msg.member.user.id;
    } else if (comp_mbti[3== "") {
      comp_mbti[3= comp_mbti[4]; //중간에 띄어쓰기가 한 번 더 들어간 경우에도 정상적인 처리를 위해서
      user_2 = comp_mbti[3].replace(/[^0-9]/g,'');
    } else {
      user_2 = comp_mbti[3].replace(/[^0-9]/g,'');
    }
 
    var user2type = 16;
    var user2name = "";
    for(var k=0; k<user_list_arr.length; k++) {
      if (user_list_arr[k].user_id == user_2) {
        user2type = user_list_arr[k].mbti;
        user2name = user_list_arr[k].username;
        break;
      }
    }
 
    //result
    if (user1type == 16 || user2type == 16) {
      msg.channel.send(noresultEmbed);
    } else{
 
      if (user2type > user1type) {
        [user1type, user2type] = [user2type, user1type];
      }
 
      var result = mbtiR[user1type][user2type];
 
      var explain_text1 = " 와(과) ";
      var explain_text2 = " 의 궁합";
 
      var resultEmbed = new Discord.MessageEmbed()
          .setColor('#0099FF')
          .setTitle(user1name.concat(explain_text1,user2name,explain_text2))
        .addField('결과', result, true)
          .setFooter('Powered by node.js  Made by M4ndU''');
 
      msg.channel.send(resultEmbed);
 
    }
  }
cs

 

 

 

 

 

 

 

 

 

 

 


실행

 

user_list.json 파일을 하나 만들어두고 봇을 실행해야한다.

 

 

실행 후에 오류나면 아래 글에서 해결법을 찾아보자. 아래 글에도 없다면 구글링!

https://mandu-mandu.tistory.com/381

 

discord js 설치, 실행 오류 / 오디오 재생 오류 문제 해결

1. npm으로 discord.js 설치 후 봇 실행시 발생하는 오류 해결 /Client.js:39 } catch { ^ 봇 실행시 위와 같은 오류가 발생할 경우입니다. 저같은 경우에는 discord.js github에 나와있는 설치방법으로 설치를 했

mandu-mandu.tistory.com

 

반응형
반응형

이 글만을 통해서 기본적으로 텍스트를 출력하는 디스코드 봇을 만들 수 있습니다.

 

1. 봇 생성하고 초대하기

2. 코드 작성하기

3. 테스트 하기

 


 

봇 생성하고 초대하기

 

먼저 봇 계정을 생성해 주어야 합니다.

 

 

디스코드 홈페이지로 이동합니다. -> https://discordapp.com/

 

Discord — A New Way to Chat with Friends & Communities

Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.

discordapp.com

 

상단에 개발자 > 개발자 포털

 

 

 

 

New Application을 누릅니다.

 

 

 

 

이름을 설정하고 Create!

 

 

 

왼쪽 탭에서 Bot을 클릭하고, 오른쪽에 있는 Add Bot을 클릭하여 봇을 만들어 줍시다!

 

 

현재 페이지에서 봇의 프로필사진, 이름을 수정하실 수 있으며,

중요한 Token 값을 확인하실 수 있습니다. (절대 외부로 유출되서는 안됩니다. Regenerate버튼을 통해 재생성 가능합니다.)

Public Bot 설정은 아무나 이 봇을 초대할 수 있는지, 나만 초대할 수 있는지를 설정하는 것입니다.

체크가 해제되어 있다면, 나만 이 봇을 초대할 수 있습니다.

 

아래로 스크롤하면, 봇의 권한을 설저할 수 있는 탭이 나옵니다.

여기서 설정하지 않아도, 디스코드 서버 역할을 통해서 권한을 부여해 줄 수도 있습니다.

 

 

 

 

 

 

이제 봇 초대코드를 생성하겠습니다.

 

왼쪽 두번째 탭 OAuth2를 클릭하시고, 중간에 bot을 체크하시면 아래에 링크가 생성됩니다.

 

해당 링크로 접속하여 자신의 서버로 봇을 초대할 수 있습니다.

 

 

 

 

현재 봇은 오프라인 상태가 됩니다.

 


 

코드 작성하기

 

 

개발환경 : UBUNTU 18.04.3

개발언어: NODE JS V8

모듈 : discord.js v11

텍스트 에디터: ATOM (편하신거 사용하시면 됩니다.)

 

 

 

설치방법

 

 바로가기 <-- (https://github.com/discordjs/discord.js#installation)

 

 

먼저 npm과 node가 설치되어 있는지 확인해보세요.

node -v
npm -v

 

 

음성 봇을 사용하지 않는 경우

npm install discord.js

 


음성 봇을 사용할 경우

npm install discord.js opusscript

 

 

 

 

 

 

설치를 마쳤다면,

이제 텍스트 에디터로 가서 소스를 작성해 봅시다.

 

 

기본적인 소스는 다음과 같습니다. node_modules폴더가 있는 폴더에서 작업해주세요!

저는 파일명을 bot.js로 했습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Extract the required classes from the discord.js module
const { Client, Attachment } = require('discord.js');
 
// Create an instance of a Discord client
const client = new Client();
 
 
client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});
 
client.on('message', msg => {
  if (msg.content.startsWith('r.help')) {
    msg.reply('Powered by node.js\nMade by M4ndU');
  }
 
  if (msg.content === 'r.whoami') {
    // Send the user's avatar URL
    msg.reply(msg.author.avatarURL);
  }
});
 
client.login('token');
 
cs

 

 

23행 token 자리에 디스코드 봇 관리 페이지에서 확인가능한 token 값을 넣어주시면 됩니다.

 

콘솔에 node bot.js을 입력해주시면 봇이 실행되면서, 디스코드 서버내에서 봇이 온라인 상태가 되는 것을 확인할 수 있습니다.

 

 

 

 

봇의 기능을 당담하는 12~21행의 내용을 분석해보겠습니다.

 

client.on('message', msg => {
  if (msg.content.startsWith('r.help')) {
    msg.reply('Powered by node.js\nMade by M4ndU');
  }

채팅창에 r.help로 시작하는 메세지를 보내면 메세지를 출력합니다.

Powered by node.js\nMade by M4ndU 이 부분을 수정하여 본인이 원하는 메세지를 넣을 수도 있습니다.

r.help를 수정하여 본인이 원하는 명령어로 변경하실 수도 있습니다.

 

 

 

if (msg.content === 'r.whoami') { 
    // Send the user's avatar URL 
    msg.reply(msg.author.avatarURL); 
  } 
});

메세지의 내용이 r.whoami와 동일하다면

그 유저의 정보를 보냅니다.

r.whoami를 수정하여 본인이 원하는 명령어로 변경하실 수도 있습니다.

msg.author.avatarURL 대신 'msg'로 자신이 원하는 메세지를 넣을 수도 있습니다.

 

 

msg.reply는 명령어를 입력한 사람을 언급과 함께 메세지를 보냅니다. 메세지만 보내고 싶으신 경우 msg.channel.send함수를 이용하시면 됩니다.

 

 

다음 포스트부터는 이 봇에 여러가지 기능을 추가해 보겠습니다.

반응형
반응형

반복적으로 같은 요청을 하는 (도배)를 막는 기능을 추가하겠습니다.




kakao auto_reply에서 제공하는 user_key를 이용하겠습니다.

user_key는 옐로우 아이디 마다 부여되는 고유 키값입니다.


처음에는 user_key값을 이용하여 반복적으로 동일한 요청을 해올 경우 딜레이를 주는 방법을 사용하려고 했지만

구현할 방법을 모르겠어서...


직전에 요청한 유저와 다음 요청해온 유저가 같을 경우 함수를 실행하지 않고 리턴하는 방법을 사용하겠습니다.

이 경우 다른 유저가 요청해오지 않는 이상 한 유저가 연속으로 요청을 할 수가 없게 되죠.


도배로 고통받는 개발자...




모든 요청(버튼)에 대해서 적용하는 방법과 각각의 요청(버튼)에 적용하는 방법이 있습니다.

모든 요청(버튼)에 적용하게 되면 한 요청을 했을 때 바로 다른 요청을 할 수가 없으므로

각각에 요청에 적용하는 방법을 사용하겠습니다.

그러기 위해서 class 를 사용하겠습니다.





코드 작성하기


적용된 전체 코드 :  https://github.com/M4ndU/inhun_kakao_chat_bot_2/blob/master/views.py




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class user_chk():
 
    def __init__(self):
        self.pre_key = "" #이전 user_key값
        self.now_key = "" #현재 user_key값
 
    def check(self, key):
        self.now_key = key # now_key값에 현재 user_key값 대입
 
        if self.pre_key == self.now_key : # 비교 하여 같으면 1을 반환
            passcode = 1
        else :
            self.pre_key = self.now_key # 다를 경우 pre_key값에 now_key값을 덮어쓰고 0 반환
            passcode = 0
        return passcode
cs



바로 다음 코드들을 작성하여 값 초기화 설정을 해 둡니다.

필요한 개수 만큼 작성합니다.


1
2
3
4
 
u0 = user_chk()
u1 = user_chk()
 
cs



이렇게 하면, u0.check(user_key)를 실행하게 될 경우

이전 user_key값(=pre_key)과 비교하여 같으면 1 반환, 다르면 pre_key에 저장하고 0을 반환합니다.


u0, u1각각 독립적으로 사용되기 때문에 u0.check("dd")를 했다고 해서 u1.pre_key에 dd가 드러가지 않는 것이죠.

이를 통해 각각 요청(버튼)에 적용하여 사용할 수 있습니다.



이제 user_key값을 가져와 봅시다.


1
2
3
4
...
    datacontent = received_json_data['content']
    user_key = received_json_data['user_key']
 
cs


기존 작성했던 datacontent = received_json_data['content'] 바로 밑에 작성해 줍니다.




user_key값도 불러왔으니 이제 각 분기마다 적용해줍니다.


https://github.com/M4ndU/inhun_kakao_chat_bot_2/blob/master/views.py

92~104


92
93
94
95
96
97
98
99
100
101
102
103
104
    if datacontent == '오늘 급식':
        if u0.check(user_key):
            return ret_proc(PlzStopIt)
        dt = datetime.datetime.today()
        diet = get_meal(dt)
        return ret_proc(diet)
 
    elif datacontent == '내일 급식':
        if u1.check(user_key):
            return ret_proc(PlzStopIt)
        dt = datetime.datetime.today() + datetime.timedelta(days=1)
        diet = get_meal(dt)
        return ret_proc(diet)
cs



        if u0.check(user_key):

            return ret_proc(PlzStopIt)


를 통해서 u0.pre_key = u0.now_key가 되어 1을 반환하게 되면

조건이 True가 되어 밑에 작업을 진행하지 않고 바로 문자열을 바로 반환합니다.

그럴려면 PlzStopIt에 문자열을 저장해주어야 겠죠?


반응형
반응형

이번에는 이번에는 맴버 관련 기능을 구현해보도록 하겠습니다.


목차입니다.



1. 맴버가 서버 접속, 퇴장시 서버 텍스트 채널에 메세지를 보내는 기능

2. 맴버가 서버 접속시 1:1 채팅에 메세지를 보내는 기능

3. 주기적으로 공지메세지를 보내는 기능 (백그라운드로 동작)










1. 맴버가 서버 접속, 퇴장시 서버 텍스트 채널에 메세지를 보내는 기능



먼저 코드를 봅시다.


1
2
3
4
5
6
7
8
9
10
11
@client.event
async def on_member_join(member):
    fmt = '{1.name} 에 오신것을 환영합니다., {0.mention} 님'
    channel = member.server.get_channel("channel_id_here")
    await client.send_message(channel, fmt.format(member, member.server))
 
@client.event
async def on_member_remove(member):
    channel = member.server.get_channel("channel_id_here")
    fmt = '{0.mention} 님이 서버에서 나가셨습니다.'
    await client.send_message(channel, fmt.format(member, member.server))
cs



on_member_join 함수는 맴버가 서버에 들어 왔을 때 실행되는 함수이고,

on_member_remove 함수는 맴버가 서버를 나갔을 때 실행되는 함수입니다.


format를 통해서 {0.mention}  에는 맴버가 언급되고,  {1.name} 에는 서버이름이 자동적으로 대입됩니다.


channel_id_here에 들어갈 id를 찾는 방법은 본문 하단에 설명되어 있습니다.









2. 맴버가 서버 접속시 1:1 채팅에 메세지를 보내는 기능



간단합니다. 아래의 코드를 이용하면 끝입니다.


await client.send_message(member, "내용")


여기서 우리는 맴버가 서버에 들어왔을 때 보내도록 할 것이므로, on_member_join 함수 밑에 두겠습니다.



1
2
3
4
5
6
@client.event
async def on_member_join(member):
    fmt = '{1.name} 에 오신걸 환영합니다, {0.mention} 님'
    channel = member.server.get_channel("channel_id_here")
    await client.send_message(channel, fmt.format(member, member.server))
    await client.send_message(member, "내용")
csv



"내용" 에 원하는 내용을 작성하시면 됩니다.









3. 주기적으로 공지사항등의 메세지를 보내는 기능입니다.



백그라운드로 동작합니다.


코드입니다. :


1
2
3
4
5
6
    async def my_background_task():
        await client.wait_until_ready()
        channel = discord.Object(id='channel_id_here')
        while not client.is_closed:
            await client.send_message(channel, "hi")
            await asyncio.sleep(5
cs


@client.event 가 필요 없습니다.



----------------channel_id_here 구하기---------------


channel_id_here 에 메세지를 보낼 텍스트 채널의 id를 입력해주시면 되는데요.

id를 확인하는 방법은 다음과 같습니다:

1. 디스코드 채널을 웹을 통해 접속한다.

2. 텍스트 채널에 들어간다.

3. URL마지막에 있는 18자리 숫자가 id다.




"hi" 대신에 보낼 메세지를 넣어주시면 됩니다.


await asyncio.sleep(5)

에서 5 대신에 자신이 원하는 시간 간격을 초 단위로 작성하여 주시면 됩니다.


EX) await asyncio.sleep(60*60*24) 이라고 하면 1일마다 메세지를 보내게 됩니다.




이 부분을 다 작성하셨으면 전체 코드에서 하단으로 가셔서 client.run() 부분을 찾습니다.

client.run() 코드 바로 위에 아래의 코드를 추가합니다 :


client.loop.create_task(my_background_task())


그러면


client.loop.create_task(my_background_task())

cleint.run('token')


처럼 됩니다.


만약 discord.errors.Forbidden: FORBIDDEN (status code: 403): Missing Access 이런 오류가 발생한다면

권한 문제이니 서버에서 봇의 권한을 설정해 주시면 됩니다.







전체 코드는 다음 링크를 참조해주세요.


https://github.com/M4ndU/inhun_discord_chat_bot_2/blob/master/inhun_bot.py

반응형
반응형

이번에는 저번에 만들었던 카카오톡 챗봇에 급식 파서를 활용하여

급식을 알려주는 챗봇을 만들도록 하겠습니다.



이전과정

[

카카오톡 챗봇 만들기 --> http://mandu-mandu.tistory.com/67

급식 파서 만들기 --> http://mandu-mandu.tistory.com/21

]





코드 작성하기


급식을 파싱해서 식단을 출력하는 부분을 구현할 것인데요.

두개의 코드상 차이는 날짜값과 요일값을 가져오는 부분밖에 차이가 없기 때문에
"오늘 급식"에 대한 코드를 작성하겠습니다.

그전에 parser.py를 view.py가 있는 디렉토리로 복사해줍니다.


코드는 그냥 한번에 다 작성했습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
from pytz import timezone
import datetime , json #datetime 모듈 import
from parser import * #parser.py import
 
 
def keyboard(request):
 
    return JsonResponse({
        'type':'buttons',
        'buttons':['오늘','내일']
    })
 
@csrf_exempt
def answer(request):
 
    json_str = ((request.body).decode('utf-8'))
    received_json_data = json.loads(json_str)
    datacontent = received_json_data['content']
 
    #date
    #오늘
    dt1 = datetime.datetime.today()
 
    local_date1 = dt1.strftime("%Y.%m.%d")
    local_weekday1 = dt1.weekday()
    #오늘
 
    #내일
    dt2 = datetime.datetime.today() + datetime.timedelta(days=1)
 
    local_date2 = dt2.strftime("%Y.%m.%d")
    local_weekday2 = dt2.weekday()
    #내일
    #date
 
    if datacontent == '오늘':
 
        #시간 관련
        meal_date = str(local_date1)
        l_wkday = int(local_weekday1)
        #시간 관련
 
        #파싱
        l_l = get_diet(2, meal_date, l_wkday)
        d_d = get_diet(3, meal_date, l_wkday)
        #파싱
 
        #디스코드 챗봇 만들기(2) 참고바람.
        if len(l_l) == 1:
            lunch = "급식이 없습니다."
            dinner = ""
        elif len(d_d) == 1:
            lunch = meal_date + " 중식\n" + l_l
            dinner = ""
        else:
            lunch = meal_date + " 중식\n" + l_l
            dinner = meal_date + " 석식\n" + d_d
        #디스코드 챗봇 만들기(2) 참고바람.
        
        return JsonResponse({
                'message': {
                    'text': lunch + dinner
                },
                'keyboard': {
                    'type':'buttons',
                    'buttons':['오늘','내일']
                }
 
            })
 
    elif datacontent == '내일':
...
cs


*'내일'에 대한 부분은 '오늘'의 코드와 동일합니다. '#시간관련' 부분의 변수명만 바꾸어서 사용하시면 됩니다.

*#파싱 부분은 중식과 석식만 파싱해옵니다. 조식을 파싱할땐 함수의 첫번째 인자로 1을 주시면 됩니다.

*#디스코드 챗봇 만들기(2) 참고바람. 부분은 http://mandu-mandu.tistory.com/65 여기에 설명이 나와 있습니다.

*자신의 상황에 맞게 수정 또는 삭제 하시면 됩니다.


*'오늘'과 '내일'의 코드는 거의 차이가 없기 때문에 함수 하나로 잘 만들어서 사용하시는게 더 좋을 것 같습니다.

--18.03.03 추가

그래서 함수 하나로 만들어서 최적화를 진행하였습니다.

수정된 코드는 제 깃허브에 있습니다.

https://github.com/M4ndU/inhun_kakao_chat_bot_2

----------


저장하시면 만약 django가 돌아가는 중이었다면 자동으로 서버를 재시작해서 바로 수정된 것을 적용합니다.

모바일 카카오톡으로 접속해서 봇에서 '오늘' 또는 '내일' 버튼을 누르면 해당 버튼에 대한 식단이 출력됩니다.





이상입니다.

반응형
반응형

이 글만을 통해서 간단한 텍스트를 출력하는 카카오톡 챗봇을 만들 수 있습니다.


카카오톡 플러스 친구를 이용합니다.




카카오톡 플러스 친구 생성하기



카카오톡 관리자 센터 페이지로 이동합니다.






플러스친구 만들기를 클릭합니다.


카카오톡 계정으로 로그인을 합니다.




모두 작성하고 확인을 클릭합니다.







Django 설치 및 설정하기


https://tutorial.djangogirls.org/ko/installation/


이곳에 설치 방법이 잘 나와있습니다.

저는 가상환경을 사용하지 않았습니다.




<linux>  django 설치하기


~$ pip3 install --upgrade pip


~$ pip3 install django






프로젝트 생성


이제 프로젝트를 생성하겠습니다.


이 내용도

https://tutorial.djangogirls.org/ko/django_start_project/

여기에 잘 나와있습니다.



생성한뒤 폴더나 파일의 이름을 바꾸어선 안됩니다.


$ django-admin startproject [프로젝트 이름] .

마침표 (.) 은 필수입니다.


$ python3 manage.py startapp [어플리케이션 이름]

어플리케이션도 같이 만들겠습니다.


example :

~/project$ mkdir tistory

~/project$ cd tistory/

~/project/tistory$ django-admin startproject mybot .

~/project/tistory$ ls

manage.py  mybot

~/project/tistory$ python3 manage.py startapp testest



$ python3 manage.py migrate

이것도 해줍니다.



설정 변경


다음과 같이 변경합니다.


mybot/settings.py

25: # SECURITY WARNING: don't run with debug turned on in production!

26: DEBUG = False

27: 

28: ALLOWED_HOSTS = ['*']


* DEBUG는 테스트를 위해선 True, 실제 사용시에는 False로 해두세요. 그냥 False로 해두시는 것을 추천

* ALLOWED_HOSTS=['*']는 모든 아이피를 허용한다는 의미입니다.


mybot/settings.py

31: # Application definition

32: 

33: INSTALLED_APPS = [

34:     'django.contrib.admin',

35:     'django.contrib.auth',

36:     'django.contrib.contenttypes',

37:     'django.contrib.sessions',

38:     'django.contrib.messages',

39:     'django.contrib.staticfiles',

40:     'testtest',

41: ]


40:  에 어플리케이션 이름을 추가합니다.



mybot/settings.py

108: TIME_ZONE = 'Asia/Seoul'



mybot/urls.py

16: from django.conf.urls import include, url

17: from django.contrib import admin

18: 

19: urlpatterns = [

20:     url(r'^testtest/', include('testtest.urls')),

21:     url(r'^admin/', admin.site.urls),

22: ]


16: include 를 추가합니다.

20: 를 추가합니다.    testtest/로 온 요청은 testtest/urls.py로 보내겠다는 의미



testtest/urls.py

1
2
3
4
5
6
7
8
9
from django.conf.urls import url
 
from . import views
 
urlpatterns = [
    url(r'^keyboard/', views.keyboard),
    url(r'^message$', views.answer),
]
 
cs


* mybot/ 경로가 아닌 testtest/ 경로입니다.

* 위 소스그대로 복사해서 파일을 만듭니다.





코드 작성하기



개발환경 : UBUNTU 16.04.3

개발언어: PYTHON 3.5.2

텍스트/코드 에디터: ATOM



testtest/views.py를 수정해주는 것입니다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
import json
 
 
 
def keyboard(request):
 
    return JsonResponse({
        'type':'buttons',
        'buttons':['오늘','내일']
    })
 
@csrf_exempt
def answer(request):
 
    json_str = ((request.body).decode('utf-8'))
    received_json_data = json.loads(json_str)
    datacontent = received_json_data['content']
 
    if datacontent == '오늘':
        today = "오늘 급식"
 
        return JsonResponse({
                'message': {
                    'text': today
                },
                'keyboard': {
                    'type':'buttons',
                    'buttons':['오늘','내일']
                }
 
            })
 
    elif datacontent == '내일':
        tomorrow = "내일 급식"
 
        return JsonResponse({
                'message': {
                    'text': tomorrow
                },
                'keyboard': {
                    'type':'buttons',
                    'buttons':['오늘','내일']
                }
 
            })
 
cs


8에서 13라인은 카카오톡에 버튼 정보를 보내는 부분.

15부터 48라인은 각 버튼에 대한 응답 내용.





카카오톡 플러스 친구 설정 및 실행, 테스트하기





관리자 센터에서 스마트채팅으로 들어갑니다.




api형 설정하기를 클릭합니다.





앱 이름, 앱 URL, 앱 설명을 작성합니다.


알림받을 전화번호는 카카오톡 가입에 사용한 전화번호를 사용하세요.



앱 URL은 자신의 서버URL을 사용합니다.


$ python3 manage.py runserver port


여기에 사용한 포트 그대로 사용


http://자신의아이피:포트/디렉토리


그리고 api테스트를 클릭하여 정상적으로 작동하는지 확인합니다.



그리고 API형 저정하기를 누릅니다.


시작하기를 눌러서 서비스를 시작합니다.




이제 모바일 카카오톡에 들어갑니다. (모바일 기기에서 진행하세요.)


플러스친구 채팅으로 이동해서


버튼이 잘 뜨는지 확인하고 버튼을 눌렀을때 정상적인 응답이 오는지 확인합니다.




다음은 급식 파서를 활용한 카카오톡 챗봇을 만들겠습니다.

반응형
반응형

이제 디스코드 봇에 부가적인 요소들을 추가해 보겠습니다.




예제 코드 참고하기



discord.py를 설치할때 들어갔던 링크로 들어가보시면,




examples 폴더에 여러 예제들이 있습니다.

거기에 있는 소스들을 가져다가 상황에 맞게 수정하셔서 사용하시면 됩니다.




~~~플레이 중 추가하기



타 봇들을 보면 프로필에 ~~~플레이 중 을 띄워서 도움말을 보는 명령어등을 알려주는 역할을 하죠.



이 것도 코드를 추가해서 할 수 있습니다.


@client.event

async def on_ready():

    print('Logged in as')

    print(client.user.name)

    print(client.user.id)

    print('---------')

    await client.change_presence(game=discord.Game(name="!a for help"))


이렇게요. (기존 코드에 진한 부분만 추가해 주면 됩니다.)








봇 꾸미기 - 메세지 예쁘게 꾸미기 (embeds)


http://discordpy.readthedocs.io/en/latest/api.html 이 문서에 자세히 기술되어 있습니다.



봇이 메세지를 보낼 때

사용자가 보낼 때 처럼 단순한 텍스트형식이 아닌

예쁜 텍스트 상자속에 텍스트가 담겨서 오는 경우가 있는데요.


예시로 ayana의 채팅스샷




이 것을 추가해 보죠.


한 가지만 해볼건데요.

필요한 것은 제목, 내용, 색상이 되겠습니다.


아래 코드처럼 기존 코드를 수정해주시면 되겠습니다.


            embed = discord.Embed(title="No Meal", description="급식이 없습니다.", color=0x00ff00)

            await client.send_message(message.channel, embed=embed)


결과 :



반응형
반응형

이번에는 저번에 만들었던 디스코드 챗봇에 급식 파서를 활용하여

급식을 알려주는 챗봇을 만들도록 하겠습니다.



이전과정

[

디스코드 챗봇 만들기 --> http://mandu-mandu.tistory.com/64

급식 파서 만들기 --> http://mandu-mandu.tistory.com/21

]






코드 작성하기


필요한 모듈로는, datetime 모듈과 만들어둔 parser.py가 필요합니다.


parser.py를 디스코드 봇 소스가 있는 디렉토리로 옮겨두고


아래 코드를, 디스코드 봇 소스 상단에 추가해줍시다.



import datetime

from parser import *





내일의 식단을 출력하기



먼저, 내일의 식단을 출력하는 코드를 작성하겠습니다.


코드를 작성하기 위해 내일의 식단을 출력하는 그 과정을 구상해 봅시다.


1. 디스코드에서 사용자로부터 명령어를 인식합니다.

저는 '!t' 로 설정하겠습니다.


2. 명령어를 인식한 시점으로부터 다음날의 날짜값과 그 날짜의 요일값을 가져옵니다.

파서의 함수가 필요로 하는 인자가 조식.중식.석식 여부 / 날짜 / 요일이기 때문입니다.

모두 datetime 함수를 사용합니다.


3. 급식을 파싱하는 함수를 호출합니다. 그리고 그 값을 저장합니다.


4. 그 값을 출력합니다.

제가 다니는 학교의 급식 상황을 보면, 조식이 전혀 없고 중식이 없을 경우 100% 석식이 없습니다.

이에 따라 조건절을 추가하여 출력되는 메세지를 '최적화'하였습니다.

(식단 내용 외 날짜등의 정보도 같이 출력해주기 때문입니다.)


중식이 없을 경우 급식이 없다고 출력,

중식이 있고 석식은 없을 경우 중식만 출력,

중식 석식 모두 있을 경우 모두 출력.



이를 코드로 구현하면 아래처럼 됩니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    if message.content.startswith('!to'):    #메세지의 내용의 시작이 !to로 시작할 경우
        to_tomorrow = datetime.datetime.today() + datetime.timedelta(days=1)    #오늘 날짜에 하루를 더함
        local_date2 = to_tomorrow.strftime("%Y.%m.%d")    #위에서 구한 날짜를 년.월.일 형식으로 저장
        local_weekday2 = to_tomorrow.weekday()    #위에서  구한 날짜의 요일값을 저장
 
        l_diet = get_diet(2, local_date2, local_weekday2)    #점심식단을 파싱해옴
        d_diet = get_diet(3, local_date2, local_weekday2)    #석식식단을 파싱해옴
 
        if len(l_diet) == 1:    #점심식단의 길이가 1일경우 = parser.py에서 식단이 없을경우 공백한자리를 반환함.
            await client.send_message(message.channel, "급식이 없습니다.")    #급식이 없다고 메세지 보냄
        elif len(d_diet) == 1:    #점심식단의 길이가 1이 아니고 석식식단의 길이가 1일경우 = 점심식단만 있을경우
            lunch = local_date2 + " 중식\n" + l_diet    #날짜와 "중식"을 앞에 붙여서
            await client.send_message(message.channel, lunch)    #메세지 보냄
        else:    #둘다 길이가 1이 아닐경우 = 점심, 석식 식단 모두 있을 경우
            lunch = local_date2 + " 중식\n" + l_diet    #앞에 부가적인 내용을 붙여서
            dinner = local_date2 + " 석식\n" + d_diet
            await client.send_message(message.channel, lunch)    #메세지를 보냄
            await client.send_message(message.channel, dinner)
cs



코드상 

@client.event

async def on_message(message):

아래에 위치하게 됩니다.



상황에 따라 if가 이미 있을 경우

if message.content.startswith('!to'):

if를 elif로 바꿔서 사용해야 합니다. (파이썬의 기본...)





특정 날짜의 식단을 출력하기



이제 날짜값을 주면 그 날짜의 급식을 출력하는 코드를 작성하겠습니다.


코드의 구성은 위 '내일의 식단을 출력하기'의 코드에서 2번의 내용만 수정해 주면 됩니다.


1. 디스코드에서 사용자로부터 명령어를 인식합니다.

저는 '!t' 로 설정하겠습니다.


2. 날짜를 입력하라는 메세지를 보냅니다. 그리고 받은 날짜의 요일값을 구합니다. (코드로 구현하면 길어집니다.)

datetime 모듈을 이용합니다.


3. 급식을 파싱하는 함수를 호출합니다. 그리고 그 값을 저장합니다.


4. 그 값을 출력합니다.

제가 다니는 학교의 급식 상황을 보면, 조식이 전혀 없고 중식이 없을 경우 100% 석식이 없습니다.

이에 따라 조건절을 추가하여 출력되는 메세지를 '최적화'하였습니다.

(식단 내용 외 날짜등의 정보도 같이 출력해주기 때문입니다.)


중식이 없을 경우 급식이 없다고 출력,

중식이 있고 석식은 없을 경우 중식만 출력,

중식 석식 모두 있을 경우 모두 출력.


이를 코드로 구현하면 아래와 같습니다.


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
   elif message.content.startswith('!g'):
        await client.send_message(message.channel, '날짜를 보내주세요...')    #날짜를 보내달라는 메세지를 보냄
        meal_date = await client.wait_for_message(timeout=15.0, author=message.author)    #제한시간은 15초
 
        if meal_date is None:    #값이 존재하지 않거나 시간이 초과되었을 경우
            await client.send_message(message.channel, '15초내로 입력해주세요. 다시시도 : !g')    #다시 시도하라는 메세지를 보냄
            return
 
        else:    #값이 있다면
            meal_date = str(meal_date.content) # str형으로 변환, (사용자로부터 20180219와 같은 형태로 받아야 합니다.)
            meal_date = '20' + meal_date[:2+ '.' + meal_date[2:4+ '.' + meal_date[4:6]    # 2018.02.19 사이에 점을 추가함
            #(사용자로부터 점이 포함된 값으로 받을경우 위 코드를 삭제해도 됩니다.)
 
            s = meal_date.replace('.'', ')     # 2018, 02, 19 점을 반점으로 교체
            ss = "datetime.datetime(" + s + ").weekday()"    #eval함수를 통해 요일값을 구하기 위한 작업
            try:
                whatday = eval(ss)    #요일값을 구해서 whatday에 저장
            except:    #오류가 날 경우 다시 시도하라는 메세지를 보냄
                await client.send_message(message.channel, '올바른 값으로 다시 시도하세요 : !g')
                return
#이하 '내일 식단을 출력하기'와 같음
            l_diet = get_diet(2, meal_date, whatday)
            d_diet = get_diet(3, meal_date, whatday)
 
            if len(l_diet) == 1:
                l_diet = "급식이 없습니다."
                await client.send_message(message.channel, embed=l_diet)
            elif len(d_diet) == 1:
                lunch = meal_date + " 중식\n" + l_diet
                await client.send_message(message.channel, embed=lunch)
            else:
                lunch = meal_date + " 중식\n" + l_diet
                dinner = meal_date + " 석식\n" + d_diet
                await client.send_message(message.channel, lunch)
                await client.send_message(message.channel, dinner)
 
cs

파서의 함수를 호출하는 부분에서 윗부분이 많이 복잡해진 것을 알 수 있습니다.

아래부분은 기존과 변화가 없기 때문에 주석을 달지 않았습니다.



위 둘을 합친 전체 코드는 아래와 같게 됩니다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import discord
import asyncio
import datetime
from parser import *
 
 
client = discord.Client()
 
@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')
 
@client.event
async def on_message(message):
    if message.content.startswith('!to'):    #메세지의 내용의 시작이 !to로 시작할 경우
        to_tomorrow = datetime.datetime.today() + datetime.timedelta(days=1)    #오늘 날짜에 하루를 더함
        local_date2 = to_tomorrow.strftime("%Y.%m.%d")    #위에서 구한 날짜를 년.월.일 형식으로 저장
        local_weekday2 = to_tomorrow.weekday()    #위에서  구한 날짜의 요일값을 저장
 
        l_diet = get_diet(2, local_date2, local_weekday2)    #점심식단을 파싱해옴
        d_diet = get_diet(3, local_date2, local_weekday2)    #석식식단을 파싱해옴
 
        if len(l_diet) == 1:    #점심식단의 길이가 1일경우 = parser.py에서 식단이 없을경우 공백한자리를 반환함.
            await client.send_message(message.channel, "급식이 없습니다.")    #급식이 없다고 메세지 보냄
        elif len(d_diet) == 1:    #점심식단의 길이가 1이 아니고 석식식단의 길이가 1일경우 = 점심식단만 있을경우
            lunch = local_date2 + " 중식\n" + l_diet    #날짜와 "중식"을 앞에 붙여서
            await client.send_message(message.channel, lunch)    #메세지 보냄
        else:    #둘다 길이가 1이 아닐경우 = 점심, 석식 식단 모두 있을 경우
            lunch = local_date2 + " 중식\n" + l_diet    #앞에 부가적인 내용을 붙여서
            dinner = local_date2 + " 석식\n" + d_diet
            await client.send_message(message.channel, lunch)    #메세지를 보냄
            await client.send_message(message.channel, dinner)
 
 
   elif message.content.startswith('!g'):
        await client.send_message(message.channel, '날짜를 보내주세요...')    #날짜를 보내달라는 메세지를 보냄
        meal_date = await client.wait_for_message(timeout=15.0, author=message.author)    #제한시간은 15초
 
        if meal_date is None:    #값이 존재하지 않거나 시간이 초과되었을 경우
            await client.send_message(message.channel, '15초내로 입력해주세요. 다시시도 : !g')    #다시 시도하라는 메세지를 보냄
            return
 
        else:    #값이 있다면
            meal_date = str(meal_date.content) # str형으로 변환, (사용자로부터 20180219와 같은 형태로 받아야 합니다.)
            meal_date = '20' + meal_date[:2+ '.' + meal_date[2:4+ '.' + meal_date[4:6]    # 2018.02.19 사이에 점을 추가함
            #(사용자로부터 점이 포함된 값으로 받을경우 위 코드를 삭제해도 됩니다.)
 
            s = meal_date.replace('.'', ')     # 2018, 02, 19 점을 반점으로 교체
            ss = "datetime.datetime(" + s + ").weekday()"    #eval함수를 통해 요일값을 구하기 위한 작업
            try:
                whatday = eval(ss)    #요일값을 구해서 whatday에 저장
            except:    #오류가 날 경우 다시 시도하라는 메세지를 보냄
                await client.send_message(message.channel, '올바른 값으로 다시 시도하세요 : !g')
                return
            #이하 '내일 식단을 출력하기'와 같음
            l_diet = get_diet(2, meal_date, whatday)
            d_diet = get_diet(3, meal_date, whatday)
 
            if len(l_diet) == 1:
                l_diet = "급식이 없습니다."
                await client.send_message(message.channel, embed=l_diet)
            elif len(d_diet) == 1:
                lunch = meal_date + " 중식\n" + l_diet
                await client.send_message(message.channel, embed=lunch)
            else:
                lunch = meal_date + " 중식\n" + l_diet
                dinner = meal_date + " 석식\n" + d_diet
                await client.send_message(message.channel, lunch)
                await client.send_message(message.channel, dinner)
 
client.run('token')
 
cs



# 글을 작성하면서 보니

#[23,35]과 [59,72]가 하는 일이 같은데

#함수로 하나 정의해서 같이 쓰는게

#효율적일 것 같다는 생각이 듭니다..


#지금은 귀찮으니 PASS


#############

18.03.03 추가

!g 를 통해 특정 날짜의 급식을 부르는 과정에서

날짜값을 받고 처리하는 과정에서 한자리 달의 경우 오류가 발생,

이 부분을 수정하여 한자리 달의 경우도 가능합니다.

수정된 코드는 깃허브를 확인.

https://github.com/M4ndU/inhun_discord_chat_bot_2

############


반응형
반응형

이 글만을 통해서 기본적으로 텍스트를 출력하는 디스코드 봇을 만들 수 있습니다.


1. 봇 생성하고 초대하기

2. 코드 작성하기

3. 테스트 하기




봇 생성하고 초대하기


먼저 봇 계정을 생성해 주어야 합니다.



디스코드 홈페이지로 이동합니다. -> https://discordapp.com/



상단에 알아보기 > 개발자





APPLICATIONS > My Apps 에서 New App을 누릅니다.





이름, 설명, 아이콘을 설정하고 Create App을 누릅니다.





내려서 Create a Bot User을 누릅니다.





Yes, do it!





Token: click to reveal 을 눌러서 토큰 값을 확인합니다. 이 값은 외부에 유출되어서는 안됩니다.


Public Bot을 체크하게 되면 누구나 봇을 서버에 추가할 수 있게 됩니다. 체크하지 않을 경우 오직 주인만이 서버에 추가할 수 있습니다.


Require OAuth2 Code Grant 는 모르겠습니다.. 아시는 분은 덧글 남겨주세요 ㅎㅎ





이번에는 봇 추가 링크를 만들겠습니다.

Generate OAuth2 URL을 클릭합니다.





CLIENT ID에 클라이언트 아이디를 입력합니다. 아마 자동으로 입력되어 있을거에요.

SCOPES에서 bot에 체크합니다.


그러면 밑에 탭이 하나 더 생깁니다.





권한 설정을 할 수 있는데요. 저는 Send Messages 와 Read Message History 이 두개만 체크해 두었습니다.


다시 위로 올라가서 생성된 URL을 복사합니다.


복사된 url로 접속하시고 자신의 서버를 선택하신뒤

승인 버튼을 누르고 리캡챠 체크 하시면 자신의 서버에 봇이 들어와 있게 됩니다.


현재 봇은 오프라인 상태가 됩니다.




코드 작성하기



개발환경 : UBUNTU 16.04.3

개발언어: PYTHON 3.5.2

텍스트 에디터: ATOM


디스코드 봇을 파이썬으로 작성하기 편하게 제작된 모듈이 있습니다.

아래 링크에 들어가서 discord.py를 설치합니다.

설치방법은 아래 링크에 모두 있습니다.


 바로가기 <-- (https://github.com/Rapptz/discord.py)




설치를 마쳤다면,

이제 텍스트 에디터로 가서 소스를 작성해 봅시다.



[2019년 공지사항]

discord.py 모듈의 업데이트로 client.send_message를 사용할 수 없습니다.

대신 message.channel.send를 이용하시면 됩니다.

아래 코드에는 수정되지 않은 상태로 올라가있으니 수정하셔서 사용하시면 됩니다.





기본적인 소스는 다음과 같습니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import discord
import asyncio
 
client = discord.Client()
 
@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')
 
@client.event
async def on_message(message):
    if message.content.startswith('!test'):
        await client.send_message(message.channel, 'test!!!!')
 
    elif message.content.startswith('!say'):
        await client.send_message(message.channel, 'leave message')
        msg = await client.wait_for_message(timeout=15.0, author=message.author)
 
        if msg is None:
            await client.send_message(message.channel, '15초내로 입력해주세요. 다시시도: !say')
            return
        else:
            await client.send_message(message.channel, msg.content)
 
client.run('token')
 
cs



위에서부터 소스를 분석해보죠.


@client.event

async def on_ready():

    print('Logged in as')

    print(client.user.name)

    print(client.user.id)

    print('------')



봇을 실행하여 로그인에 성공하면, 위에 문장을 출력하게 됩니다.

(터미널 or 콘솔창에 출력됩니다.)

디스코드 내에서는 봇이 온라인으로 표시됩니다.




@client.event

async def on_message(message):

    if message.content.startswith('!test'):

        await client.send_message(message.channel, 'test!!!!')

 


디스코드 텍스트 채널에 !test 라고 메세지를 보내면 봇이 test!!!!라고 메세지를 보냅니다.




    elif message.content.startswith('!say'):

        await client.send_message(message.channel, 'leave message')

        msg = await client.wait_for_message(timeout=15.0, author=message.author)

 


디스코드 텍스트 채널에 !say 라고 메세지를 보내면 봇이 leave message 라고 메세지를 보냅니다.

그리고 15초 동안 그 사용자의 메세지를 기다립니다. 그리고 그 사용자의 메세지를 msg에 저장합니다.


제한시간은 timout의 값을 변경하여 설정할 수 있습니다.




        if msg is None:

            await client.send_message(message.channel, '15초내로 입력해주세요. 다시시도: !say')

            return

        else:

            await client.send_message(message.channel, msg.content)

 


msg의 값이 존재하지 않을경우 (=사용자가 메세지를 보내지 않았을 경우)

문장을 하나 출력합니다.


msg의 값이 존재할 경우 (=사용자가 메세지를 보냈을 경우)

그 메세지를 그대로 보냅니다.




client.run('token')



token자리에 자신의 봇의 토큰값으로 코드를 수정해 주고 실행해야 합니다.


ex) client.run('AAAAAAA.111111111_BBBBBB')


작은따옴표 주의







테스트 하기



코드를 작성하고 저장을 했다면, 이제 실행을 해줍시다.



터미널에서 봇이 로그인이 되었는지 확인하고 디스코드로 갑니다.




로그인이 되었다면, 봇이 온라인으로 표시될 겁니다.




텍스트 채널에서 메세지를 보내서 봇이 정상적으로 동작하는지 확인합니다.




만약 디스코드내에서 봇이 응답하지 못하고, 터미널에서 위와같은 오류가 났을경우,

디스코드 텍스트 채널에서 권한이 없어서 메세지를 보내지 못하는 것이므로

봇에 권한설정을 해 주시면 해결됩니다.

반응형

+ Recent posts