diff --git a/discord_bot.py b/discord_bot.py index ff0240f..d0c7ea1 100644 --- a/discord_bot.py +++ b/discord_bot.py @@ -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) - matches = dota.get_friends_recent_matches() + 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 diff --git a/dota.db b/dota.db index 874cd8c..718a1e7 100644 Binary files a/dota.db and b/dota.db differ diff --git a/dota.py b/dota.py index 721b843..d834100 100644 --- a/dota.py +++ b/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): - match_ = match_client.get_matches_by_match_id(self.match_id) + 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): - return player_client.get_players_by_account_id_select_matches(self.steam_id, limit=1) + 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(): diff --git a/utils.py b/utils.py index 38d6117..86290fa 100644 --- a/utils.py +++ b/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