Skip to content

Commit 0d86f6a

Browse files
committed
initialize audios setting cache at cog startup to reduce config calls
1 parent 2d024ac commit 0d86f6a

11 files changed

Lines changed: 48 additions & 112 deletions

File tree

redbot/cogs/audio/core/commands/audioset.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -600,9 +600,7 @@ async def command_audioset_historical_queue(self, ctx: commands.Context):
600600
601601
Daily queues creates a playlist for all tracks played today.
602602
"""
603-
daily_playlists = self._daily_playlist_cache.setdefault(
604-
ctx.guild.id, await self.config.guild(ctx.guild).daily_playlists()
605-
)
603+
daily_playlists = self._daily_playlist_cache.get(ctx.guild.id)
606604
await self.config.guild(ctx.guild).daily_playlists.set(not daily_playlists)
607605
self._daily_playlist_cache[ctx.guild.id] = not daily_playlists
608606
await self.send_embed_msg(
@@ -644,9 +642,7 @@ async def command_audioset_dj(self, ctx: commands.Context):
644642
645643
DJ mode allows users with the DJ role to use audio commands.
646644
"""
647-
dj_role = self._dj_role_cache.setdefault(
648-
ctx.guild.id, await self.config.guild(ctx.guild).dj_role()
649-
)
645+
dj_role = self._dj_role_cache.get(ctx.guild.id)
650646
dj_role = ctx.guild.get_role(dj_role)
651647
if dj_role is None:
652648
await self.send_embed_msg(
@@ -665,9 +661,7 @@ async def command_audioset_dj(self, ctx: commands.Context):
665661
return await self.send_embed_msg(
666662
ctx, title=_("Response timed out, try again later.")
667663
)
668-
dj_enabled = self._dj_status_cache.setdefault(
669-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
670-
)
664+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
671665
await self.config.guild(ctx.guild).dj_enabled.set(not dj_enabled)
672666
self._dj_status_cache[ctx.guild.id] = not dj_enabled
673667
await self.send_embed_msg(
@@ -943,9 +937,7 @@ async def command_audioset_role(self, ctx: commands.Context, *, role_name: disco
943937
"""Set the role to use for DJ mode."""
944938
await self.config.guild(ctx.guild).dj_role.set(role_name.id)
945939
self._dj_role_cache[ctx.guild.id] = role_name.id
946-
dj_role = self._dj_role_cache.setdefault(
947-
ctx.guild.id, await self.config.guild(ctx.guild).dj_role()
948-
)
940+
dj_role = self._dj_role_cache.get(ctx.guild.id)
949941
dj_role_obj = ctx.guild.get_role(dj_role)
950942
await self.send_embed_msg(
951943
ctx,
@@ -1440,9 +1432,7 @@ async def command_audioset_persist_queue(self, ctx: commands.Context):
14401432
14411433
Persistent queues allows the current queue to be restored when the queue closes.
14421434
"""
1443-
persist_cache = self._persist_queue_cache.setdefault(
1444-
ctx.guild.id, await self.config.guild(ctx.guild).persist_queue()
1445-
)
1435+
persist_cache = self._persist_queue_cache.get(ctx.guild.id)
14461436
await self.config.guild(ctx.guild).persist_queue.set(not persist_cache)
14471437
self._persist_queue_cache[ctx.guild.id] = not persist_cache
14481438
await self.send_embed_msg(

redbot/cogs/audio/core/commands/controller.py

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ async def command_disconnect(self, ctx: commands.Context):
3333
if not self._player_check(ctx):
3434
return await self.send_embed_msg(ctx, title=_("Nothing playing."))
3535
else:
36-
dj_enabled = self._dj_status_cache.setdefault(
37-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
38-
)
36+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
3937
vote_enabled = await self.config.guild(ctx.guild).vote_enabled()
4038
player = lavalink.get_player(ctx.guild.id)
4139
can_skip = await self._can_instaskip(ctx, ctx.author)
@@ -146,9 +144,7 @@ async def command_now(self, ctx: commands.Context):
146144

147145
player.store("np_message", message)
148146

149-
dj_enabled = self._dj_status_cache.setdefault(
150-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
151-
)
147+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
152148
vote_enabled = await self.config.guild(ctx.guild).vote_enabled()
153149
if (
154150
(dj_enabled or vote_enabled)
@@ -198,9 +194,7 @@ async def command_now(self, ctx: commands.Context):
198194
@commands.bot_has_permissions(embed_links=True)
199195
async def command_pause(self, ctx: commands.Context):
200196
"""Pause or resume a playing track."""
201-
dj_enabled = self._dj_status_cache.setdefault(
202-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
203-
)
197+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
204198
if not self._player_check(ctx):
205199
return await self.send_embed_msg(ctx, title=_("Nothing playing."))
206200
player = lavalink.get_player(ctx.guild.id)
@@ -242,9 +236,7 @@ async def command_prev(self, ctx: commands.Context):
242236
"""Skip to the start of the previously played track."""
243237
if not self._player_check(ctx):
244238
return await self.send_embed_msg(ctx, title=_("Nothing playing."))
245-
dj_enabled = self._dj_status_cache.setdefault(
246-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
247-
)
239+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
248240
vote_enabled = await self.config.guild(ctx.guild).vote_enabled()
249241
is_alone = await self.is_requester_alone(ctx)
250242
is_requester = await self.is_requester(ctx, ctx.author)
@@ -306,9 +298,7 @@ async def command_seek(self, ctx: commands.Context, seconds: Union[int, str]):
306298
307299
Accepts seconds or a value formatted like 00:00:00 (`hh:mm:ss`) or 00:00 (`mm:ss`).
308300
"""
309-
dj_enabled = self._dj_status_cache.setdefault(
310-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
311-
)
301+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
312302
vote_enabled = await self.config.guild(ctx.guild).vote_enabled()
313303
is_alone = await self.is_requester_alone(ctx)
314304
is_requester = await self.is_requester(ctx, ctx.author)
@@ -389,9 +379,7 @@ async def command_seek(self, ctx: commands.Context, seconds: Union[int, str]):
389379
async def command_shuffle(self, ctx: commands.Context):
390380
"""Toggle shuffle."""
391381
if ctx.invoked_subcommand is None:
392-
dj_enabled = self._dj_status_cache.setdefault(
393-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
394-
)
382+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
395383
can_skip = await self._can_instaskip(ctx, ctx.author)
396384
if dj_enabled and not can_skip:
397385
return await self.send_embed_msg(
@@ -433,9 +421,7 @@ async def command_shuffle_bumpped(self, ctx: commands.Context):
433421
Set this to disabled if you wish to avoid bumped songs being shuffled. This takes priority
434422
over `[p]shuffle`.
435423
"""
436-
dj_enabled = self._dj_status_cache.setdefault(
437-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
438-
)
424+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
439425
can_skip = await self._can_instaskip(ctx, ctx.author)
440426
if dj_enabled and not can_skip:
441427
return await self.send_embed_msg(
@@ -485,9 +471,7 @@ async def command_skip(self, ctx: commands.Context, skip_to_track: int = None):
485471
)
486472
if not player.current:
487473
return await self.send_embed_msg(ctx, title=_("Nothing playing."))
488-
dj_enabled = self._dj_status_cache.setdefault(
489-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
490-
)
474+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
491475
vote_enabled = await self.config.guild(ctx.guild).vote_enabled()
492476
is_alone = await self.is_requester_alone(ctx)
493477
is_requester = await self.is_requester(ctx, ctx.author)
@@ -563,9 +547,7 @@ async def command_skip(self, ctx: commands.Context, skip_to_track: int = None):
563547
@commands.bot_has_permissions(embed_links=True)
564548
async def command_stop(self, ctx: commands.Context):
565549
"""Stop playback and clear the queue."""
566-
dj_enabled = self._dj_status_cache.setdefault(
567-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
568-
)
550+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
569551
vote_enabled = await self.config.guild(ctx.guild).vote_enabled()
570552
if not self._player_check(ctx):
571553
return await self.send_embed_msg(ctx, title=_("Nothing playing."))
@@ -619,9 +601,7 @@ async def command_stop(self, ctx: commands.Context):
619601
@commands.bot_has_permissions(embed_links=True)
620602
async def command_summon(self, ctx: commands.Context):
621603
"""Summon the bot to a voice channel."""
622-
dj_enabled = self._dj_status_cache.setdefault(
623-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
624-
)
604+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
625605
vote_enabled = await self.config.guild(ctx.guild).vote_enabled()
626606
is_alone = await self.is_requester_alone(ctx)
627607
is_requester = await self.is_requester(ctx, ctx.author)
@@ -697,9 +677,7 @@ async def command_summon(self, ctx: commands.Context):
697677
@commands.bot_has_permissions(embed_links=True)
698678
async def command_volume(self, ctx: commands.Context, vol: int = None):
699679
"""Set the volume, 1% - 150%."""
700-
dj_enabled = self._dj_status_cache.setdefault(
701-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
702-
)
680+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
703681
can_skip = await self._can_instaskip(ctx, ctx.author)
704682
max_volume = await self.config.guild(ctx.guild).max_volume()
705683

@@ -744,9 +722,7 @@ async def command_volume(self, ctx: commands.Context, vol: int = None):
744722
@commands.bot_has_permissions(embed_links=True)
745723
async def command_repeat(self, ctx: commands.Context):
746724
"""Toggle repeat."""
747-
dj_enabled = self._dj_status_cache.setdefault(
748-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
749-
)
725+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
750726
can_skip = await self._can_instaskip(ctx, ctx.author)
751727
if dj_enabled and not can_skip and not await self._has_dj_role(ctx, ctx.author):
752728
return await self.send_embed_msg(
@@ -788,9 +764,7 @@ async def command_repeat(self, ctx: commands.Context):
788764
@commands.bot_has_permissions(embed_links=True)
789765
async def command_remove(self, ctx: commands.Context, index_or_url: Union[int, str]):
790766
"""Remove a specific track number from the queue."""
791-
dj_enabled = self._dj_status_cache.setdefault(
792-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
793-
)
767+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
794768
if not self._player_check(ctx):
795769
return await self.send_embed_msg(ctx, title=_("Nothing playing."))
796770
player = lavalink.get_player(ctx.guild.id)
@@ -865,9 +839,7 @@ async def command_remove(self, ctx: commands.Context, index_or_url: Union[int, s
865839
@commands.bot_has_permissions(embed_links=True)
866840
async def command_bump(self, ctx: commands.Context, index: int):
867841
"""Bump a track number to the top of the queue."""
868-
dj_enabled = self._dj_status_cache.setdefault(
869-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
870-
)
842+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
871843
if not self._player_check(ctx):
872844
return await self.send_embed_msg(ctx, title=_("Nothing playing."))
873845
player = lavalink.get_player(ctx.guild.id)

redbot/cogs/audio/core/commands/equalizer.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ async def command_equalizer(self, ctx: commands.Context):
3737
if not self._player_check(ctx):
3838
ctx.command.reset_cooldown(ctx)
3939
return await self.send_embed_msg(ctx, title=_("Nothing playing."))
40-
dj_enabled = self._dj_status_cache.setdefault(
41-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
42-
)
40+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
4341
player = lavalink.get_player(ctx.guild.id)
4442
eq = player.fetch("eq", Equalizer())
4543
reactions = [
@@ -163,9 +161,7 @@ async def command_equalizer_load(self, ctx: commands.Context, eq_preset: str):
163161
if not self._player_check(ctx):
164162
return await self.send_embed_msg(ctx, title=_("Nothing playing."))
165163

166-
dj_enabled = self._dj_status_cache.setdefault(
167-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
168-
)
164+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
169165
player = lavalink.get_player(ctx.guild.id)
170166
if dj_enabled and not await self._can_instaskip(ctx, ctx.author):
171167
return await self.send_embed_msg(
@@ -192,9 +188,7 @@ async def command_equalizer_reset(self, ctx: commands.Context):
192188
"""Reset the eq to 0 across all bands."""
193189
if not self._player_check(ctx):
194190
return await self.send_embed_msg(ctx, title=_("Nothing playing."))
195-
dj_enabled = self._dj_status_cache.setdefault(
196-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
197-
)
191+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
198192
if dj_enabled and not await self._can_instaskip(ctx, ctx.author):
199193
return await self.send_embed_msg(
200194
ctx,
@@ -226,9 +220,7 @@ async def command_equalizer_save(self, ctx: commands.Context, eq_preset: str = N
226220
"""Save the current eq settings to a preset."""
227221
if not self._player_check(ctx):
228222
return await self.send_embed_msg(ctx, title=_("Nothing playing."))
229-
dj_enabled = self._dj_status_cache.setdefault(
230-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
231-
)
223+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
232224
if dj_enabled and not await self._can_instaskip(ctx, ctx.author):
233225
ctx.command.reset_cooldown(ctx)
234226
return await self.send_embed_msg(
@@ -316,9 +308,7 @@ async def command_equalizer_set(
316308
if not self._player_check(ctx):
317309
return await self.send_embed_msg(ctx, title=_("Nothing playing."))
318310

319-
dj_enabled = self._dj_status_cache.setdefault(
320-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
321-
)
311+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
322312
if dj_enabled and not await self._can_instaskip(ctx, ctx.author):
323313
return await self.send_embed_msg(
324314
ctx,

redbot/cogs/audio/core/commands/localtracks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ async def _local_folder_menu(
110110
"\N{BLACK RIGHTWARDS ARROW}\N{VARIATION SELECTOR-16}": next_page,
111111
}
112112

113-
dj_enabled = await self.config.guild(ctx.guild).dj_enabled()
113+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
114114
if dj_enabled and not await self._can_instaskip(ctx, ctx.author):
115115
return await menu(ctx, folder_page_list, DEFAULT_CONTROLS)
116116
else:

redbot/cogs/audio/core/commands/player.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -919,9 +919,7 @@ async def _search_menu(
919919
else:
920920
tracks = query
921921

922-
dj_enabled = self._dj_status_cache.setdefault(
923-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
924-
)
922+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
925923

926924
len_search_pages = math.ceil(len(tracks) / 5)
927925
search_page_list = []

redbot/cogs/audio/core/commands/playlists.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,9 +1468,7 @@ async def command_playlist_start(
14681468
if scope_data is None:
14691469
scope_data = [None, ctx.author, ctx.guild, False]
14701470
scope, author, guild, specified_user = scope_data
1471-
dj_enabled = self._dj_status_cache.setdefault(
1472-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
1473-
)
1471+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
14741472
if dj_enabled and not await self._can_instaskip(ctx, ctx.author):
14751473
ctx.command.reset_cooldown(ctx)
14761474
await self.send_embed_msg(

redbot/cogs/audio/core/commands/queue.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,7 @@ async def command_queue_clear(self, ctx: commands.Context):
181181
player = lavalink.get_player(ctx.guild.id)
182182
except (NodeNotFound, PlayerNotFound):
183183
return await self.send_embed_msg(ctx, title=_("There's nothing in the queue."))
184-
dj_enabled = self._dj_status_cache.setdefault(
185-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
186-
)
184+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
187185
if not self._player_check(ctx) or not player.queue:
188186
return await self.send_embed_msg(ctx, title=_("There's nothing in the queue."))
189187
if (
@@ -212,9 +210,7 @@ async def command_queue_clean(self, ctx: commands.Context):
212210
player = lavalink.get_player(ctx.guild.id)
213211
except (NodeNotFound, PlayerNotFound):
214212
return await self.send_embed_msg(ctx, title=_("There's nothing in the queue."))
215-
dj_enabled = self._dj_status_cache.setdefault(
216-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
217-
)
213+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
218214
if not self._player_check(ctx) or not player.queue:
219215
return await self.send_embed_msg(ctx, title=_("There's nothing in the queue."))
220216
if (
@@ -309,9 +305,7 @@ async def command_queue_search(self, ctx: commands.Context, *, search_words: str
309305
@commands.cooldown(1, 30, commands.BucketType.guild)
310306
async def command_queue_shuffle(self, ctx: commands.Context):
311307
"""Shuffles the queue."""
312-
dj_enabled = self._dj_status_cache.setdefault(
313-
ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
314-
)
308+
dj_enabled = self._dj_status_cache.get(ctx.guild.id)
315309
if (
316310
dj_enabled
317311
and not await self._can_instaskip(ctx, ctx.author)

redbot/cogs/audio/core/events/cog.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ async def on_red_audio_track_start(
3939

4040
track_identifier = track.track_identifier
4141
if self.playlist_api is not None:
42-
daily_cache = self._daily_playlist_cache.setdefault(
43-
guild.id, await self.config.guild(guild).daily_playlists()
44-
)
42+
daily_cache = self._daily_playlist_cache.get(guild.id)
4543
global_daily_playlists = self._daily_global_playlist_cache.setdefault(
4644
self.bot.user.id, await self.config.daily_playlists()
4745
)
@@ -137,9 +135,7 @@ async def on_red_audio_track_start(
137135
log.verbose(
138136
"Failed to delete global daily playlist ID: %s", too_old_id, exc_info=exc
139137
)
140-
persist_cache = self._persist_queue_cache.setdefault(
141-
guild.id, await self.config.guild(guild).persist_queue()
142-
)
138+
persist_cache = self._persist_queue_cache.get(guild.id)
143139
if persist_cache:
144140
await self.api_interface.persistent_queue_api.played(
145141
guild_id=guild.id, track_id=track_identifier
@@ -165,9 +161,7 @@ async def on_red_audio_track_enqueue(
165161
):
166162
if not (track and guild):
167163
return
168-
persist_cache = self._persist_queue_cache.setdefault(
169-
guild.id, await self.config.guild(guild).persist_queue()
170-
)
164+
persist_cache = self._persist_queue_cache.get(guild.id)
171165
if persist_cache:
172166
await self.api_interface.persistent_queue_api.enqueued(
173167
guild_id=guild.id, room_id=track.extras["vc"], track=track

0 commit comments

Comments
 (0)