동아리 디스코드 서버를 운영하는데, 공지사항의 UI를 깔끔하게 만들고자 임베드 메시지를 출력하는 봇을 구현했다.
환경 설정
봇을 등록하기 위해, '디스코드 개발자 포털' 접속
Discord Developer Portal — API Docs for Bots and Developers
Integrate your service with Discord — whether it's a bot or a game or whatever your wildest imagination can come up with.
discord.com
등록 과정은 아래 주소를 참고했다.
실전! 초보를 위한 디스코드 봇 개발 with 파이썬 (2) - 봇 등록과 기본 명령어 추가하기
지난 시간에 작업 환경 만들기까지 완료했습니다. 이번 시간에는 봇을 등록하고, 필요한 패키지를 설치한 후, 본격적으로 봇 코딩을 시작하겠습니다. 1. 디스코드 앱 등록 디스코드 봇을 만드려
arpaap.tistory.com
discord, asyncio를 미리 pip로 설치하고, 파이선 파일로 다음 코드 작성하기
import discord, asyncio
token = "토큰 입력"
client = discord.Client()
prefix = "!"
@client.event
async def on_ready():
await client.change_presence(status=discord.Status.online, activity=discord.Game("공지"))
print("봇 실행됨!")
print(client.user.name)
print(client.user.id)
@client.event
async def on_message(message):
if message.author.bot:
return None
if message.content.startswith(prefix):
msg = message.content[1:]
else:
return None
if msg == "안녕":
await message.channel.send("안녕하세요!")
elif msg == "공지":
embed = discord.Embed(title="🔥KHUDA 디스코드에 오신 것을 환영합니다!", description="KHUDA는 팀원과 함께 성장하며, 본인의 한계를 뛰어넘는 문화를 추구합니다. \n\n", color=0xAAFFFF)
embed.add_field(name="♦️ 디스코드 개선사항 ", value="쿠다 디스코드의 발전을 위한 피드백이나 건의 사항은 5기 교육부로 연락 주시거나, ‘문의방’ 채널에 남겨주시면 감사하겠습니다.", inline=False)
await message.channel.send(embed=embed)
client.run(token)
☑️봇 실행됨! 문구: py파일이 정상적으로 실행될 때, 터미널에서 보이는 문구
☑️ '!' 으로 채널에 메시지를 입력해야 봇이 채널에 메시지를 띄움
- !안녕 -> 안녕하세요!
- !공지 -> 임베드로 작성한 공지사항 출력
☑️ inline=False: 한 줄 띄어진 후에 값 출력
☑️ inline=True: 같은 라인에 출력