Recently, Misty has begun to get sick that she doesn't want to go to school. In Previous article, I was printing () the current time and scheduled transmission time for debugging, but I came up with the idea of "print on syslog (print () ) Send everything to Discord. ”I changed it, but an error occurred and I was struggling for about two days. I would like to introduce the coping method at that time.
Code when throwing an error
bot.py
@tasks.loop(seconds=60)
async def loop():
global time_set
global tem_set
w_list = ["Moon", "fire", "water", "wood", "Money", "soil", "Day"] #It has evolved!
now_t, now_dt = datetime.now().strftime('%H:%M'), w_list[datetime.now().weekday()]
ch = client.get_channel(771155766056452167)
await ch.send(f"Current time:`{now_t}`/ Today's day of the week:`{now_dt}`/ Scheduled transmission time:`{time_set}`/ Scheduled body temperature to be transmitted:`{tem_set}`")
if now_dt != "Day": # Day曜Day以外だったら送信するようにした
if now_t == time_set: #Is it the scheduled transmission time?
dt_now = datetime.now().strftime("%Y-%m-%d") #Current time 2020-01-Obtained in the form of 01, dt_Store in now
file_name = "cfg.json"
with open(file_name, "r", encoding="utf-8")as f:
cfg = json.load(f)
cfg["output"]["ans_1"] = f"{dt_now}"
cfg["output"]["ans_4"] = f"{tem_set}"
params = {"entry.{}".format(cfg["entry"][k]): cfg["output"][k] for k in cfg["entry"].keys()}
res = requests.get(cfg["form_url"] + "formResponse", params=params)
if res.status_code == 200:
await template_embed(message=768274673984208926, title="Log information", description=f"[URL]({res.url})",
name_1="Completion status", name_2="Sent body temperature", value_1="Succeeded", color=discord.Color.green())
else:
res.raise_for_status()
await template_embed(message=768274673984208926, title="Log information", name_1="Completion status", name_2="Body temperature to be sent",
value_1="An error has occurred.", color=discord.Color.red())
else:
if now_t == "21:00":
time_set = setting_time_set()
tem_set = set_tem()
await template_embed(message=768274673984208926, title="Notification of transmission time update", name_1="Next scheduled transmission time", name_2="Body temperature to be sent",
value_1=time_set, color=discord.Color.blue())
When I actually run this code, I get the following error: At first, I thought it was a copy / paste of the ID, but even if I copied it again, I got an error, so maybe there is something else? When I looked it up, [it was](https://qiita.com/coolwind0202/items/a4405be45734bd7f6cd5#attributeerror-%E5%9E%8B%E5%90%8D-object-has-no-attribute-% E5% B1% 9E% E6% 80% A7% E5% 90% 8D)!
Functions such as client.get_channel () return None until the BOT is ready. For example, did you suddenly get_channel () at the beginning of the program? To avoid this, you need to get it "after" the ready event occurs.
"Well, by the way ..." (crying) Where did my two days go?
error
AttributeError: 'NoType' object has no attribute 'send'
The workaround was surprisingly easy, yes.
Before doing get_channel ()
in the previous article, wait_until_ready ()
[(reference)](https://discordpy.readthedocs.io/ja/latest/api.html?highlight=wait_until_ready#discord.Client All you have to do is put in .wait_until_ready).
bot.py
@tasks.loop(seconds=60)
async def loop():
global time_set
global tem_set
w_list = ["Moon", "fire", "water", "wood", "Money", "soil", "Day"] #It has evolved!
now_t, now_dt = datetime.now().strftime('%H:%M'), w_list[datetime.now().weekday()]
await client.wait_until_ready()
ch = client.get_channel(771155766056452167)
await ch.send(f"Current time:`{now_t}`/ Today's day of the week:`{now_dt}`/ Scheduled transmission time:`{time_set}`/ Scheduled body temperature to be transmitted:`{tem_set}`")
if now_dt != "Day": # Day曜Day以外だったら送信するようにした
if now_t == time_set: #Is it the scheduled transmission time?
dt_now = datetime.now().strftime("%Y-%m-%d") #Current time 2020-01-Obtained in the form of 01, dt_Store in now
file_name = "cfg.json"
with open(file_name, "r", encoding="utf-8")as f:
cfg = json.load(f)
cfg["output"]["ans_1"] = f"{dt_now}"
cfg["output"]["ans_4"] = f"{tem_set}"
params = {"entry.{}".format(cfg["entry"][k]): cfg["output"][k] for k in cfg["entry"].keys()}
res = requests.get(cfg["form_url"] + "formResponse", params=params)
if res.status_code == 200:
await template_embed(message=768274673984208926, title="Log information", description=f"[URL]({res.url})",
name_1="Completion status", name_2="Sent body temperature", value_1="Succeeded", color=discord.Color.green())
else:
res.raise_for_status()
await template_embed(message=768274673984208926, title="Log information", name_1="Completion status", name_2="Body temperature to be sent",
value_1="An error has occurred.", color=discord.Color.red())
else:
if now_t == "21:00":
time_set = setting_time_set()
tem_set = set_tem()
await template_embed(message=768274673984208926, title="Notification of transmission time update", name_1="Next scheduled transmission time", name_2="Body temperature to be sent",
value_1=time_set, color=discord.Color.blue())
Oh, it's solved in one shot ()
--If you get an error when using a function such as get_channel ()
in tasks.loop
, you can fix it by insertingwait_until_ready ()
.
I'm sorry for the very rough summary ... I hope it helps someone who is in a similar situation.
-Check if Discord.py bot doesn't work
Recommended Posts