Mojangcam

디스코드 봇 공지 명령어 만들기 본문

Python

디스코드 봇 공지 명령어 만들기

Mojangcam 2023. 12. 16. 01:13
반응형

 

디스코드 봇 공지 명령어 만들기

디스코드 사용자라면 파이썬과 Discord.py를 이용해 간단하게 봇을 만들어 볼 만한 가치가 있습니다. 오늘은 그 중에서도 봇으로 공지 명령어를 만드는 방법을 소개하겠습니다.

1. 필요한 모듈 설치하기

먼저, 디스코드 봇을 만들기 위해서는 Discord.py 모듈이 필요합니다. 이 모듈을 설치하기 위해서는 pip(Python 패키지 관리자)를 사용합니다.

pip install discord.py

위의 명령어를 터미널에 입력하면 Discord.py를 설치할 수 있습니다.

2. 봇 명령어 생성하기

다음으로 봇 명령어를 생성해보겠습니다. "/공지"라는 명령어를 만들어서 공지사항을 전달하는 봇을 만들어 보겠습니다.

2-1. 봇 명령어 "/공지" 생성

봇 명령어 "/공지"를 생성하는 코드는 다음과 같습니다.

@bot.tree.command(name="공지", description="공지")
@app_commands.describe(content="내용")
async def notification(interaction: discord.Interaction, content: str):
    if not interaction.channel.id == 1106897001804730378: #공지를 보낼 특정 채널 아이디 입력
        await interaction.response.send_message("이 채널에서는 사용할 수 없습니다.")
        return
    await interaction.guild.get_channel(int(1106897209619914892)).send(content) # 공지를 출력할 특정 채널 아이디 입력
    await interaction.response.send_message("success")

위 코드를 통해 "/공지 [할말]"이라는 명령어를 사용하면 특정 채널에 입력한 내용이 출력되어 모든 유저들이 메시지를 볼 수 있게 됩니다.

3. 전체 코드 보기

앞서 설명한 내용을 바탕으로 완성된 전체 코드는 다음과 같습니다.


import discord
from discord.ext import commands
from discord import app_commands

bot = commands.Bot(command_prefix="/", intents=discord.Intents.all())
client = discord.Client(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)")
        print("봇이 준비되었습니다. 로그인 정보는 다음과 같습니다.")
        print(bot.user.name)
        print('------------------')
    except Exception as e:
        print(e)

@bot.tree.command(name="공지", description="공지")
@app_commands.describe(content="내용")
async def notification(interaction: discord.Interaction, content: str):
    if not interaction.channel.id == 1106897001804730378: #공지를 보낼 특정 채널 아이디 입력
        await interaction.response.send_message("이 채널에서는 사용할 수 없습니다.")
        return
    await interaction.guild.get_channel(int(1106897209619914892)).send(f"@everyone\n```{content}```") # 공지를 출력할 특정 채널 아이디 입력
    await interaction.response.send_message("성공적으로 공지하였습니다.")

bot.run('토큰')

위 코드는 봇이 준비되었을 때 봇의 로그인 정보를 출력하고, "/공지 [할말]"이라는 명령어를 수행하면 특정 채널에 입력한 내용을 출력하는 디스코드 봇입니다. 마지막 줄에는 봇을 실행하기 위한 토큰을 입력해야 합니다.

이제 이 코드를 실행하면 디스코드에 로그인하고 공지 명령어를 사용할 준비가 된 것입니다. 직접 실행해보고 다양한 명령어를 추가해 보세요!

반응형