对于我的不和谐机器人,我试图让机器人在说出“引用”(来自特定频道的消息)时离线,我可以运行命令来运行所有引用并重新添加
它的问题是我收到一个错误,指出第 125 行没有等待协程。我很困惑如何解决这个问题,所以我希望这里有人知道该怎么做。< /p>
这是机器人的命令代码。
@bot.command()
@commands.is_owner()
async def refresh_quotes(ctx):
with open("quotes.json", "r") as f:
quotes = json.load(f)
count = 1
async with ctx.channel.typing():
channel = bot.get_channel(819715867424194581)
messages = await channel.history(limit=None, oldest_first=True).flatten()
for i in messages:
quotes.pop(str(count))
with open("quotes.json", 'w') as f:
json.dump(quotes, f, indent=4)
quotes[str(count)] = str(i.content)
with open("quotes.json", 'w') as f:
json.dump(quotes, f, indent=4)
count += 1
await ctx.send("Successfully refreshed quotes.json.") # Line 125
答案 0 :(得分:0)
通过反复试验修复了它。这是成品,以防有人需要它:
@bot.command()
@commands.is_owner()
async def refresh_quotes(ctx, guild=None):
if guild is None:
guild = ctx.guild
with open(f"quotes/{guild.id}/quotes.json", "r") as f:
quotes = json.load(f)
count = 1
async with ctx.channel.typing():
channel = bot.get_channel(819715867424194581)
messages = await channel.history(limit=None, oldest_first=True).flatten()
try:
for i in messages:
with open(f"quotes/{guild.id}/quotes.json", 'w') as f:
json.dump(quotes, f, indent=4)
quotes[str(count)] = str(i.content)
count += 1
if count % 10 == 0:
await asyncio.sleep(1)
except FileNotFoundError:
await ctx.send(f"Quotes aren't set up in this server! To set it up, use
``{get_prefix(ctx.message)}setup "
f"quotes`` in the text channel you want to put quotes.")
finally:
with open(f"quotes/{guild.id}/quotes.json", 'w') as f:
json.dump(quotes, f, indent=4)
await ctx.send(f"Successfully refreshed quotes for server.")