-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
280 lines (248 loc) · 8.39 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import atexit
import os
import lights_controller as lc
import nextcord
from dotenv import load_dotenv
from nextcord import Interaction
from nextcord.ext import commands
from color_utils import dominant_color, format_color, parse_color
print("Loading Bot")
bot = commands.Bot(
activity=nextcord.Activity(
type=nextcord.ActivityType.watching,
name="the lights",
),
description="A bot to control the lights",
)
@bot.event
async def on_ready():
try:
last_configuration = lc.load_last_configuration()
lc.lights[:] = [parse_color(x) for x in last_configuration["colors"]]
lc.lights.brightness(last_configuration["brightness"])
except KeyError:
lc.lights[:] = 0
finally:
lc.lights.update()
print("Bot is ready")
@bot.event
async def on_close():
print("Bot is closing")
lc.save_last_configuration([format_color(x) for x in lc.lights], lc.lights.brightness())
@bot.slash_command(
name="ping",
description="Replies with the bot's ping to the server",
)
async def slash_command_ping(
interaction: Interaction,
):
await interaction.response.send_message(f"Ping: `{round(bot.latency * 1000)} ms`")
@bot.slash_command(
name="off",
description="Turns the lights off",
)
async def slash_command_off(
interaction: Interaction,
):
lc.lights[:] = 0
lc.lights.update()
await interaction.response.send_message("Turned the lights off")
@bot.slash_command(
name="set",
description="Sets all lights in the slice to the specified color",
)
async def slash_command_set(
interaction: Interaction,
color: str,
start: int = 0,
stop: int = len(lc.lights),
step: int = 1,
):
try:
parsed_color = parse_color(color)
except ValueError as err:
await interaction.response.send_message(str(err))
else:
try:
lc.lights[start:stop:step] = parsed_color
except ValueError:
await interaction.response.send_message(
f"`{start}` to `{stop}` with step size `{step}` is an invalid slice"
)
else:
lc.lights.update()
await interaction.response.send_message(
f"Set lights `{start}` to `{stop}` with step size `{step}` to `{color}`"
)
@bot.slash_command(
name="brightness",
description="Sets the brightness of the lights",
)
async def slash_command_brightness(
interaction: Interaction,
brightness: str | None = None,
):
if not brightness:
await interaction.response.send_message(f"Brightness: `{round(lc.lights.brightness() / 0xFF * 100)}%`")
return
try:
if "%" in brightness:
parsed_brightness = int(float(brightness[:-1]) / 100 * 0xFF)
elif "." in brightness:
parsed_brightness = int(float(brightness) * 0xFF)
else:
try:
parsed_brightness = int(brightness, 0)
except ValueError:
parsed_brightness = int(brightness, 16)
except ValueError:
await interaction.response.send_message(f"`{brightness}` is an invalid brightness")
else:
lc.lights.brightness(parsed_brightness)
lc.lights.update()
await interaction.response.send_message(f"Set brightness to `{brightness}`")
@bot.slash_command(
name="savecolor",
description="Saves the specified color",
)
async def slash_command_savecolor(
interaction: Interaction,
name: str,
color: str | None = None,
):
name = name.lower()
if not color:
color = format_color(dominant_color(lc.lights[:]))
try:
parsed_color = parse_color(color)
except ValueError as err:
await interaction.response.send_message(str(err))
else:
lc.save_color(name, format_color(parsed_color))
await interaction.response.send_message(f"Saved color `{color}` as `{name}`")
@bot.slash_command(
name="deletecolor",
description="Deletes the specified color",
)
async def slash_command_deletecolor(
interaction: Interaction,
name: str,
):
name = name.lower()
try:
lc.delete_color(name)
except KeyError:
await interaction.response.send_message(f"Color `{name}` not found")
else:
await interaction.response.send_message(f"Deleted color `{name}`")
@bot.slash_command(
name="save",
description="Saves the current pattern",
)
async def slash_command_save(
interaction: Interaction,
name: str,
):
name = name.lower()
lc.save_pattern(name, [format_color(x) for x in lc.lights], lc.lights.brightness())
await interaction.response.send_message(f"Saved current pattern as `{name}`")
@bot.slash_command(
name="delete",
description="Deletes the specified pattern",
)
async def slash_command_delete(
interaction: Interaction,
name: str,
):
name = name.lower()
try:
lc.delete_pattern(name)
except KeyError:
await interaction.response.send_message(f"Pattern `{name}` not found")
else:
await interaction.response.send_message(f"Deleted pattern `{name}`")
@bot.slash_command(
name="load",
description="Loads a saved pattern",
)
async def slash_command_load(
interaction: Interaction,
name: str,
):
name = name.lower()
try:
pattern = lc.load_pattern(name)
except KeyError:
await interaction.response.send_message(f"Pattern `{name}` not found")
else:
lc.lights[:] = [parse_color(x) for x in pattern["colors"]]
lc.lights.brightness(pattern["brightness"])
lc.lights.update()
await interaction.response.send_message(f"Loaded pattern `{name}`")
@bot.slash_command(
name="list",
description="Lists all saved patterns and colors",
)
async def slash_command_list(
interaction: Interaction,
):
await interaction.response.send_message(
embed=nextcord.Embed(title="Saved Patterns and Colors", color=dominant_color(lc.lights[:]))
.add_field(name="Colors", value="\n".join([f"`{str(x)}`" for x in lc.list_colors()[0]]))
.add_field(name="Patterns", value="\n".join([f"`{str(x)}`" for x in lc.list_patterns()[0]]))
)
@bot.slash_command(
name="flash",
description="Flashes the lights",
)
async def slash_command_flash(
interaction: Interaction,
color: str | None = None,
name: str | None = None,
brightness: str | None = None,
duration: float = 0.5,
):
try:
if brightness is not None:
if "%" in brightness:
parsed_brightness = int(float(brightness[:-1]) / 100 * 0xFF)
elif "." in brightness:
parsed_brightness = int(float(brightness) * 0xFF)
else:
try:
parsed_brightness = int(brightness, 0)
except ValueError:
parsed_brightness = int(brightness, 16)
else:
parsed_brightness = None
except ValueError:
await interaction.response.send_message(f"`{brightness}` is an invalid brightness")
return
if color is not None:
try:
parsed_color = parse_color(color)
except ValueError as err:
await interaction.response.send_message(str(err))
else:
if brightness is None or parsed_brightness is None:
parsed_brightness = lc.lights.brightness()
message = await interaction.response.send_message(f"Flashing lights `{color}` for `{duration}` seconds")
await lc.lights.animator.flash(parsed_color, parsed_brightness, duration)
await message.edit(content=f"Flashed lights `{color}` for `{duration}` seconds")
elif name is not None:
name = name.lower()
try:
pattern = lc.load_pattern(name)
except KeyError:
await interaction.response.send_message(f"Pattern `{name}` not found")
else:
message = await interaction.response.send_message(f"Flashing lights `{name}` for `{duration}` seconds")
if brightness is None or parsed_brightness is None:
parsed_brightness = int(pattern["brightness"])
await lc.lights.animator.flash([parse_color(x) for x in pattern["colors"]], parsed_brightness, duration)
await message.edit(content=f"Flashed lights `{name}` for `{duration}` seconds")
else:
await interaction.response.send_message("Please provide either a color or a pattern name")
load_dotenv()
bot.run(os.getenv("BOT_TOKEN"))
atexit.register(on_close)