using @property for existing context functions

This commit is contained in:
2021-11-06 17:56:17 +01:00
parent c3fc008f3a
commit 9b2babf4e3
3 changed files with 21 additions and 23 deletions
+15 -19
View File
@@ -241,15 +241,16 @@ class Music(BaseCog):
async def play(self, ctx: CustomContext): async def play(self, ctx: CustomContext):
"""Start the radio""" """Start the radio"""
# Get the player for this guild from cache. # Get the player for this guild from cache.
player = ctx.get_player() if ctx.player.is_connected:
if player.is_connected:
await ctx.send("Already connected") await ctx.send("Already connected")
return return
await self.fill_player_queue(player, self.bot.config["queue_buffer_size"] + 1) await self.fill_player_queue(
ctx.player, self.bot.config["queue_buffer_size"] + 1
)
if not player.is_playing: if not ctx.player.is_playing:
await player.play() await ctx.player.play()
await EmbedGenerator.Title(ctx, "*⃣ | Connected.") await EmbedGenerator.Title(ctx, "*⃣ | Connected.")
return return
@@ -257,21 +258,19 @@ class Music(BaseCog):
@commands.command(name="skip", aliases=["next"]) @commands.command(name="skip", aliases=["next"])
async def skip(self, ctx: CustomContext): async def skip(self, ctx: CustomContext):
"""Skip the current song""" """Skip the current song"""
player = ctx.get_player() await ctx.player.skip()
await player.skip()
await ctx.send("Skipped current song") await ctx.send("Skipped current song")
@commands.command(name="queue") @commands.command(name="queue")
async def queue(self, ctx: CustomContext): async def queue(self, ctx: CustomContext):
"""Display the current radio queue""" """Display the current radio queue"""
player = ctx.get_player()
embed_color = self.bot.colors["embed"] embed_color = self.bot.colors["embed"]
embed = Embed(title="Coming Up...", colour=embed_color) embed = Embed(title="Coming Up...", colour=embed_color)
if len(player.queue) > 0: if len(ctx.player.queue) > 0:
embed.description = "\n".join( embed.description = "\n".join(
f"{index}. [{track.title}]({track.uri})" f"{index}. [{track.title}]({track.uri})"
for index, track in enumerate(player.queue, 1) for index, track in enumerate(ctx.player.queue, 1)
) )
else: else:
embed.description = "We are still determining a playlist" embed.description = "We are still determining a playlist"
@@ -281,11 +280,9 @@ class Music(BaseCog):
@commands.command(name="disconnect", aliases=["dc", "stop"]) @commands.command(name="disconnect", aliases=["dc", "stop"])
async def disconnect(self, ctx: CustomContext): async def disconnect(self, ctx: CustomContext):
"""Disconnects the radio from the channel""" """Disconnects the radio from the channel"""
player = ctx.get_player()
if not ctx.author.voice or ( if not ctx.author.voice or (
player.is_connected ctx.player.is_connected
and ctx.author.voice.channel.id != int(player.channel_id) and ctx.author.voice.channel.id != int(ctx.player.channel_id)
): ):
# Abuse prevention. Users not in voice channels, or not in the same voice channel as the bot # Abuse prevention. Users not in voice channels, or not in the same voice channel as the bot
# may not disconnect the bot. # may not disconnect the bot.
@@ -294,18 +291,17 @@ class Music(BaseCog):
# Clear the queue to ensure old tracks don't start playing # Clear the queue to ensure old tracks don't start playing
# when someone else queues something. # when someone else queues something.
player.queue.clear() ctx.player.queue.clear()
# Stop the current track so Lavalink consumes less resources. # Stop the current track so Lavalink consumes less resources.
await player.stop() await ctx.player.stop()
# Disconnect from the voice channel. # Disconnect from the voice channel.
await ctx.voice_client.disconnect(force=True) await ctx.voice_client.disconnect(force=True)
await EmbedGenerator.Title(ctx, "*⃣ | Disconnected.") await EmbedGenerator.Title(ctx, "*⃣ | Disconnected.")
@commands.command(name="now") @commands.command(name="now")
async def now_playing(self, ctx: CustomContext): async def now_playing(self, ctx: CustomContext):
player: DefaultPlayer = self.bot.lavalink.player_manager.get(ctx.guild.id) """Displays information about the currently played track"""
track = player.current embed = await self.create_track_embed(ctx.player.current)
embed = await self.create_track_embed(track)
await ctx.send(embed=embed) await ctx.send(embed=embed)
+2 -2
View File
@@ -30,7 +30,7 @@ class SettingsCog(BaseCog, name="Settings"):
voicechannel_id = ctx.author.voice.channel.id voicechannel_id = ctx.author.voice.channel.id
textchannel_id = ctx.message.channel.id textchannel_id = ctx.message.channel.id
await AutoJoin.update_channel( await AutoJoin.update_channel(
ctx.get_redis(), ctx.guild.id, voicechannel_id, textchannel_id ctx.redis, ctx.guild.id, voicechannel_id, textchannel_id
) )
await EmbedGenerator.Message(ctx, "Autojoin", "`enabled`") await EmbedGenerator.Message(ctx, "Autojoin", "`enabled`")
@@ -39,7 +39,7 @@ class SettingsCog(BaseCog, name="Settings"):
@commands.cooldown(rate=1, per=5, type=commands.BucketType.user) @commands.cooldown(rate=1, per=5, type=commands.BucketType.user)
async def autojoin_del(self, ctx: CustomContext): async def autojoin_del(self, ctx: CustomContext):
"""Disable the bot automatically joining""" """Disable the bot automatically joining"""
await AutoJoin.del_channel(ctx.get_redis(), ctx.guild.id) await AutoJoin.del_channel(ctx.redis, ctx.guild.id)
await EmbedGenerator.Message(ctx, "Autojoin", "`disabled`") await EmbedGenerator.Message(ctx, "Autojoin", "`disabled`")
+4 -2
View File
@@ -5,10 +5,12 @@ from lavalink.models import DefaultPlayer
class CustomContext(commands.Context): class CustomContext(commands.Context):
def get_redis(self) -> Redis: @property
def redis(self) -> Redis:
return self.bot._redis_client return self.bot._redis_client
def get_player(self) -> DefaultPlayer: @property
def player(self) -> DefaultPlayer:
if hasattr(self.bot, "lavalink"): if hasattr(self.bot, "lavalink"):
return self.bot.lavalink.player_manager.get(self.guild.id) return self.bot.lavalink.player_manager.get(self.guild.id)