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
|
||||
from discord.ext import tasks
|
||||
import logging
|
||||
|
||||
import dota
|
||||
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
|
||||
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
|
||||
|
||||
@ -18,14 +25,23 @@ async def send_message(channel):
|
||||
else:
|
||||
send_message.change_interval(minutes=15)
|
||||
|
||||
try:
|
||||
matches = dota.get_friends_recent_matches()
|
||||
except:
|
||||
return
|
||||
for match_ in matches:
|
||||
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']])
|
||||
|
||||
@tasks.loop(minutes=1)
|
||||
async def heartbeat():
|
||||
utils.heartbeat()
|
||||
|
||||
@bot.event
|
||||
async def on_ready():
|
||||
channel = bot.get_channel(channel_id)
|
||||
send_message.start(channel)
|
||||
heartbeat.start()
|
||||
|
||||
bot.run('MTE1MjE2NTc3NDMwNDIyMzI2Mg.GEi-17.VvuIkRy_cFD9XF6wtTagY95LKEbTxKaxy-FxGw') # 这里替换成你自己的 token
|
||||
|
||||
13
dota.py
13
dota.py
@ -1,11 +1,16 @@
|
||||
import peewee
|
||||
import opendota
|
||||
import datetime
|
||||
import logging
|
||||
|
||||
import players
|
||||
import utils
|
||||
|
||||
|
||||
logging.basicConfig(filename='/root/develop/log/dotabot.log', level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
db = peewee.SqliteDatabase('dota.db')
|
||||
hero_client = opendota.HeroesApi()
|
||||
player_client = opendota.PlayersApi()
|
||||
@ -49,7 +54,11 @@ class Match(BaseModel):
|
||||
party_size = peewee.IntegerField()
|
||||
|
||||
def serialize_match(self):
|
||||
try:
|
||||
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 = {
|
||||
'players': [players.serialize_player(player) for player in match_.players],
|
||||
'dire_score': match_.dire_score,
|
||||
@ -69,7 +78,11 @@ class Friend(BaseModel):
|
||||
name = peewee.CharField()
|
||||
|
||||
def get_recent_matches(self):
|
||||
try:
|
||||
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():
|
||||
|
||||
13
utils.py
13
utils.py
@ -1,5 +1,10 @@
|
||||
import datetime
|
||||
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):
|
||||
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
|
||||
else:
|
||||
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