Python/Discord API

discord.py에서 앱 버튼(Context Menu) 구현 방법

jimmy_AI 2024. 3. 30. 00:26
반응형

파이썬으로 디스코드 앱을 구현할 때,

아래와 같은 이미지의 앱 버튼을 구현하는 방법을 간략하게 정리해보도록 하겠습니다.

 

 

Python Discord API Context Menu 구현 예제

discord bot 선언 후에, 다음과 같이 context_menu 데코레이터를 활용하면

위와 같은 앱 버튼 형태를 구현할 수 있습니다.

import discord
from discord.ext import commands

# 봇 선언 부분 예시
intents = discord.Intents.all()
intents.messages = True
intents.message_content = True

bot = commands.Bot(command_prefix="!", intents=intents)

# 앱 버튼 생성에 필요한 코드
@bot.tree.context_menu(name="메뉴 이름")
async def bot_reply_context(interaction: discord.Interaction, message: discord.Message):
    await interaction.response.send_message(f"원본 메시지 내용: {message.content}")
    
# 앱 실행 부분 예시
TOKEN = "디스코드 앱 토큰 번호"
bot.run(TOKEN)

 

위와 같이 코드를 작성해주시고, 앱 코드를 실행하면

name 인자로 주신 "메뉴 이름"에 해당하는 앱 버튼이 생성되며,

앱 버튼을 클릭했던 원본 메시지의 텍스트가 그대로 출력되는 것을 확인하실 수 있습니다.