-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathembedmaker.py
More file actions
200 lines (175 loc) · 8.67 KB
/
embedmaker.py
File metadata and controls
200 lines (175 loc) · 8.67 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import discord
import serverinfo
from operator import itemgetter, attrgetter
from config import trivia_path, public_mapshot_path, public_trivia_path, public_topshot_path, public_map_path
async def split_string(maps_string):
"""
map_string: type: str
map_string: example: "[map.name](url + map.name) [map.name](url + map.name)"
"""
maps = maps_string.split()
strings = []
string = ""
last_map = maps[-1]
i = 0
while True:
# if adding next map doesn't make the string longer than 1024, do it
if not len(string + maps[i]) >= 1024:
string += maps[i] + " "
# added map wasn't last_map, continue adding
if not last_map in string:
i += 1
# last_map met, break
else:
strings.append(string)
break
# string would be over 1024, append current and create empty string
else:
strings.append(string)
if last_map in string:
break
string = ""
# return a list of strings
return strings
async def make_embed(keyword, maps=None, message=None, tags=None):
"""
keyword: type: str
maps: type: dict
maps: keys: 'junk', 'beta', 'fix', 'inprogress', 'match', 'pub', 'finished', 'classic'
message: type: str
"""
embeds = []
embed = discord.Embed(title="WIP DP2 Map Browser", description="Searching for: {}".format(keyword), color=0xfed900)
if maps:
i = 1
# cycle through all the categories
for category in maps:
# check if category is not empty
if len(maps[category]) != 0:
# default to these if category length is less than 1024
strings = [ maps[category] ]
last_string = maps[category]
# embed field[value] limit is 1024, split data to chunks of 1024 if exceeds limit
if len(maps[category]) >= 1024:
strings = await split_string(maps[category])
last_string = strings[-1]
# cycle through all the strings, embed can fit 5 fields max (6000 chars max, 5 * 1024 = 5120, but just to be sure...)
x = 0
while True:
if i <= 5:
embed.add_field(name=category, value=strings[x],
inline=False)
# keep going until last_string
if last_string != strings[x]:
i += 1
x += 1
# last_string, break
else:
x += 1
i += 1
break
# if embed has 5 fields, append current and make an empty one
else:
embeds.append(embed)
embed = discord.Embed(title="WIP DP2 Map Browser", description="Searching for: {}".format(keyword), color=0xfed900)
i = 1
# when done with all strings, append the last embed
embeds.append(embed)
# returns a list if embeds
return embeds
elif message:
# if message is not empty
if message == "":
message = "No map message"
embed.add_field(name="Results", value=message,
inline=False)
if tags:
embed.add_field(name="Tags", value=tags, inline=False)
if keyword != "No match":
# add download link
embed.add_field(name="Download", value="[CLICK HERE TO DOWNLOAD](" + public_map_path + keyword + ".bsp)",
inline=False)
# add a mapshot
embed.set_image(url=public_mapshot_path + keyword + ".jpg")
# add thumbnail
embed.set_thumbnail(url=public_topshot_path + keyword + ".jpg")
embed.set_footer(text="Powered by Toolwut's BSP images", icon_url="https://www.zwodnik.com/media/cache/c3/b9/c3b94c5c0934232730a21baa3bddcb1c.png")
# returns a single embed
return embed
def make_results(status, map_memory, teams=None, winner=None):
embed = discord.Embed(title="whoa's match broadcaster", description="The map has ended!", color=0xfed900)
winner_team = ""
scores = status.get("_scores").split()
winner_team = "Tie"
for team in scores:
for opponent in scores:
if opponent != team:
if int(team.split(":")[-1]) > int(opponent.split(":")[-1]):
winner_team = team
string = ""
if teams:
for team in scores:
string += "**{}**\n{}\n".format(team, teams[team.split(":")[0]])
embed.add_field(name="Winner team: {}".format(winner_team.split(":")[0]), value=string, inline=False)
embed.add_field(name="Map", value=status.get("map_name").split("/")[-1], inline=False)
for map in map_memory:
if map.name.split('/')[-1] == status.get("map_name").split("/")[-1]:
embed.set_thumbnail(url=public_mapshot_path + map.name + ".jpg")
if winner:
embed.add_field(name="Winner: {}".format(winner.name), value="With {} kills, {} wins the map!".format(winner.score, winner.name), inline=False)
for player in sorted(status.get("players"), key=attrgetter('score'), reverse=True):
string += "{} - {} kills\n".format(player.name, player.score)
embed.add_field(name="Scoreboard", value=string, inline=False)
return embed
def make_status(ip, port, map_memory=None):
teams = {"Red": "", "Blue": "", "Yellow": "", "Purple": "", "Observer": "", "Unknown": ""}
status = serverinfo.status(ip, port)
embed = discord.Embed(title="whoa's match broadcaster", description="Broadcasting: {}".format(status.get("hostname")), color=0xfed900)
if status.get("_scores"):
embed.add_field(name="Scores", value="{}".format(status.get("_scores")),
inline=False)
else:
if status.get("players"):
scoring_leader = sorted(status.get("players"), key=attrgetter('score'), reverse=True)[0]
embed.add_field(name="Scores", value="{} is in the lead with {} kills".format(scoring_leader.name, scoring_leader.score),
inline=False)
embed.add_field(name="Map", value="{} - Time: {}".format(status.get("mapname"), status.get("TimeLeft")), inline=False)
if status.get("mapname")
for map in map_memory:
if map.name.split('/')[-1] == status.get("mapname").split("/")[-1]:
embed.set_thumbnail(url=public_mapshot_path + map.name + ".jpg")
if status.get("players"):
playercount = len(status.get("players"))
for player in status.get("players"):
teams[player.team] += "{} - kills: {}\n".format(player.name, str(player.score))
for team in teams:
if teams[team]:
embed.add_field(name=team, value=teams[team], inline=True)
results = None
if status.get("_scores"):
for team in status.get("_scores").split():
if int(team.split(":")[-1]) >= int(status.get("fraglimit")):
results = make_results(status, map_memory, teams=teams)
elif status.get("TimeLeft") == "0:00":
results = make_results(status, map_memory, teams=teams)
else:
if status.get("players"):
scores = sorted(status.get("players"), key=attrgetter('score'), reverse=True)
if scores[0].score >= int(status.get("fraglimit")):
results = make_results(status, map_memory, winner=scores[0])
elif status.get("TimeLeft") == "0:00":
results = make_results(status, map_memory, winner=scores[0])
return embed, results
def get_servers():
servers = serverinfo.get_serverlist()
embed = discord.Embed(title="whoa's broadcaster serverlist", description="Currently {} servers with players".format(len(servers)), color=0xfed900)
x = 1
for server in servers:
embed.add_field(name="Server #{}".format(x), value="**{}**\n{}:{}".format(server[0].get("hostname"), server[1], server[2]), inline=False)
x += 1
return servers, embed
def trivia(filename):
embed = discord.Embed(title="whoa's map trivia", description="What's this map?", color=0xfed900)
embed.set_image(url=public_trivia_path + filename + ".jpg")
embed.set_footer(text="Powered by Toolwut's BSP images", icon_url="https://www.zwodnik.com/media/cache/c3/b9/c3b94c5c0934232730a21baa3bddcb1c.png")
return embed