121 lines
4.0 KiB
Python
121 lines
4.0 KiB
Python
import discord
|
|
from discord.ext import tasks
|
|
from loguru import logger
|
|
|
|
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=3)
|
|
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']])
|
|
|
|
@bot.command(description="获取最近战绩", name='recent_matches') # this decorator makes a slash command
|
|
async def get_friends_recent_matches(ctx, name, match_count=5): # a slash command will be created with the name "ping"
|
|
await ctx.defer()
|
|
logger.info(f"get_friends_recent_matches {name} {match_count}")
|
|
friends = dota.Friend.filter(name=name)
|
|
match_count = int(match_count)
|
|
if friends.count() == 0:
|
|
await ctx.respond(content=f'找不到 {name} 的信息')
|
|
return
|
|
data = dota.Friend.serialize_recent_matches_for_discord(friends, match_count)
|
|
await ctx.respond(content=data['content'], embeds=[discord.Embed.from_dict(embed) for embed in data['embeds']])
|
|
|
|
@bot.command(description='获取朋友', name='list_friends')
|
|
async def get_friends(ctx):
|
|
logger.info(f'get_friends')
|
|
friends = dota.Friend.select()
|
|
msg = ''
|
|
if friends.count() == 0:
|
|
msg = '没有朋友'
|
|
else:
|
|
for friend in friends:
|
|
msg += f'{friend.name} {friend.steam_id}\n'
|
|
await ctx.respond(content=msg)
|
|
|
|
@bot.command(description='修改朋友信息', name='mod_friend')
|
|
async def mod_friend(ctx, steam_id, name):
|
|
logger.info(f'mod_friend {steam_id} {name}')
|
|
friend = dota.Friend.get_or_none(steam_id=steam_id)
|
|
if friend:
|
|
friend.name = name
|
|
friend.save()
|
|
await ctx.respond(content=f'修改成功 {steam_id} {name}')
|
|
else:
|
|
await ctx.respond(content=f'找不到 {steam_id}')
|
|
|
|
@bot.command(description='添加朋友', name='add_friend')
|
|
async def add_friend(ctx, steam_id, name):
|
|
logger.info(f'add_friend {steam_id} {name}')
|
|
friend = dota.Friend.get_or_none(steam_id=steam_id)
|
|
if friend:
|
|
await ctx.respond(content=f'已经存在 {steam_id} {name}')
|
|
else:
|
|
friend = dota.Friend.create(steam_id=steam_id, name=name, active=True)
|
|
await ctx.respond(content=f'添加成功 {steam_id} {name}')
|
|
|
|
@bot.command(description='禁用朋友', name='deactivate_friend')
|
|
async def deactivate_friend(ctx, steam_id):
|
|
logger.info(f'deactivate_friend {steam_id}')
|
|
friend = dota.Friend.get_or_none(steam_id=steam_id)
|
|
if friend:
|
|
friend.active = False
|
|
friend.save()
|
|
await ctx.respond(content=f'禁用成功 {steam_id}')
|
|
else:
|
|
await ctx.respond(content=f'找不到 {steam_id}')
|
|
|
|
@bot.command(description='启用朋友', name='activate_friend')
|
|
async def activate_friend(ctx, steam_id):
|
|
logger.info(f'activate_friend {steam_id}')
|
|
friend = dota.Friend.get_or_none(steam_id=steam_id)
|
|
if friend:
|
|
friend.active = True
|
|
friend.save()
|
|
await ctx.respond(content=f'启用成功 {steam_id}')
|
|
else:
|
|
await ctx.respond(content=f'找不到 {steam_id}')
|
|
|
|
@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
|