feat(bot.py): 完成了发送消息功能

完成了发送消息功能

Signed-off-by: Ching <loooching@gmail.com>
This commit is contained in:
Ching 2023-09-16 04:34:52 +08:00
parent 2133b78299
commit ee77d2bc92
4 changed files with 109 additions and 24 deletions

View File

@ -1,16 +1,26 @@
import discord
from discord.ext import tasks
import dota
bot = discord.Bot()
bot = discord.Bot(proxy='http://127.0.0.1:1235')
@bot.event
async def on_ready():
print(f"We have logged in as {bot.user}")
channel_id = 1152167937852055552
@tasks.loop(seconds=10)
async def send_message():
channel = bot.get_channel(1152167937852055552)
await channel.send('Hello World!')
print('send message')
async def send_message(channel):
matches = dota.get_friends_recent_matches()
for match_ in matches:
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

BIN
dota.db

Binary file not shown.

110
dota.py
View File

@ -11,6 +11,7 @@ hero_client = opendota.HeroesApi()
player_client = opendota.PlayersApi()
match_client = opendota.MatchesApi()
class BaseModel(peewee.Model):
class Meta:
database = db
@ -48,15 +49,17 @@ class Match(BaseModel):
party_size = peewee.IntegerField()
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 = {
'players': [players.serialize_player(player) for player in match.players],
'dire_score': match.dire_score,
'radiant_score': match.radiant_score,
'start_time': datetime.datetime.fromtimestamp(match.start_time).strftime('%Y-%m-%d %H:%M:%S'),
'duration': '%d:%02d:%02d' % utils.convert_seconds_to_hms(match.duration),
'radiant_win': match.radiant_win,
'players': [players.serialize_player(player) for player in match_.players],
'dire_score': match_.dire_score,
'radiant_score': match_.radiant_score,
# isoformat utc+8
'start_time': datetime.datetime.fromtimestamp(match_.start_time).strftime('%Y-%m-%dT%H:%M:%S.000+08:00'),
'duration': '%d:%02d:%02d' % utils.convert_seconds_to_hms(match_.duration),
'radiant_win': match_.radiant_win,
'party_size': self.party_size,
'match_id': self.match_id,
}
return match_data
@ -70,18 +73,89 @@ class Friend(BaseModel):
def get_friends_recent_matches():
matches = []
for friend in Friend.select():
for match in friend.get_recent_matches():
if not Match.select().where(Match.match_id == match.match_id).exists():
match_ = Match.create(
match_id=match.match_id,
start_time=datetime.datetime.fromtimestamp(match.start_time),
duration=match.duration,
radiant_win=match.radiant_win,
party_size=match.party_size,
for match_ in friend.get_recent_matches():
if not Match.select().where(Match.match_id == match_.match_id).exists():
match_obj = Match.create(
match_id=match_.match_id,
start_time=datetime.datetime.fromtimestamp(match_.start_time),
duration=match_.duration,
radiant_win=match_.radiant_win,
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):
print(match)
radiant = []
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

View File

@ -11,10 +11,11 @@ def serialize_player(player):
'total_gold': player.total_gold,
'last_hits': player.last_hits,
'denies': player.denies,
'hero_damage': player.hero_damage,
'party_id': player.party_id,
'win': player.win,
'level': player.level,
'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