Skip to content

Commit

Permalink
remake pause
Browse files Browse the repository at this point in the history
  • Loading branch information
solaluset committed Dec 29, 2022
1 parent acf5c28 commit 9d6e5f5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
2 changes: 0 additions & 2 deletions config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@
HELP_VOL_LONG = "Changes the volume of the AudioPlayer. Argument specifies the % to which the volume should be set."
HELP_PREV_SHORT = "Go back one Song"
HELP_PREV_LONG = "Plays the previous song again."
HELP_RESUME_SHORT = "Resume Music"
HELP_RESUME_LONG = "Resumes the AudioPlayer."
HELP_SKIP_SHORT = "Skip a song"
HELP_SKIP_LONG = "Skips the currently playing song and goes to the next item in the queue."
HELP_SONGINFO_SHORT = "Info about current Song"
Expand Down
20 changes: 20 additions & 0 deletions musicbot/audiocontroller.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from enum import Enum
from typing import TYPE_CHECKING, Coroutine, Optional, List, Tuple

import discord
Expand All @@ -16,6 +17,12 @@
_cached_downloaders: List[Tuple[dict, yt_dlp.YoutubeDL]] = []


class PauseState(Enum):
NOTHING_TO_PAUSE = "Nothing to pause or resume."
PAUSED = "Playback Paused :pause_button:"
RESUMED = "Resumed playback :arrow_forward:"


class AudioController(object):
"""Controls the playback of audio and the sequential playing of the songs.
Expand Down Expand Up @@ -105,6 +112,19 @@ def track_history(self):
history_string += "\n" + trackname
return history_string

def pause(self):
client = self.guild.voice_client
if client:
if client.is_playing():
client.pause()
return PauseState.PAUSED
elif client.is_paused():
client.resume()
return PauseState.RESUMED
else:
return PauseState.NOTHING_TO_PAUSE
return PauseState.NOTHING_TO_PAUSE

def next_song(self, error=None):
"""Invoked after a song is finished. Plays the next song if there is one."""

Expand Down
35 changes: 8 additions & 27 deletions musicbot/commands/music.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import discord
from config import config
from discord.ext import commands, bridge

from config import config
from musicbot import linkutils, utils
from musicbot.bot import MusicBot, Context

Expand Down Expand Up @@ -114,22 +115,17 @@ async def _shuffle(self, ctx: Context):
audiocontroller.add_task(audiocontroller.preload(song))

@bridge.bridge_command(
name="pause", description=config.HELP_PAUSE_LONG, help=config.HELP_PAUSE_SHORT
name="pause",
description=config.HELP_PAUSE_LONG,
help=config.HELP_PAUSE_SHORT,
aliases=["resume"],
)
async def _pause(self, ctx: Context):
if not await utils.play_check(ctx):
return

if ctx.guild.voice_client is not None:
if ctx.guild.voice_client.is_paused():
return await self._resume(ctx)

elif ctx.guild.voice_client.is_playing():
ctx.guild.voice_client.pause()
return await ctx.send("Playback Paused :pause_button:")

await ctx.send("Nothing to pause.")
return
result = ctx.bot.audio_controllers[ctx.guild].pause()
await ctx.send(result.value)

@bridge.bridge_command(
name="queue",
Expand Down Expand Up @@ -262,21 +258,6 @@ async def _prev(self, ctx: Context):
else:
await ctx.send("No previous track.")

@bridge.bridge_command(
name="resume",
description=config.HELP_RESUME_LONG,
help=config.HELP_RESUME_SHORT,
)
async def _resume(self, ctx: Context):
if not await utils.play_check(ctx):
return

if ctx.guild.voice_client and ctx.guild.voice_client.is_paused():
ctx.guild.voice_client.resume()
await ctx.send("Resumed playback :arrow_forward:")
else:
await ctx.send("Playback is not paused.")

@bridge.bridge_command(
name="songinfo",
description=config.HELP_SONGINFO_LONG,
Expand Down

0 comments on commit 9d6e5f5

Please sign in to comment.