我有一個 discord.py Python 腳本,但它不發送訊息并出現此錯誤:
RuntimeWarning: coroutine 'Messageable.send' was never awaited
我的代碼:
@client.command()
async def shift(ctx, time=None, shifts=None):
if time is None:
f=open('times.json')
lines=f.readlines()
print(lines[1])
print(lines[2])
embed=discord.Embed(title="Shift Infos für diesen Tag")
embed.add_field(name="Zeit", value=f"{time}", inline=False)
embed.add_field(name="Shift", value=f"{shifts}", inline=False)
embed.add_field(name="Game", value="https://web.roblox.com/games/8063846199/VWS-Verkehrsbetriebe-Beta", inline=False)
await ctx.send(embed=embed)
else:
if shift is None:
ctx.send("Bitte gebe ein ob heute eine Shift ist. (Ja oder Nein)")
else:
ctx.send(f"Neue Shift Einstellung: Zeit: {time} {shifts}")
with open('times.json', 'a') as the_file:
the_file.write(f'{time}\n')
the_file.write(f'{shifts}')
uj5u.com熱心網友回復:
您的錯誤訊息已經告訴您解決方案。
send是異步的,所以你必須await這樣做,就像你發送嵌入時所做的那樣。
else:
if shift is None:
await ctx.send("Bitte gebe ein ob heute eine Shift ist. (Ja oder Nein)")
else:
await ctx.send(f"Neue Shift Einstellung: Zeit: {time} {shifts}")
https://discordpy.readthedocs.io/en/master/api.html?highlight=send#discord.abc.Messageable.send
還有一個不相關的說明:您打開了檔案,但從未在第一個 if 塊中關閉它。考慮使用背景關系管理器,就像您在最后一個 else 塊中所做的那樣。
uj5u.com熱心網友回復:
@client.command()
async def shift(ctx, time=None, shifts=None):
if time is None:
f=open('times.json')
lines=f.readlines()
print(lines[1])
print(lines[2])
embed=discord.Embed(title="Shift Infos für diesen Tag")
embed.add_field(name="Zeit", value=f"{time}", inline=False)
embed.add_field(name="Shift", value=f"{shifts}", inline=False)
embed.add_field(name="Game", value="https://web.roblox.com/games/8063846199/VWS-Verkehrsbetriebe-Beta", inline=False)
await ctx.send(embed=embed)
else:
if shift is None:
await ctx.send("Bitte gebe ein ob heute eine Shift ist. (Ja oder Nein)")
else:
await ctx.send(f"Neue Shift Einstellung: Zeit: {time} {shifts}")
with open('times.json', 'a') as the_file:
the_file.write(f'{time}\n')
the_file.write(f'{shifts}')
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/385019.html
