내 봇이 10 분마다 이미지를 채널에 자동으로 보내도록 만들고 싶습니다. 내 코드는 다음과 같습니다.
def job():
channel = client.get_channel(803842308139253760)
channel.send(file=discord.File(random.choice('image1', 'image2', 'image3))
schedule.every(10).minutes.do(job)
while True:
schedule.run_pending()
time.sleep(1)
일정이 작동한다는 것을 알고 있습니다. 그러나 어떤 이유로 메시지를 보낼 수 없습니다. 이 오류가 발생합니다 : AttributeError: 'NoneType' object has no attribute 'send'
. 저는 프로그래밍이 처음이므로 어떤 통찰력이라도 크게 감사하겠습니다!
채널 변수가 내부 캐시에 없기 때문에 None
(a NoneType
에는 속성 / 메서드가 없음) 전체 스레드를 차단하므로로드되지 않기 때문에 오류가 발생합니다. 나는 당신의 코드를 고칠 수 있다고 생각하지만 백그라운드 작업에 대한 정말 나쁜 솔루션입니다. 다행히도 discord.py
이러한 작업을 수행하기위한 기본 제공 확장 기능이 제공됩니다.
from discord.ext import tasks
@tasks.loop(minutes=10) # You can either pass seconds, minutes or hours
async def send_image(channel: discord.TextChannel):
image = discord.File("path here")
await channel.send(file=image)
# Starting the task (you can also do it on the `on_ready` event so it starts when the bot is running)
@client.command()
async def start(ctx):
channel = client.get_channel(ID_HERE)
send_image.start(channel)
# Using the `on_ready` event
@client.event
async def on_ready():
await client.wait_until_ready() # Waiting till the internal cache is done loading
channel = client.get_channel(ID_HERE)
send_image.start(channel)
@client.command()
async def stop(ctx):
send_image.stop()
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다