-
kick-off-bot(1)프로젝트/Kick-off-bot 2021. 12. 21. 23:17
본격적인 코딩에 들어가 보자, 제일 먼저 작성한 부분은 내가 만드는 봇이 어떤 권한을 가지고 어떤 행동을 할 건지에 대한 부분을 명시하는 부분이다. 어찌 보면 많이 놓치는 부분으로 썸네일 같은 에러를 보게 만드는 부분이니 봇 구현 이전에 확실하게 작성을 해두면 좋다.
const { Client, MessageActionRow, MessageButton, MessageEmbed } = require("discord.js"); const { token } = require("./.config.json"); const client = new Client({ intents: [32767] });
첫 줄은 discord.js에서 내가 사용할 모듈을 나열하여 가져왔다. 내가 생각하기에 제일 기본적인 모듈들이지 않을까 싶다. 메세지를 보내고 버튼을 만드는 것이 제일 기초 예제이기도 하기 때문이다.
두 번째 라인은 봇이 디스코드에 로그인 하기 위한 토큰 값을. config.json에 적어놓았고 그 값을 가져온 것이다. token 같은 건 한번 뚫리면 끝이기 때문에 깃헙에도 푸시하면 안 된다. 그러나 좋은 소식은 어떤 로직인지 모르겠지만 내가 실수로 깃헙에 토큰을 올렸을 때 디스코드에서 토큰이 공개되었다며 기존의 토큰을 지우고 새 토큰을 발행했다고 메일을 보내온다. 그래서 걱정할 필요는 없지만 그래도 조심해야 하는 부분이다.
마지막 client로 선언한 부분은 디스코드 봇의 핵심인 권한 부분이다. 내가 적어둔 32767이라는 숫자는 모든 권한에 접근하겠다는 값이며 보통은 ({ intents: ["GUILDS", "GUILD_MESSAGES"] }) 처럼 일일이 사용할 권한을 세팅해 주어야 하는데 위와 같이 선언하면 간단하다. 물론 필요한 것만 가져와야 한다는 것에 동의한다. 비트 연산자를 아는 사람은 아래 사이트에서 필요한 권한을 세팅해 볼 수 있겠다.
그리고 작성한 코드는 앞 포스트의 예제에도 나와있는 한번만 실행하는 함수이다.
client.once("ready", () => { console.log("Ready!"); client.user.setActivity("리그", { type: "WATCHING" }); });
client.user.setActivity
이 부분을 추가하게 된 이유는 밋밋한 로그인 상태를 바꿔보고 싶었기 때문이다. 디스코드를 이용해본 사람은 알 수도 있는데 채널에 접속한 누군가가 게임을 하거나 디스코드가 감지할 수 있는 행동을 하고 있을 때 ~~ 하는 중 같은 문구를 프로필 밑에 적어준다. 만드는 봇은 축구가 주제인 채널이었기 때문에 '리그 시청 중'이라는 문구를 달아주었다.마지막으로 적는 부분은 모든 코드의 제일 하단에 적는 , 가장 중요한 부분이다. 봇이 로그인을 하는 부분이다.
client.login(token);
once 함수와 login 사이에서 이제 당신들의 원하는 모든 코드를 구현할 준비가 되었다. 다음에는 ping-pong에 대한 예제를 자세하게 살펴보려고 한다.
728x90반응형LIST'프로젝트 > Kick-off-bot' 카테고리의 다른 글
kick-off-bot(3) - develop (0) 2022.01.06 kick-off-bot(2) / ping-pong (0) 2021.12.30 무엇을 먼저 알아야할까? (0) 2021.12.19 디스코드 봇을 만들어달라고? (0) 2021.12.13