54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
import discord
|
|
from discord.ext import tasks
|
|
import logging
|
|
|
|
import dota
|
|
import utils
|
|
|
|
|
|
formatter = logging.Formatter('%(levelname)s %(name)s %(asctime)s %(message)s', '%Y-%m-%d %H:%M:%S')
|
|
log_handler = logging.FileHandler(utils.logger_file)
|
|
log_handler.setFormatter(formatter)
|
|
logger = logging.getLogger(__name__)
|
|
logger.addHandler(log_handler)
|
|
logger.propagate = False
|
|
|
|
logger.info('start bot')
|
|
|
|
# bot = discord.Bot(proxy='http://127.0.0.1:1235')
|
|
bot = discord.Bot()
|
|
|
|
@bot.event
|
|
async def on_ready():
|
|
logger.info(f"We have logged in as {bot.user}")
|
|
|
|
channel_id = 1152167937852055552
|
|
|
|
@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)
|
|
|
|
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
|