refactor: Convert match serialization methods to async
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
b6d2b7f863
commit
079f7f42e1
@ -43,15 +43,15 @@ async def send_message(channel):
|
||||
except:
|
||||
return
|
||||
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}")
|
||||
try:
|
||||
await channel.send(content=data['content'], embeds=[discord.Embed.from_dict(embed) for embed in data['embeds']])
|
||||
except Exception as e:
|
||||
logger.error(f"send match error {e}")
|
||||
|
||||
@bot.command(description="获取最近战绩", name='recent_matches') # this decorator makes a slash command
|
||||
async def get_friends_recent_matches(ctx, name, match_count=5): # a slash command will be created with the name "ping"
|
||||
@bot.command(description="获取最近战绩", name='recent_matches')
|
||||
async def get_friends_recent_matches(ctx, name, match_count=5):
|
||||
await ctx.defer()
|
||||
logger.info(f"get_friends_recent_matches {name} {match_count}")
|
||||
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:
|
||||
await ctx.respond(content=f'找不到 {name} 的信息')
|
||||
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:
|
||||
await ctx.respond(content=f'找不到 {name} 的战绩')
|
||||
return
|
||||
|
||||
19
dota.py
19
dota.py
@ -119,7 +119,7 @@ class Friend(BaseModel):
|
||||
return data
|
||||
|
||||
@classmethod
|
||||
def serialize_recent_matches_for_discord(cls, friends, limit=5):
|
||||
async def serialize_recent_matches_for_discord(cls, friends, limit=5):
|
||||
# {
|
||||
# "content": "## 水哥的战报\n",
|
||||
# "embeds": [
|
||||
@ -150,6 +150,7 @@ class Friend(BaseModel):
|
||||
'content': f'## {name}的战报',
|
||||
'embeds': [],
|
||||
}
|
||||
|
||||
for match_ in matches[:limit]:
|
||||
duration = '%d:%02d:%02d' % utils.convert_seconds_to_hms(match_['duration'])
|
||||
summary = f"{duration}"
|
||||
@ -177,12 +178,13 @@ class Friend(BaseModel):
|
||||
'timestamp': end_time,
|
||||
'url': f"https://www.opendota.com/matches/{match_['match_id']}",
|
||||
})
|
||||
|
||||
|
||||
# 生成图片报告
|
||||
image_url = None
|
||||
try:
|
||||
# 使用asyncio运行异步函数
|
||||
image_url = asyncio.run(image_generator.generate_recent_matches_image(name, matches[:limit]))
|
||||
# 直接等待异步函数,而不是使用asyncio.run()
|
||||
image_generator = ImageGenerator()
|
||||
image_url = await image_generator.generate_recent_matches_image(name, matches[:limit])
|
||||
except Exception as e:
|
||||
logger.error(f"生成最近比赛报告图片失败: {str(e)}")
|
||||
|
||||
@ -215,7 +217,7 @@ def get_friends_recent_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",
|
||||
# "tts": false,
|
||||
@ -323,11 +325,12 @@ def serialize_match_for_discord(match_):
|
||||
# 生成比赛报告图片
|
||||
image_url = None
|
||||
try:
|
||||
# 使用asyncio运行异步函数
|
||||
image_url = asyncio.run(image_generator.generate_match_report(match_))
|
||||
# 直接等待异步函数,而不是使用asyncio.run()
|
||||
image_generator = ImageGenerator()
|
||||
image_url = await image_generator.generate_match_report(match_)
|
||||
except Exception as e:
|
||||
logger.error(f"生成比赛报告图片失败: {str(e)}")
|
||||
|
||||
|
||||
data = {
|
||||
"content": content,
|
||||
"tts": False,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user