refactor: Convert match serialization methods to async
This commit is contained in:
parent
f18f03a7f7
commit
47f4df7803
@ -43,15 +43,15 @@ async def send_message(channel):
|
|||||||
except:
|
except:
|
||||||
return
|
return
|
||||||
for match_ in matches:
|
for match_ in matches:
|
||||||
data = dota.serialize_match_for_discord(match_)
|
data = await dota.serialize_match_for_discord(match_)
|
||||||
logger.info(f"sending match {match_['match_id']}, {data}")
|
logger.info(f"sending match {match_['match_id']}, {data}")
|
||||||
try:
|
try:
|
||||||
await channel.send(content=data['content'], embeds=[discord.Embed.from_dict(embed) for embed in data['embeds']])
|
await channel.send(content=data['content'], embeds=[discord.Embed.from_dict(embed) for embed in data['embeds']])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"send match error {e}")
|
logger.error(f"send match error {e}")
|
||||||
|
|
||||||
@bot.command(description="获取最近战绩", name='recent_matches') # this decorator makes a slash command
|
@bot.command(description="获取最近战绩", name='recent_matches')
|
||||||
async def get_friends_recent_matches(ctx, name, match_count=5): # a slash command will be created with the name "ping"
|
async def get_friends_recent_matches(ctx, name, match_count=5):
|
||||||
await ctx.defer()
|
await ctx.defer()
|
||||||
logger.info(f"get_friends_recent_matches {name} {match_count}")
|
logger.info(f"get_friends_recent_matches {name} {match_count}")
|
||||||
friends = dota.Friend.filter(name=name)
|
friends = dota.Friend.filter(name=name)
|
||||||
@ -59,7 +59,7 @@ async def get_friends_recent_matches(ctx, name, match_count=5): # a slash comman
|
|||||||
if friends.count() == 0:
|
if friends.count() == 0:
|
||||||
await ctx.respond(content=f'找不到 {name} 的信息')
|
await ctx.respond(content=f'找不到 {name} 的信息')
|
||||||
return
|
return
|
||||||
data = dota.Friend.serialize_recent_matches_for_discord(friends, match_count)
|
data = await dota.Friend.serialize_recent_matches_for_discord(friends, match_count)
|
||||||
if not data:
|
if not data:
|
||||||
await ctx.respond(content=f'找不到 {name} 的战绩')
|
await ctx.respond(content=f'找不到 {name} 的战绩')
|
||||||
return
|
return
|
||||||
|
|||||||
15
dota.py
15
dota.py
@ -119,7 +119,7 @@ class Friend(BaseModel):
|
|||||||
return data
|
return data
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def serialize_recent_matches_for_discord(cls, friends, limit=5):
|
async def serialize_recent_matches_for_discord(cls, friends, limit=5):
|
||||||
# {
|
# {
|
||||||
# "content": "## 水哥的战报\n",
|
# "content": "## 水哥的战报\n",
|
||||||
# "embeds": [
|
# "embeds": [
|
||||||
@ -150,6 +150,7 @@ class Friend(BaseModel):
|
|||||||
'content': f'## {name}的战报',
|
'content': f'## {name}的战报',
|
||||||
'embeds': [],
|
'embeds': [],
|
||||||
}
|
}
|
||||||
|
|
||||||
for match_ in matches[:limit]:
|
for match_ in matches[:limit]:
|
||||||
duration = '%d:%02d:%02d' % utils.convert_seconds_to_hms(match_['duration'])
|
duration = '%d:%02d:%02d' % utils.convert_seconds_to_hms(match_['duration'])
|
||||||
summary = f"{duration}"
|
summary = f"{duration}"
|
||||||
@ -181,8 +182,9 @@ class Friend(BaseModel):
|
|||||||
# 生成图片报告
|
# 生成图片报告
|
||||||
image_url = None
|
image_url = None
|
||||||
try:
|
try:
|
||||||
# 使用asyncio运行异步函数
|
# 直接等待异步函数,而不是使用asyncio.run()
|
||||||
image_url = asyncio.run(image_generator.generate_recent_matches_image(name, matches[:limit]))
|
image_generator = ImageGenerator()
|
||||||
|
image_url = await image_generator.generate_recent_matches_image(name, matches[:limit])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"生成最近比赛报告图片失败: {str(e)}")
|
logger.error(f"生成最近比赛报告图片失败: {str(e)}")
|
||||||
|
|
||||||
@ -215,7 +217,7 @@ def get_friends_recent_matches():
|
|||||||
return matches
|
return matches
|
||||||
|
|
||||||
|
|
||||||
def serialize_match_for_discord(match_):
|
async def serialize_match_for_discord(match_):
|
||||||
# {
|
# {
|
||||||
# "content": "## 天辉\n\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n\n## 夜魇\n\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n",
|
# "content": "## 天辉\n\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n\n## 夜魇\n\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n",
|
||||||
# "tts": false,
|
# "tts": false,
|
||||||
@ -323,8 +325,9 @@ def serialize_match_for_discord(match_):
|
|||||||
# 生成比赛报告图片
|
# 生成比赛报告图片
|
||||||
image_url = None
|
image_url = None
|
||||||
try:
|
try:
|
||||||
# 使用asyncio运行异步函数
|
# 直接等待异步函数,而不是使用asyncio.run()
|
||||||
image_url = asyncio.run(image_generator.generate_match_report(match_))
|
image_generator = ImageGenerator()
|
||||||
|
image_url = await image_generator.generate_match_report(match_)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"生成比赛报告图片失败: {str(e)}")
|
logger.error(f"生成比赛报告图片失败: {str(e)}")
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user