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 5fe1adc..874cd8c 100644 Binary files a/dota.db and b/dota.db differ diff --git a/dota.py b/dota.py index 559e89f..721b843 100644 --- a/dota.py +++ b/dota.py @@ -89,20 +89,29 @@ def get_friends_recent_matches(): def serialize_match_for_discord(match_): # { -# "content": "3黑(我, 受风,小金 )00:34:23 赢", +# "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, # "embeds": [ # { # "id": 652627557, -# "title": "天辉 - 32", -# "description": "我(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 伤害", # "color": 6732650, # "fields": [ # { # "id": 878517961, -# "name": "夜魇 - 23", -# "value": "我(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 伤害", -# "inline": false +# "name": "天辉", +# "value": "23", +# "inline": true +# }, +# { +# "id": 159867313, +# "name": "夜魇 ", +# "value": "23", +# "inline": true +# }, +# { +# "id": 202767768, +# "name": "3黑(我, 受风,小金 )00:34:23 赢", +# "value": "" # } # ], # "author": { @@ -123,7 +132,9 @@ def serialize_match_for_discord(match_): break win = is_radiant == match_['radiant_win'] if match_['party_size'] > 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