Python - 在不和谐时停止工作
Python - stops job on discord
此机器人的计划是唤醒 --> 检索我需要的数据 --> 自行终止。
我试过使用client.logout()
和client.close()
,但是程序运行后不会停止。有什么建议吗?
import discord
import os
from discord.ext import tasks
from discord.ext import commands
discord_client = discord.Client()
@discord_client.event
async def on_ready():
for guild in discord_client.guilds:
if guild.name == GUILD:
break
print(
f'{discord_client.user} is connected to the following guild:\n'
f'{guild.name}(id: {guild.id})'
)
# Send message when Bot wakes up
channel = discord_client.get_channel(ID)
await channel.send("*wakes up and starts working*")
retrieve_data.start()
await discord_client.logout()
@tasks.loop(count=1)
async def retrieve_data():
# do things here
discord_client.run(TOKEN)
您没有定义您的令牌,导入 load_dotenv 并调用它,您有未定义的变量,而且 logout() 已被弃用,因此请改用 close() 并且它有效。
import discord
import os
from discord.ext import tasks
from dotenv import load_dotenv
from discord.ext import commands
load_dotenv()
discord_client = discord.Client()
@discord_client.event
async def on_ready():
# for guild in discord_client.guilds:
# if guild.name == GUILD:
# break
# print(
# f'{discord_client.user} is connected to the following guild:\n'
# f'{guild.name}(id: {guild.id})'
# )
# Send message when Bot wakes up
# channel = discord_client.get_channel(ID)
# await channel.send("*wakes up and starts working*")
await retrieve_data.start()
await discord_client.close()
@tasks.loop(count=1)
async def retrieve_data():
print("test")
discord_client.run(os.getenv("TOKEN"))
此机器人的计划是唤醒 --> 检索我需要的数据 --> 自行终止。
我试过使用client.logout()
和client.close()
,但是程序运行后不会停止。有什么建议吗?
import discord
import os
from discord.ext import tasks
from discord.ext import commands
discord_client = discord.Client()
@discord_client.event
async def on_ready():
for guild in discord_client.guilds:
if guild.name == GUILD:
break
print(
f'{discord_client.user} is connected to the following guild:\n'
f'{guild.name}(id: {guild.id})'
)
# Send message when Bot wakes up
channel = discord_client.get_channel(ID)
await channel.send("*wakes up and starts working*")
retrieve_data.start()
await discord_client.logout()
@tasks.loop(count=1)
async def retrieve_data():
# do things here
discord_client.run(TOKEN)
您没有定义您的令牌,导入 load_dotenv 并调用它,您有未定义的变量,而且 logout() 已被弃用,因此请改用 close() 并且它有效。
import discord
import os
from discord.ext import tasks
from dotenv import load_dotenv
from discord.ext import commands
load_dotenv()
discord_client = discord.Client()
@discord_client.event
async def on_ready():
# for guild in discord_client.guilds:
# if guild.name == GUILD:
# break
# print(
# f'{discord_client.user} is connected to the following guild:\n'
# f'{guild.name}(id: {guild.id})'
# )
# Send message when Bot wakes up
# channel = discord_client.get_channel(ID)
# await channel.send("*wakes up and starts working*")
await retrieve_data.start()
await discord_client.close()
@tasks.loop(count=1)
async def retrieve_data():
print("test")
discord_client.run(os.getenv("TOKEN"))