Advertisement
Guest User

Untitled

a guest
Aug 8th, 2023
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | Source Code | 0 0
  1. import discord
  2. import os
  3. import openai
  4.  
  5. intents = discord.Intents.default()
  6. intents.message_content = True
  7.  
  8. # Discord 봇 토큰
  9. TOKEN = 'YOUR_DISCORD_BOT_TOKEN'
  10.  
  11. # OpenAI API 키
  12. OPENAI_API_KEY = 'YOUR_OPENAI_API_KEY'
  13.  
  14. # Discord 봇 클라이언트 생성
  15. client = discord.Client(intents=intents)
  16.  
  17. # 봇이 준비되었을 때 실행되는 이벤트 핸들러
  18. @client.event
  19. async def on_ready():
  20.     print(f'Logged in as {client.user.name} - {client.user.id}')
  21.  
  22. # 메시지가 도착했을 때 실행되는 이벤트 핸들러
  23. @client.event
  24. async def on_message(message):
  25.     # 봇의 메시지는 무시
  26.     if message.author.bot:
  27.         return
  28.    
  29.     # 사용자 메시지를 GPT-3 API를 사용해 반박하는 함수 호출
  30.     response = generate_response(message.content)
  31.    
  32.     # 반박한 내용을 디스코드 채널에 전송
  33.     await message.channel.send(response)
  34.  
  35. # GPT-3 API를 사용해 반박하는 함수
  36. def generate_response(user_message):
  37.     openai.api_key = OPENAI_API_KEY
  38.     prompt = f"User said: {user_message}\nBot:"
  39.  
  40.     # GPT-3에 요청 보내기
  41.     response = openai.Completion.create(
  42.         engine="text-davinci-003",  # GPT-3 엔진 선택
  43.         prompt=prompt,
  44.         max_tokens=50  # 응답으로 생성되는 최대 토큰 수
  45.     )
  46.  
  47.     return response.choices[0].text.strip()
  48.  
  49. # Discord 봇 실행
  50. client.run(TOKEN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement