파이썬 수업/파이썬 미니프로젝트

[파이썬] 디스코드 챗봇 만들기- 추방

climacus 2023. 4. 22. 10:35

파이썬으로 디스코드 서버에 챗봇 붙이는 프로젝트 진행

1. 디스코드 서버 열기

2. 파이썬에 디스코드 설치

pip install discord

3. import 디스코드

4. 디스코드 개발자 채널에 가서, bot 키 생성

https://discord.com/developers/applications

생성하는 방법은 여기에->https://scvtwo.tistory.com/196

5. 파이썬에서 아래와 같이 명령문 코딩

import discord
from discord.ext import commands

DISCORD_BOT_KEY = '디스코드 봇 키 입력'

# Intents 객체 생성
intents = discord.Intents.default()
intents.members = True

client = discord.Client(intents=intents)

bot = commands.Bot(command_prefix=';',intents=discord.Intents.all())

@bot.event
async def on_ready():
    print("봇이 시작되었습니다.")

@bot.command()
async def 안녕(ctx):
	await ctx.send('안녕하세요!')
        
@bot.command()
async def 바보(ctx):
    await ctx.send("{}라고 하셨군요, 반갑습니다 바보똥꼬 {}님!".format(
        ctx.message.content, ctx.author.name))

@bot.command()
async def 추방(ctx):   
    if ctx.message.content.startswith(";추방"):
        member = ctx.message.guild.get_member(int(ctx.message.content.split(" ")[1]))
        await ctx.message.guild.kick(member, reason=None)

bot.run('디스코드 봇 키 입력')

 

반응형