Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more basic functionality: pause, resume etc. #7

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@ def main():
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)

# Bot Setup
bot = commands.Bot(command_prefix=when_mentioned_or('?'), description=description)

@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}#{bot.user.discriminator}')

token = os.environ.get('BOT_TOKEN')
bot.add_cog(Music(bot))
bot.run(token)

if __name__ == '__main__':
main()

40 changes: 34 additions & 6 deletions yamb/cogs/music.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,49 @@ class Music(commands.Cog):
def __init__(self, bot):
self.bot = bot

@commands.command()
@commands.command(aliases=['p'])
async def play(self, ctx, *args):
"""Plays music from query"""

url = ' '.join(args)
async with ctx.typing():
player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True)
player.requester = ctx.author
ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)

await ctx.send('Now playing: {}'.format(player.title))
await self.np(ctx)

@commands.command()
async def np(self, ctx):
"""Display the currently playing track"""

if ctx.voice_client is None:
return await ctx.send("Not connected to a voice channel.")

title = ctx.voice_client.source.title
url = ctx.voice_client.source.url
requester = ctx.voice_client.source.requester

embed = discord.Embed()
embed.add_field(name="Now playing",
value=f'[{title}]({url})')
embed.add_field(name="Requested by",
value=requester.mention)

await ctx.send(embed=embed)

@commands.command()
async def pause(self, ctx):
"""Pauses the player"""
ctx.voice_client.pause()

@commands.command()
async def resume(self, ctx):
"""Resumes the player"""
ctx.voice_client.resume()


@commands.command(aliases=['vol'])
async def volume(self, ctx, volume: int):
"""Changes the player's volume"""

Expand All @@ -77,7 +108,7 @@ async def volume(self, ctx, volume: int):
ctx.voice_client.source.volume = volume / 100
await ctx.send("Changed volume to {}%".format(volume))

@commands.command()
@commands.command(aliases=['dc'])
async def stop(self, ctx):
"""Stops and disconnects the bot from voice"""

Expand All @@ -93,6 +124,3 @@ async def ensure_voice(self, ctx):
raise commands.CommandError("Author not connected to a voice channel.")
elif ctx.voice_client.is_playing():
ctx.voice_client.stop()