[Python] 디스코드 청소 봇 만들기 | discord clean bot

파트라슈·2025년 1월 18일

1. 디스코드 서버 생성

이미 서버가 있는 상태면 패스
---> 해당링크 들어가서 서버 생성 참고하기 서버생성링크

2. 기본세팅

생각보다 질문이 많이 들어와서 같이 정리합니다.
제가 한 방법으로 알려드리니 편한 방법으로 알아서 커스텀 하시면 됩니다.

  1. 바탕화면에 폴더 생성 (ex. clean-bot)
  2. vscode 들어가서 생성 한 폴더 열기 ctrl + k + o 하면 열릴겁니다.
  3. python 최신버전으로 설치 홈페이지 들어가서 설치 하셔야합니다.
    (설치 됐는지 확인하는법 터미널에서 python -v 버전 3.13.1 찍히면 성공)
  4. 좌측에 파일+ 모양 있습니다 그걸로 파일 생성
  5. 코드 실행은 우측 상단에 재생버튼 있을겁니다 초록색으로 혹은 F5

3. 코드

pip install discord.py
터미널에 입력 후 엔터로 실행

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.messages = True
intents.message_content = True

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

@bot.event
async def on_ready():
    print(f"Logged in as {bot.user}")

@bot.command()
async def 청소(ctx, number: int = None):
    if number is None:
        await ctx.send("지울 메시지 수를 입력해주세요. 예: !청소 3")
        return

    if not ctx.author.guild_permissions.manage_messages:
        await ctx.send("메시지를 관리할 권한이 없습니다!")
        return

    def is_not_command(msg):
        return msg.id != ctx.message.id

    deleted = await ctx.channel.purge(limit=number + 1, check=is_not_command)
    await ctx.send(f"{len(deleted)}개의 메시지가 삭제되었습니다.", delete_after=5)

bot.run("토큰")
profile
내 머리속에서 나오는 코드는 없다

0개의 댓글