디스 코드 ping - diseu kodeu ping

prefix 지정하기

prefix는 번역하면 접두사 라는 뜻을 가지고 있습니다. 한마디로 봇의 명령어를 사용할때 모든봇이 명령어를 겹치지 않도록 하는것은 힘드므로 prefix로 시작하는 명령어만 인식하도록 하는 방식입니다. 먼저 prefix를 config.js에 등록합니다.

module.exports = { token: 'TOKEN', prefix: '!' }

이제 메세지를 보냈을때 이벤트를 처리하는 코드를 작성합니다.

const Discord = require('discord.js') const config = require('./lib/config') const client = new Discord.Client() client.on('ready', () => { console.log('봇이 켜졌습니다.') }) client.on('message', (message) => { if(message.channel.type == 'dm') return if(!message.content.startsWith(config.prefix)) return if(message.content.startsWith(config.prefix + 'ping')) { message.channel.send(client.ping + ' ms') } }) client.login(config.token)

채널이 DM(개인 메세지)이거나 prefix로 시작하지 않았을 경우 리턴해서 이벤트를 취소합니다. 그리고 !ping으로 시작했을 경우 client.ping 으로 봇의 핑을 얻어옵니다. message.channel.send 로 메세지의 채널에 핑을 보냅니다. 성공하셨다면 아래와 같은 모양이 될 것입니다. 성공하셨다면 축하드립니다!

디스 코드 ping - diseu kodeu ping