Mojangcam

디스코드 봇 만들기 #2 - Slash Command에 옵션(Arguments) 추가하기! 본문

Python

디스코드 봇 만들기 #2 - Slash Command에 옵션(Arguments) 추가하기!

Mojangcam 2025. 4. 15. 00:00
반응형

안녕하세요! Mojangcam입니다 🙌
지난 시간에는 기본적인 슬래시 커맨드 /hello 를 만들어봤죠?

이번에는 사용자 입력값을 받아서 동작하는 Slash Command 를 만들어볼게요.
예를 들면 /say message:안녕 처럼 말이죠!


📦 준비된 코드

이번에도 Python과 discord.py 를 기반으로 합니다.
먼저 아래 코드를 참고해주세요👇

```python
import discord
from discord.ext import commands
from discord import app_commands

intents = discord.Intents.default()
bot = commands.Bot(command_prefix="/", intents=discord.Intents.all())

@bot.event
async def on_ready():
try:
synced = await bot.tree.sync()
await bot.change_presence(activity=discord.Game("디스코드 봇 상태메시지"))
print(f"synced {len(synced)} command(s)")
for i in synced:
print(f"command : {i}")
except Exception as e:
print(e)

기본 hello 명령어

@bot.tree.command(name="hello", description="간단한 인사 명령어")
async def hello(interaction: discord.Interaction):
await interaction.response.send_message("안녕하세요!")

옵션(Arguments)이 있는 say 명령어

@bot.tree.command(name="say", description="입력한 메시지를 따라 말해요!")
@app_commands.describe(message="반복할 메시지를 입력해주세요.")
async def say(interaction: discord.Interaction, message: str):
await interaction.response.send_message(f"당신이 한 말: {message}")

bot.run("YOUR_BOT_TOKEN")

 

🧪 실제 사용 모습

디스코드 채팅창에 아래처럼 입력하면?

/say message:안녕 나는 봇이야

👇 봇이 이렇게 대답합니다:

당신이 한 말: 안녕 나는 봇이야
 
 

📌 다양한 타입의 옵션도 가능해요!

타입설명예시
str 문자열 message:안녕하세요
int 숫자 count:3
bool true/false 선택 is_public:true
discord.User 유저 선택 user:@홍길동
discord.TextChannel 텍스트 채널 선택 channel:#일반

🧠 마무리

옵션을 받는 슬래시 커맨드는 봇의 기능을 한층 더 다양하게 만들어줍니다!
다음 시간에는 선택지를 제한하는 Enum 스타일 옵션,
또는 서브 커맨드 분류(Command Groups) 에 대해서도 알아볼게요!

궁금한 점은 댓글이나 방명록에 남겨주세요 🙌
읽어주셔서 감사합니다!

반응형