본문으로 바로가기

[discord.py 2.0] 08. Embed 꾸미기

category 정보/Discord Bot 2022. 9. 7. 15:19
728x90
반응형

이전글보기

[정보/Discord Bot] - [discord.py 2.0] 04. discord.py 라이브러리 설치하기

[정보/Discord Bot] - [discord.py 2.0] 05. 봇 기본설정하기

[정보/Discord Bot] - [discord.py 2.0] 06. 봇 명령어 추가하기

[정보/Discord Bot] - [discord.py 2.0] 07. 대화에 Embed 추가하기


이번 포스팅에서는 discord.py 의 Embed Class에서 지원하는 기능을 알아보면서 Embed를 꾸며볼겁니다.

 

Embed class의 기본 지원형식은 다음과 같습니다.

 

이전에 작성했던 Embed 코드에서 추가해보겠습니다.

@app.command(name="핑")
async def ping(ctx):
    embed = discord.Embed(title="ping pong", description="핑퐁")
    await ctx.reply('퐁', embeds=[embed, embed])

위 코드에서 생성하는 Embed 클래스의 생성자와, 메소드를 이용하여 Embed를 꾸미게 됩니다.

뭐가 어떤건지 헷갈릴 수 있으니 전부다 끼얹어보았습니다.

@app.command(name="핑")
async def ping(ctx):
    embed = discord.Embed(title="ping pong", description="핑퐁", colour=0xffffff)
    embed.set_author(name='Author name', icon_url="https://han.gl/XBMeC")
    embed.set_footer(text="footer", icon_url="https://han.gl/XBMeC")
    embed.set_image(url="https://han.gl/XBMeC")
    embed.set_thumbnail(url="https://han.gl/XBMeC")
    embed.add_field(name="필드1", value="inline false 필드1 내용", inline=False)
    embed.add_field(name="필드2", value="inline false 필드2 내용", inline=False)
    embed.add_field(name="필드3", value="inline True 필드3 내용", inline=True)
    embed.add_field(name="필드4", value="inline True 필드4 내용", inline=True)
    await ctx.reply('퐁', embed=embed)

위처럼 입력할 경우 결과는 다음과 같습니다.

embed.set_author(name='Author name', icon_url="https://han.gl/XBMeC")

①: Author : Embed의 최상단 영역입니다. icon_url을 추가하시면 이미지가 붙습니다

embed = discord.Embed(title="ping pong", description="핑퐁", colour=0xffffff)

②: title : Embed의 제목입니다 - Embed 생성시에 할당합니다

③: description : Embed의 내용입니다 - Embed 생성시에 할당합니다

      colour은 Embed왼편에 있는 색상을 결정합니다.

embed.add_field(name="필드1", value="inline false 필드1 내용", inline=False)
embed.add_field(name="필드2", value="inline false 필드2 내용", inline=False)
embed.add_field(name="필드3", value="inline True 필드3 내용", inline=True)
embed.add_field(name="필드4", value="inline True 필드4 내용", inline=True)

④: field : Embed 내부에 추가적인 공간을 넣습니다. inline이 true일 경우, compact 하게 넣고, inline이 false 인 경우, 한 field가 한줄을 모두 차지합니다.

embed.set_thumbnail(url="https://han.gl/XBMeC")

⑤: thumbnail : 썸네일을 등록합니다.

embed.set_image(url="https://han.gl/XBMeC")

⑥: image : Embed에 이미지를 추가합니다. 이미지의 사이즈는 embed내에서 별도로 지정할 수 없으며, 크기를 변경하시려면 미리 resize를 하셔야 합니다.

embed.set_footer(text="footer", icon_url="https://han.gl/XBMeC")

⑦: footer : Embed의 맨 아래에 footer를 추가합니다.

 

각 필드에 따라서 필요하신 내용만 추가하여 꾸미시면 되겠습니다.

 

현재까지 진행된 소스코드입니다.

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
import asyncio
import discord
from discord.ext import commands
 
intents = discord.Intents.all()
app = commands.Bot(command_prefix='!', intents=intents)
 
 
async def main():
    async with app:
        file = open("discord_token.txt")
        bot_token = file.readline()
        file.close()
        await app.start(bot_token)
 
 
@app.command()
async def ping(ctx):
    await ctx.send('pong')
 
 
@app.command(name="핑")
async def ping(ctx):
    embed = discord.Embed(title="ping pong", description="핑퐁", colour=0xffffff)
    embed.set_author(name='Author name', icon_url="https://han.gl/XBMeC")
    embed.set_footer(text="footer", icon_url="https://han.gl/XBMeC")
    embed.set_image(url="https://han.gl/XBMeC")
    embed.set_thumbnail(url="https://han.gl/XBMeC")
    embed.add_field(name="필드1", value="inline false 필드1 내용", inline=False)
    embed.add_field(name="필드2", value="inline false 필드2 내용", inline=False)
    embed.add_field(name="필드3", value="inline True 필드3 내용", inline=True)
    embed.add_field(name="필드4", value="inline True 필드4 내용", inline=True)
    await ctx.reply('퐁', embed=embed)
 
 
asyncio.run(main())
 
 

 

참고로 Embed에 포함되는 image는 기본적으로 최대 1장인 것으로 알고 있습니다 (정확히는 썸네일1장+이미지1장 씩 해서 총 2장)

추가로 Attribute 상에서는 video url이 추가가 되지만, 접근할 수 있는 방법을 확인하지 못했습니다..

이 내용은 이후에라도 확인이 될 경우 추가로 포스팅을 하도록 하겠습니다.


포스팅에 사용된 모든 소스코드는 아래 Github에서 확인하실 수 있습니다.

https://github.com/aochfl/ChoRi_TestBot

 

GitHub - aochfl/ChoRi_TestBot

Contribute to aochfl/ChoRi_TestBot development by creating an account on GitHub.

github.com


참고자료

https://discord.com/developers/docs/reference - ( Discord Developer API )

https://discordpy.readthedocs.io/en/latest/index.html  -  ( discord.py library 문서 )

 

728x90
반응형