feat(bot.py): 完成了发送消息功能
完成了发送消息功能 Signed-off-by: Ching <loooching@gmail.com>
This commit is contained in:
parent
2133b78299
commit
ee77d2bc92
@ -1,16 +1,26 @@
|
|||||||
import discord
|
import discord
|
||||||
from discord.ext import tasks
|
from discord.ext import tasks
|
||||||
|
import dota
|
||||||
|
|
||||||
bot = discord.Bot()
|
bot = discord.Bot(proxy='http://127.0.0.1:1235')
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
print(f"We have logged in as {bot.user}")
|
print(f"We have logged in as {bot.user}")
|
||||||
|
|
||||||
|
channel_id = 1152167937852055552
|
||||||
|
|
||||||
@tasks.loop(seconds=10)
|
@tasks.loop(seconds=10)
|
||||||
async def send_message():
|
async def send_message(channel):
|
||||||
channel = bot.get_channel(1152167937852055552)
|
matches = dota.get_friends_recent_matches()
|
||||||
await channel.send('Hello World!')
|
for match_ in matches:
|
||||||
print('send message')
|
data = dota.serialize_match_for_discord(match_)
|
||||||
|
print(data)
|
||||||
|
await channel.send(content=data['content'], embeds=[discord.Embed.from_dict(embed) for embed in data['embeds']])
|
||||||
|
|
||||||
|
@bot.event
|
||||||
|
async def on_ready():
|
||||||
|
channel = bot.get_channel(channel_id)
|
||||||
|
send_message.start(channel)
|
||||||
|
|
||||||
bot.run('MTE1MjE2NTc3NDMwNDIyMzI2Mg.GEi-17.VvuIkRy_cFD9XF6wtTagY95LKEbTxKaxy-FxGw') # 这里替换成你自己的 token
|
bot.run('MTE1MjE2NTc3NDMwNDIyMzI2Mg.GEi-17.VvuIkRy_cFD9XF6wtTagY95LKEbTxKaxy-FxGw') # 这里替换成你自己的 token
|
||||||
|
|||||||
110
dota.py
110
dota.py
@ -11,6 +11,7 @@ hero_client = opendota.HeroesApi()
|
|||||||
player_client = opendota.PlayersApi()
|
player_client = opendota.PlayersApi()
|
||||||
match_client = opendota.MatchesApi()
|
match_client = opendota.MatchesApi()
|
||||||
|
|
||||||
|
|
||||||
class BaseModel(peewee.Model):
|
class BaseModel(peewee.Model):
|
||||||
class Meta:
|
class Meta:
|
||||||
database = db
|
database = db
|
||||||
@ -48,15 +49,17 @@ class Match(BaseModel):
|
|||||||
party_size = peewee.IntegerField()
|
party_size = peewee.IntegerField()
|
||||||
|
|
||||||
def serialize_match(self):
|
def serialize_match(self):
|
||||||
match = match_client.get_matches_by_match_id(self.match_id)
|
match_ = match_client.get_matches_by_match_id(self.match_id)
|
||||||
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,
|
||||||
'radiant_score': match.radiant_score,
|
'radiant_score': match_.radiant_score,
|
||||||
'start_time': datetime.datetime.fromtimestamp(match.start_time).strftime('%Y-%m-%d %H:%M:%S'),
|
# isoformat utc+8
|
||||||
'duration': '%d:%02d:%02d' % utils.convert_seconds_to_hms(match.duration),
|
'start_time': datetime.datetime.fromtimestamp(match_.start_time).strftime('%Y-%m-%dT%H:%M:%S.000+08:00'),
|
||||||
'radiant_win': match.radiant_win,
|
'duration': '%d:%02d:%02d' % utils.convert_seconds_to_hms(match_.duration),
|
||||||
|
'radiant_win': match_.radiant_win,
|
||||||
'party_size': self.party_size,
|
'party_size': self.party_size,
|
||||||
|
'match_id': self.match_id,
|
||||||
}
|
}
|
||||||
return match_data
|
return match_data
|
||||||
|
|
||||||
@ -70,18 +73,89 @@ class Friend(BaseModel):
|
|||||||
|
|
||||||
|
|
||||||
def get_friends_recent_matches():
|
def get_friends_recent_matches():
|
||||||
|
matches = []
|
||||||
for friend in Friend.select():
|
for friend in Friend.select():
|
||||||
for match in friend.get_recent_matches():
|
for match_ in friend.get_recent_matches():
|
||||||
if not Match.select().where(Match.match_id == match.match_id).exists():
|
if not Match.select().where(Match.match_id == match_.match_id).exists():
|
||||||
match_ = Match.create(
|
match_obj = Match.create(
|
||||||
match_id=match.match_id,
|
match_id=match_.match_id,
|
||||||
start_time=datetime.datetime.fromtimestamp(match.start_time),
|
start_time=datetime.datetime.fromtimestamp(match_.start_time),
|
||||||
duration=match.duration,
|
duration=match_.duration,
|
||||||
radiant_win=match.radiant_win,
|
radiant_win=match_.radiant_win,
|
||||||
party_size=match.party_size,
|
party_size=match_.party_size,
|
||||||
)
|
)
|
||||||
notify_match(match_.serialize_match())
|
matches.append(match_obj.serialize_match())
|
||||||
|
return matches
|
||||||
|
|
||||||
|
def serialize_match_for_discord(match_):
|
||||||
|
# {
|
||||||
|
# "content": "3黑(我, 受风,小金 )00:34:23 赢",
|
||||||
|
# "tts": false,
|
||||||
|
# "embeds": [
|
||||||
|
# {
|
||||||
|
# "id": 652627557,
|
||||||
|
# "title": "天辉 - 32",
|
||||||
|
# "description": "我(LV23 大鱼人): **2 杀 5 死 3 助** | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害",
|
||||||
|
# "color": 6732650,
|
||||||
|
# "fields": [
|
||||||
|
# {
|
||||||
|
# "id": 878517961,
|
||||||
|
# "name": "夜魇 - 23",
|
||||||
|
# "value": "我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害\n我(LV23 大鱼人): 2 杀 5 死 3 助 | 12345 经济 | 13442 伤害",
|
||||||
|
# "inline": false
|
||||||
|
# }
|
||||||
|
# ],
|
||||||
|
# "author": {
|
||||||
|
# "name": "opendota",
|
||||||
|
# "url": "https://www.opendota.com/matches/7335993790"
|
||||||
|
# },
|
||||||
|
# "timestamp": "2022-01-01T13:22:00.000Z"
|
||||||
|
# }
|
||||||
|
# ],
|
||||||
|
# "components": [],
|
||||||
|
# "actions": {}
|
||||||
|
# }
|
||||||
|
party = [player['nickname'] for player in match_['players'] if player['nickname']]
|
||||||
|
is_radiant = False
|
||||||
|
for player in match_['players']:
|
||||||
|
if player['nickname']:
|
||||||
|
is_radiant = player['is_radiant']
|
||||||
|
break
|
||||||
|
win = is_radiant == match_['radiant_win']
|
||||||
|
if match_['party_size'] > 1:
|
||||||
|
content = f"{match_['party_size']}黑({','.join(party)}){match_['duration']} {'赢' if win else '输'}"
|
||||||
|
|
||||||
def notify_match(match):
|
radiant = []
|
||||||
print(match)
|
dire = []
|
||||||
|
for player in match_['players']:
|
||||||
|
desc = f"{player['nickname'] or player['personaname']}(LV{player['level']} {player['hero']}): {player['kills']} 杀 {player['deaths']} 死 {player['assists']} 助 | {player['total_gold']} 经济 | {player['hero_damage']} 伤害"
|
||||||
|
if player['is_radiant']:
|
||||||
|
radiant.append(desc)
|
||||||
|
else:
|
||||||
|
dire.append(desc)
|
||||||
|
|
||||||
|
color = 6732650 if win else 16724787 # 66bb6a or FF3333
|
||||||
|
|
||||||
|
data = {
|
||||||
|
"content": content,
|
||||||
|
"embeds": [
|
||||||
|
{
|
||||||
|
"description": '\n'.join(radiant),
|
||||||
|
"color": color,
|
||||||
|
"fields": [
|
||||||
|
{
|
||||||
|
"name": "夜魇 - %s" % match_['dire_score'],
|
||||||
|
"value": '\n'.join(dire),
|
||||||
|
"inline": False
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"title": "天辉 - %s" % match_['radiant_score'],
|
||||||
|
"author": {
|
||||||
|
"name": "opendota",
|
||||||
|
"url": "https://www.opendota.com/matches/%s" % match_['match_id']
|
||||||
|
},
|
||||||
|
"timestamp": match_['start_time']
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
|||||||
@ -11,10 +11,11 @@ def serialize_player(player):
|
|||||||
'total_gold': player.total_gold,
|
'total_gold': player.total_gold,
|
||||||
'last_hits': player.last_hits,
|
'last_hits': player.last_hits,
|
||||||
'denies': player.denies,
|
'denies': player.denies,
|
||||||
|
'hero_damage': player.hero_damage,
|
||||||
'party_id': player.party_id,
|
'party_id': player.party_id,
|
||||||
'win': player.win,
|
'win': player.win,
|
||||||
'level': player.level,
|
'level': player.level,
|
||||||
'is_radiant': player.is_radiant,
|
'is_radiant': player.is_radiant,
|
||||||
'hero_id': dota.Hero.get(hero_id=player.hero_id).localized_name,
|
'hero': dota.Hero.get(hero_id=player.hero_id).localized_name,
|
||||||
}
|
}
|
||||||
return player_data
|
return player_data
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user