feat(bot.py, utils.py): 修改了消息的格式,更改发送任务循环时间

修改了消息的格式,更改发送任务循环时间

Signed-off-by: Ching <loooching@gmail.com>
This commit is contained in:
Ching 2023-09-16 13:03:31 +08:00
parent e95c543170
commit df6d64f432
4 changed files with 59 additions and 14 deletions

View File

@ -1,6 +1,7 @@
import discord import discord
from discord.ext import tasks from discord.ext import tasks
import dota import dota
import utils
bot = discord.Bot(proxy='http://127.0.0.1:1235') bot = discord.Bot(proxy='http://127.0.0.1:1235')
@ -10,12 +11,16 @@ async def on_ready():
channel_id = 1152167937852055552 channel_id = 1152167937852055552
@tasks.loop(seconds=10) @tasks.loop(minutes=1)
async def send_message(channel): 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() matches = dota.get_friends_recent_matches()
for match_ in matches: for match_ in matches:
data = dota.serialize_match_for_discord(match_) 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']]) await channel.send(content=data['content'], embeds=[discord.Embed.from_dict(embed) for embed in data['embeds']])
@bot.event @bot.event

BIN
dota.db

Binary file not shown.

51
dota.py
View File

@ -89,20 +89,29 @@ def get_friends_recent_matches():
def serialize_match_for_discord(match_): 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, # "tts": false,
# "embeds": [ # "embeds": [
# { # {
# "id": 652627557, # "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, # "color": 6732650,
# "fields": [ # "fields": [
# { # {
# "id": 878517961, # "id": 878517961,
# "name": "夜魇 - 23", # "name": "天辉",
# "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 伤害", # "value": "23",
# "inline": false # "inline": true
# },
# {
# "id": 159867313,
# "name": "夜魇 ",
# "value": "23",
# "inline": true
# },
# {
# "id": 202767768,
# "name": "3黑 受风,小金 00:34:23 赢",
# "value": ""
# } # }
# ], # ],
# "author": { # "author": {
@ -123,7 +132,9 @@ def serialize_match_for_discord(match_):
break break
win = is_radiant == match_['radiant_win'] win = is_radiant == match_['radiant_win']
if match_['party_size'] > 1: 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 = [] radiant = []
dire = [] dire = []
@ -136,20 +147,36 @@ def serialize_match_for_discord(match_):
color = 6732650 if win else 16724787 # 66bb6a or FF3333 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 = { data = {
"content": content, "content": content,
"tts": False,
"embeds": [ "embeds": [
{ {
"description": '\n'.join(radiant),
"color": color, "color": color,
"fields": [ "fields": [
{ {
"name": "夜魇 - %s" % match_['dire_score'], "name": "天辉" + radiant_indicator,
"value": '\n'.join(dire), "value": match_['radiant_score'],
"inline": False "inline": True
},
{
"name": "夜魇" + dire_indicator,
"value": match_['dire_score'],
"inline": True
},
{
"name": summary,
"value": f"{''.join(party)}"
} }
], ],
"title": "天辉 - %s" % match_['radiant_score'],
"author": { "author": {
"name": "opendota", "name": "opendota",
"url": "https://www.opendota.com/matches/%s" % match_['match_id'] "url": "https://www.opendota.com/matches/%s" % match_['match_id']

View File

@ -1,4 +1,17 @@
import datetime
import requests
def convert_seconds_to_hms(total_seconds): def convert_seconds_to_hms(total_seconds):
hours, remainder = divmod(total_seconds, 3600) hours, remainder = divmod(total_seconds, 3600)
minutes, seconds = divmod(remainder, 60) minutes, seconds = divmod(remainder, 60)
return hours, minutes, seconds 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