feat(dota.py, bot.py, utils.py): 增加 logger 和 heartbeat
增加 logger 和 heartbeat Signed-off-by: Ching <loooching@gmail.com>
This commit is contained in:
parent
7bf56375c6
commit
862f164f84
@ -1,13 +1,20 @@
|
|||||||
import discord
|
import discord
|
||||||
from discord.ext import tasks
|
from discord.ext import tasks
|
||||||
|
import logging
|
||||||
|
|
||||||
import dota
|
import dota
|
||||||
import utils
|
import utils
|
||||||
|
|
||||||
bot = discord.Bot(proxy='http://127.0.0.1:1235')
|
|
||||||
|
logging.basicConfig(filename='/root/develop/log/dotabot.log', level=logging.INFO)
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# bot = discord.Bot(proxy='http://127.0.0.1:1235')
|
||||||
|
bot = discord.Bot()
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
print(f"We have logged in as {bot.user}")
|
logger.info(f"We have logged in as {bot.user}")
|
||||||
|
|
||||||
channel_id = 1152167937852055552
|
channel_id = 1152167937852055552
|
||||||
|
|
||||||
@ -18,14 +25,23 @@ async def send_message(channel):
|
|||||||
else:
|
else:
|
||||||
send_message.change_interval(minutes=15)
|
send_message.change_interval(minutes=15)
|
||||||
|
|
||||||
|
try:
|
||||||
matches = dota.get_friends_recent_matches()
|
matches = dota.get_friends_recent_matches()
|
||||||
|
except:
|
||||||
|
return
|
||||||
for match_ in matches:
|
for match_ in matches:
|
||||||
data = dota.serialize_match_for_discord(match_)
|
data = dota.serialize_match_for_discord(match_)
|
||||||
|
logger.info(f'sending match {match_.match_id}, {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']])
|
||||||
|
|
||||||
|
@tasks.loop(minutes=1)
|
||||||
|
async def heartbeat():
|
||||||
|
utils.heartbeat()
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
channel = bot.get_channel(channel_id)
|
channel = bot.get_channel(channel_id)
|
||||||
send_message.start(channel)
|
send_message.start(channel)
|
||||||
|
heartbeat.start()
|
||||||
|
|
||||||
bot.run('MTE1MjE2NTc3NDMwNDIyMzI2Mg.GEi-17.VvuIkRy_cFD9XF6wtTagY95LKEbTxKaxy-FxGw') # 这里替换成你自己的 token
|
bot.run('MTE1MjE2NTc3NDMwNDIyMzI2Mg.GEi-17.VvuIkRy_cFD9XF6wtTagY95LKEbTxKaxy-FxGw') # 这里替换成你自己的 token
|
||||||
|
|||||||
13
dota.py
13
dota.py
@ -1,11 +1,16 @@
|
|||||||
import peewee
|
import peewee
|
||||||
import opendota
|
import opendota
|
||||||
import datetime
|
import datetime
|
||||||
|
import logging
|
||||||
|
|
||||||
import players
|
import players
|
||||||
import utils
|
import utils
|
||||||
|
|
||||||
|
|
||||||
|
logging.basicConfig(filename='/root/develop/log/dotabot.log', level=logging.INFO)
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
db = peewee.SqliteDatabase('dota.db')
|
db = peewee.SqliteDatabase('dota.db')
|
||||||
hero_client = opendota.HeroesApi()
|
hero_client = opendota.HeroesApi()
|
||||||
player_client = opendota.PlayersApi()
|
player_client = opendota.PlayersApi()
|
||||||
@ -49,7 +54,11 @@ class Match(BaseModel):
|
|||||||
party_size = peewee.IntegerField()
|
party_size = peewee.IntegerField()
|
||||||
|
|
||||||
def serialize_match(self):
|
def serialize_match(self):
|
||||||
|
try:
|
||||||
match_ = match_client.get_matches_by_match_id(self.match_id)
|
match_ = match_client.get_matches_by_match_id(self.match_id)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error('fail to get match %s' % self.match_id)
|
||||||
|
raise e
|
||||||
match_data = {
|
match_data = {
|
||||||
'players': [players.serialize_player(player) for player in match_.players],
|
'players': [players.serialize_player(player) for player in match_.players],
|
||||||
'dire_score': match_.dire_score,
|
'dire_score': match_.dire_score,
|
||||||
@ -69,7 +78,11 @@ class Friend(BaseModel):
|
|||||||
name = peewee.CharField()
|
name = peewee.CharField()
|
||||||
|
|
||||||
def get_recent_matches(self):
|
def get_recent_matches(self):
|
||||||
|
try:
|
||||||
return player_client.get_players_by_account_id_select_matches(self.steam_id, limit=1)
|
return player_client.get_players_by_account_id_select_matches(self.steam_id, limit=1)
|
||||||
|
except:
|
||||||
|
logger.error('fail to get player %s recent matches' % self.steam_id)
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
def get_friends_recent_matches():
|
def get_friends_recent_matches():
|
||||||
|
|||||||
13
utils.py
13
utils.py
@ -1,5 +1,10 @@
|
|||||||
import datetime
|
import datetime
|
||||||
import requests
|
import requests
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
logging.basicConfig(filename='/root/develop/log/dotabot.log', level=logging.INFO)
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
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)
|
||||||
@ -15,3 +20,11 @@ def is_game_time():
|
|||||||
return datetime.datetime.now().hour >= 21 or datetime.datetime.now().hour < 1
|
return datetime.datetime.now().hour >= 21 or datetime.datetime.now().hour < 1
|
||||||
else:
|
else:
|
||||||
return datetime.datetime.now().hour >= 10 or datetime.datetime.now().hour < 1
|
return datetime.datetime.now().hour >= 10 or datetime.datetime.now().hour < 1
|
||||||
|
|
||||||
|
|
||||||
|
def heartbeat():
|
||||||
|
try:
|
||||||
|
resp = requests.get('https://up.tunpok.com/api/push/BDb4MJWDVh?status=up&msg=OK&ping=')
|
||||||
|
except:
|
||||||
|
logger.error('fail to heartbeat, resp status: %s' % resp.status_code)
|
||||||
|
pass
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user