-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
105 lines (80 loc) · 3.23 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import discord
import bot_utils.db_handler as dbh
from os.path import join
from asyncio import sleep
from bot_utils.player import Player
from config import dc_token, channel_id, monitored_players,\
api_score_search_interval
scoresaber_icon_url = "https://pbs.twimg.com/profile_images/1346980795513139201/rYiHR2pu_400x400.png"
song_picture = "http://new.scoresaber.com/api/static/covers/{0}.png"
db_setup_sql = open(join("sql", "db_setup.sql"), "r").read()
class BotInstance:
def __init__(self):
self.commands = []
self.loop_running = False
inte = discord.Intents.default()
self.dc = discord.Client(intents=inte)
self.setup_discord()
def register_command_event(self):
@self.dc.event
async def on_message(message: discord.Message):
print(message.content)
@self.dc.event
async def on_ready():
await announce_loop(self)
def setup_discord(self):
try:
print("Connecting to discord...")
self.register_command_event()
self.dc.run(dc_token)
except Exception as e:
print("Unable to connect to discord-api. " +
"Make sure that the api is currently available " +
"and your key is correct")
raise e
async def announce_loop(bot: BotInstance):
if bot.loop_running:
print("Somehow a second loop tried to start")
return
bot.loop_running = True
while True:
await announce_new_scores(bot)
for i in range(api_score_search_interval):
print(f"{i}/{api_score_search_interval}")
await sleep(60)
def pretty_difficulty(difficulty_raw):
diff_parts = difficulty_raw.split("_")
try:
return diff_parts[1]
except IndexError as ie:
return difficulty_raw
def get_embed(score_data, player_name):
embed = discord.Embed(title=score_data["songName"], color=0xc20000)
embed.set_author(name="New Score by {0}".format(player_name),
icon_url=scoresaber_icon_url)
embed.set_thumbnail(url=song_picture.format(score_data["songHash"]))
embed.add_field(name="Score", value=score_data["score"], inline=True)
embed.add_field(name="PP", value=score_data["pp"], inline=True)
embed.add_field(name="Time Set", value=score_data["timeSet"], inline=False)
embed.add_field(name="Diffuculty",
value=pretty_difficulty(score_data["difficultyRaw"]),
inline=True)
return embed
async def announce_new_scores(bot: BotInstance):
print("Announcing...")
channel: discord.TextChannel = bot.dc.get_channel(channel_id)
print(f"Channel {channel.name} selected!")
for player_id in monitored_players:
player: Player = Player(player_id)
if not player.is_in_db():
await player.insert()
if not await player.has_new_scores():
print(f"{player.player_name} has no new scores!")
continue
new_scores = await player.get_unannounced_scores()
for new_score in new_scores:
await channel.send(embed=get_embed(new_score, player.player_name))
if __name__ == "__main__":
dbh.init_connection()
dbh.get().send_statm(db_setup_sql)
BotInstance()