From df6d64f43218977d5b25e80a0caa099d8f48aa6f Mon Sep 17 00:00:00 2001 From: Ching Date: Sat, 16 Sep 2023 13:03:31 +0800 Subject: [PATCH] =?UTF-8?q?feat(bot.py,=20utils.py):=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=BA=86=E6=B6=88=E6=81=AF=E7=9A=84=E6=A0=BC=E5=BC=8F=EF=BC=8C?= =?UTF-8?q?=E6=9B=B4=E6=94=B9=E5=8F=91=E9=80=81=E4=BB=BB=E5=8A=A1=E5=BE=AA?= =?UTF-8?q?=E7=8E=AF=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改了消息的格式,更改发送任务循环时间 Signed-off-by: Ching --- discord_bot.py | 9 +++++++-- dota.db | Bin 28672 -> 28672 bytes dota.py | 51 +++++++++++++++++++++++++++++++++++++------------ utils.py | 13 +++++++++++++ 4 files changed, 59 insertions(+), 14 deletions(-) diff --git a/discord_bot.py b/discord_bot.py index 27dac04..ff0240f 100644 --- a/discord_bot.py +++ b/discord_bot.py @@ -1,6 +1,7 @@ import discord from discord.ext import tasks import dota +import utils bot = discord.Bot(proxy='http://127.0.0.1:1235') @@ -10,12 +11,16 @@ async def on_ready(): channel_id = 1152167937852055552 -@tasks.loop(seconds=10) +@tasks.loop(minutes=1) async def send_message(channel): + if utils.is_game_time(): + send_message.change_interval(minutes=1) + else: + send_message.change_interval(minutes=15) + matches = dota.get_friends_recent_matches() for match_ in matches: data = dota.serialize_match_for_discord(match_) - print(data) await channel.send(content=data['content'], embeds=[discord.Embed.from_dict(embed) for embed in data['embeds']]) @bot.event diff --git a/dota.db b/dota.db index 5fe1adc6cb432028f621d94fcb5a418f025367df..874cd8ca3d32dd86ad7e60458ff25bbdaf301a74 100644 GIT binary patch delta 229 zcmZp8z}WDBae_4C`iU~mjO#ZhEb$j&=U>dgzn}js|3?0={EPWt^55DlDA3EF&3WVF zvn*x?$!8n3Je|Ch^Js5>ERgrKfBMVCeVm6H9u)(5FZ#P)bZz7~+`Yb@nL*quKPk0{ zv-!=YAKD<58~R_iPh@MH`>8XGnL#)=FPH5~OOGUw`K)6iF9QPuGyg0G{yqG&`0oKN z?BREjnZ2rYy$BnFF%t))k%5u1u7Rblp{atAk(Hsjm5~`+CacWs6_?Khf>dxKR2YL) R7#Lfbm~w`)PQI6~1OP5*Pmllr delta 65 zcmZp8z}WDBae_4C!ih4@j0-m=Eb(V! 1: - content = f"{match_['party_size']}黑({','.join(party)}){match_['duration']} {'赢' if win else '输'}" + summary = f"{match_['party_size']}黑 {match_['duration']}" + else: + summary = f"单排 {match_['duration']}" radiant = [] dire = [] @@ -136,20 +147,36 @@ def serialize_match_for_discord(match_): color = 6732650 if win else 16724787 # 66bb6a or FF3333 + content = '## 天辉\n\n' + '\n'.join(radiant) + '\n\n## 夜魇\n\n' + '\n'.join(dire) + '\n' + radiant_indicator = '' + dire_indicator = '' + if is_radiant: + radiant_indicator = ' 🌟' + else: + dire_indicator = ' 🌟' + data = { "content": content, + "tts": False, "embeds": [ { - "description": '\n'.join(radiant), "color": color, "fields": [ { - "name": "夜魇 - %s" % match_['dire_score'], - "value": '\n'.join(dire), - "inline": False + "name": "天辉" + radiant_indicator, + "value": match_['radiant_score'], + "inline": True + }, + { + "name": "夜魇" + dire_indicator, + "value": match_['dire_score'], + "inline": True + }, + { + "name": summary, + "value": f"{','.join(party)}" } ], - "title": "天辉 - %s" % match_['radiant_score'], "author": { "name": "opendota", "url": "https://www.opendota.com/matches/%s" % match_['match_id'] diff --git a/utils.py b/utils.py index 420c4ab..38d6117 100644 --- a/utils.py +++ b/utils.py @@ -1,4 +1,17 @@ +import datetime +import requests + def convert_seconds_to_hms(total_seconds): hours, remainder = divmod(total_seconds, 3600) minutes, seconds = divmod(remainder, 60) return hours, minutes, seconds + +def is_workday(): + return datetime.datetime.today().weekday() < 5 + +def is_game_time(): + # game time is workday 21:00 - 1:00, weekend 10:00 - 1:00 + if is_workday(): + return datetime.datetime.now().hour >= 21 or datetime.datetime.now().hour < 1 + else: + return datetime.datetime.now().hour >= 10 or datetime.datetime.now().hour < 1