디스코드 봇 개발 중 오류가 났습니다
조회수 388회
코드를 실행하여 !학습모드를 입력하면 해당 오류를 발생시킵니다.
ERROR discord.ext.commands.bot Ignoring exception in command None discord.ext.commands.errors.CommandNotFound: Command "학습모드" is not found
헤당 코드 전문입니다.
import discord
from discord.ext import commands
from fuzzywuzzy import process
# 봇 토큰 입력
token = 'X'
intents = discord.Intents.all()
# 봇 클라이언트 생성
bot = commands.Bot(command_prefix='!', intents=intents)
# 변수 초기화
learning_mode = False
learned_responses = {}
# Cog 정의
class LearningCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
# !학습모드 명령어 처리
@commands.command(name='학습모드')
async def learning_mode_toggle(self, ctx):
global learning_mode
learning_mode = not learning_mode
if learning_mode:
await ctx.send('학습 모드가 활성화되었습니다. 다음에 오는 모든 문장을 학습합니다.')
else:
await ctx.send('학습 모드가 비활성화되었습니다.')
# 학습 모드가 종료될 때 학습된 응답을 파일에 저장
with open('학습된_응답.txt', 'w') as file:
for keyword, response in learned_responses.items():
file.write(f'{keyword}: {response}\n')
print("학습모드:", learning_mode) # 디버그 출력 추가
# 메시지 이벤트 처리
@commands.Cog.listener()
async def on_message(self, message):
if learning_mode:
await self.handle_learning_mode_message(message)
else:
await self.handle_normal_mode_message(message)
# 메시지 수정 또는 삭제 이벤트 처리
@commands.Cog.listener()
async def on_message_edit(self, before, after):
await self.handle_message(after)
@commands.Cog.listener()
async def on_message_delete(self, message):
pass
async def handle_message(self, message):
if learning_mode:
await self.handle_learning_mode_message(message)
else:
await self.handle_normal_mode_message(message)
async def handle_learning_mode_message(self, message):
# 사용자의 메시지를 키워드와 응답으로 분리
parts = message.content.split(':')
if len(parts) == 2:
keyword, response = parts[0].strip(), parts[1].strip()
learned_responses[keyword] = response
await message.channel.send(f'키워드 "{keyword}"에 대한 응답을 학습했습니다.')
# 학습 메시지를 처리한 후, 채팅 메시지를 삭제
await message.delete()
else:
await message.channel.send('올바른 형식으로 입력해주세요. (예: 키워드: 응답)')
async def handle_normal_mode_message(self, message):
# 일반 모드에서 저장된 응답 중 사용자의 채팅에 포함된 키워드에 대한 응답 반환
response = self.get_keyword_response(message.content)
await message.channel.send(response)
# 저장된 응답 중 사용자의 채팅에 포함된 키워드에 대한 응답 반환 함수
def get_keyword_response(self, user_message):
if learned_responses:
keyword_choices = list(learned_responses.keys())
most_similar_keyword, similarity = process.extractOne(user_message, keyword_choices)
if similarity > 70: # 임의의 유사성 기준, 필요에 따라 조절 가능
return learned_responses[most_similar_keyword]
return '잘 모르겠어요'
# Cog를 봇에 추가
bot.add_cog(LearningCog(bot))
# 종료 명령어 처리
@bot.command(name='종료')
async def shutdown(ctx):
await ctx.send('봇이 종료됩니다.')
await bot.close()
# 봇 종료
try:
bot.run(token)
except KeyboardInterrupt:
print("봇이 수동으로 종료되었습니다.")
bot.loop.run_until_complete(bot.logout())
finally:
bot.loop.close()
댓글 입력