본문 바로가기

etc.

[Discord] 봇 실행 테스트

봇 실행 테스트 코드

외장 함수인 discord, asyncio를 import한다.

 

@client.event

: 봇이 실행되는 동안 발생하는 이벤트

 

@client.run

: 토큰 작성하여 봇 실행

 

on_ready()

: 봇이 오프라인에서 온라인으로 바뀌었을 때 1회 시작됨

 

client.change_presence()

: 상태 메시지 변경

 

on_message()

: 봇이 작동하고 있는 채널에서 메시지가 감지, message.content와 동일한 경우에 봇 동작

 

message.channel.send()

: 채널에 메시지 전송

 

message.author.send()

: 메시지 작성자에게 dm 전송

 

format() 

: {} 내용 설정

 

message.author

: ukkiyeon#0691

 

message.author.mention

: @ukkiyeon 멘션


에러 해결 1

Import "discord" could not be resolved Pylance

ModuleNotFoundError : No module named 'discord'

=> 패키지가 설치되는 경로가 달라서 발생하는 에러로, 인터프리터를 변경하면 해결된다.

 

F1 > Select Interpreter 검색 > Conda로 인터프리터를 변경했더니 해결되었다.

(케바케같지만 ..)

에러 해결 2

다시 실행했더니 다른 에러 발생

TypeError: __init__() missing 1 required keyword-only arguments: 'intents' 

=> 특정 버전부터는 봇에 대한 intents(의도)를 명시해야 한다고 한다.

 

위와 같이 intents 관련 코드를 추가해주었다.

 

https://devwithpug.github.io/python/sabot-2/#2-intents%EC%9D%98-%ED%95%84%EC%9A%94%EC%84%B1

 

2.파이썬을 이용한 디스코드 봇 만들기: discord.py API

discord.py API에 대해

devwithpug.github.io

에러 해결 3

RuntimeError: Event loop is closed

=> python 3.8 이상부터 windows에서 asyncio를 사용할 경우 발생하는 에러라고 한다. 

 

위와 같은 코드를 추가해주었다.

 

https://velog.io/@hyesukim1/%EC%98%A4%EB%A5%98RuntimeError-Event-loop-is-closed

 

[오류]RuntimeError: Event loop is closed

asyncio를 사용한 비동기 프로그래밍 모듈을 사용할 때 자꾸 오류가 나서 찾아보고 해결했다.python 3.8이상부터 운영체제 windows에서 asyncio를 사용할 경우 정상적으로 작동 되었음에도 위와같은 오

velog.io

에러 해결 4

discord.errors.PrivilegedIntentsRequired: Shard ID None is requesting privileged intents that have not been explicitly enabled in the developer portal. It is recommended to go to https://discord.com/developers/applications/ and explicitly enable the privileged intents within your application's page. If this is not possible, then consider disabling the privileged intents instead.

=> 개발자 포털에 가서, Privileged Gateway Intents 관련 설정을 모두 허용해서 해결했다.


봇 실행

이후 정상적으로 실행되었다.

 

봇이 온라인으로 활성화되었고, 상태메시지도 변경되었다.

 

'테스트'라고 메시지를 보내면, 봇이 감지하고 메시지를 보내준다.

마찬가지로 디엠을 보내도 정상적으로 실행된다.

 

다만, 채널에 메시지를 보냈을 때 봇은 채널과 디엠 둘 다 메시지를 보내줘야 하는데, 채널에는 위처럼 메시지를 보내지만 디엠은 보내지 않았다.

 

오타 발견

수정했더니 디엠도 잘 전송되었다.

 

참고

https://www.youtube.com/watch?v=iVxXPmJObfY