Skip to content

Commit

Permalink
refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrWieczorek98 committed Dec 22, 2024
1 parent 13032f5 commit 31cbcd8
Show file tree
Hide file tree
Showing 8 changed files with 627 additions and 522 deletions.
62 changes: 45 additions & 17 deletions src/audio_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,95 @@

class AudioPlayer(wavelink.Player):
"""
Expands default wavelink player class functionality
Extends the functionality of the default Wavelink player class.
"""

def __init__(
self,
client: discord.Client = MISSING,
channel: Connectable = MISSING,
*,
nodes: list[wavelink.Node] | None = None
nodes: list[wavelink.Node] | None = None,
):
super().__init__(client=client, channel=channel, nodes=nodes)
self._filters_applied = False

@property
def filters_applied(self):
def filters_applied(self) -> bool:
"""
Indicates whether audio filters are currently applied.
"""
return self._filters_applied

async def play_track(self, playable: wavelink.Search, start_time: int):
async def play_track(self, playable: wavelink.Search, start_time: int = 0) -> None:
"""
Plays a track, starting at a specific time.
Args:
playable (wavelink.Search): The track to be played.
start_time (int): The time (in seconds) to start playback. Defaults to 0.
"""
self.autoplay = wavelink.AutoPlayMode.partial
await self.queue.put_wait(playable)
if not self.playing:
await self.play(self.queue.get(), start=start_time)

async def play_previous_track(self):
async def play_previous_track(self) -> None:
"""
Plays the previously played track from the queue history.
"""
queue = self.queue
history = self.queue.history

# If player is currently playing a track then last object in history is that track
# (because button is disabled for the very first track)
# otherwise last object in history is a previous track
if self.playing:
current_track = history[-1]
previous_track = history[-2]
queue._queue.appendleft(current_track)
await history.delete(-1)
queue._queue.appendleft(current_track) # Moves the current track back to the queue
await history.delete(-1) # Removes the current track from history
else:
previous_track = history[-1]

await self.play(previous_track, add_history=False)

async def play_track_from_queue(self, index: int):
async def play_track_from_queue(self, index: int) -> None:
"""
Plays a specific track from the queue by index.
Args:
index (int): The index of the track in the queue.
"""
track = self.queue[index]
await self.queue.delete(index)
await self.play(track)

async def play_track_from_history(self, index: int):
async def play_track_from_history(self, index: int) -> None:
"""
Plays a specific track from the queue history by index.
Args:
index (int): The index of the track in the history.
"""
history = self.queue.history
track = history[index]
await history.delete(index)
await self.play(track)

async def disable_filters(self):
async def disable_filters(self) -> None:
"""
Disables all currently applied audio filters.
"""
await self.set_filters()
self._filters_applied = False

async def toggle_nightcore_filter(self):
async def toggle_nightcore_filter(self) -> None:
"""
Toggles the Nightcore audio filter on or off.
"""
if self._filters_applied:
await self.set_filters()
await self.set_filters() # Resets filters to default
else:
filters: wavelink.Filters = wavelink.Filters()
filters.timescale.set(pitch=1.2, speed=1.1, rate=1)
filters = wavelink.Filters()
filters.timescale.set(pitch=1.2, speed=1.1, rate=1.0)
filters.equalizer.reset()
await self.set_filters(filters)

Expand Down
42 changes: 29 additions & 13 deletions src/cogs/admin_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,73 @@

class AdminCog(commands.Cog):
"""
Class used for administrative bot commands
Class used for administrative bot commands.
"""

def __init__(self, bot: DiscordBot) -> None:
super().__init__()
self.bot: DiscordBot = bot
self.bot = bot

# Implement meta functions
@self.bot.command()
@commands.guild_only()
async def sync(
ctx: Context, guilds: Greedy[discord.Object], spec: Optional[Literal["~", "*", "^"]] = None
) -> None:
"""
This command sync slash commands with discord
Syncs slash commands with Discord.
Parameters:
guilds (Greedy[discord.Object]): Guilds to sync the commands to.
spec (Literal["~", "*", "^"], optional): Syncing option:
- "~": Sync only to the current guild.
- "*": Copy global commands to the current guild.
- "^": Clear commands from the current guild.
- None: Sync globally.
"""
bot = cast(DiscordBot, ctx.bot)

# Handle syncing based on spec and guilds
if not guilds:
if spec == "~":
synced = await bot.tree.sync(guild=ctx.guild)
location = "to the current guild"
elif spec == "*":
bot.tree.copy_global_to(guild=ctx.guild)
synced = await bot.tree.sync(guild=ctx.guild)
location = "to the current guild (including global commands)"
elif spec == "^":
bot.tree.clear_commands(guild=ctx.guild)
await bot.tree.sync(guild=ctx.guild)
synced = []
location = "after clearing commands from the current guild"
else:
synced = await bot.tree.sync()
location = "globally"

is_spec = 'globally' if spec is None else 'to the current guild.'
await ctx.send(f"Synced {len(synced)} commands {is_spec}")
await ctx.send(f"Synced {len(synced)} commands {location}.")
return

ret = 0
# Sync to specific guilds
successful_syncs = 0
for guild in guilds:
try:
await bot.tree.sync(guild=guild)
except discord.HTTPException:
pass
continue
else:
ret += 1
successful_syncs += 1

await ctx.send(f"Synced the tree to {ret}/{len(guilds)}.")
await ctx.send(f"Synced the tree to {successful_syncs}/{len(guilds)} guilds.")

@commands.guild_only()
@app_commands.command(name="restart")
async def restart_bot(self, interaction: discord.Interaction, member: discord.Member = None) -> None:
async def restart_bot(self, interaction: discord.Interaction) -> None:
"""
This command exits the program which should automatically reboot the container
Restarts the bot by exiting the program, which should trigger a container reboot.
Parameters:
interaction (discord.Interaction): The interaction triggering the command.
"""
await interaction.response.send_message(content="BRB")
logging.warning('Restart called from %d', interaction.guild.id)
logging.warning("Restart called from guild: %d", interaction.guild.id)
sys.exit()
Loading

0 comments on commit 31cbcd8

Please sign in to comment.