From a6ffce021529818a691b22fe1d01f715350faf71 Mon Sep 17 00:00:00 2001 From: cloudwithax Date: Sun, 17 Oct 2021 12:07:29 -0400 Subject: [PATCH] Added examples and updated README --- README.md | 5 +- examples/basic.py | 135 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 examples/basic.py diff --git a/README.md b/README.md index 853bad2..12f2119 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Pomice The modern [Lavalink](https://github.com/freyacodes/Lavalink) wrapper designed for [discord.py](https://github.com/Rapptz/discord.py) -![](https://img.shields.io/badge/license-GPL-2f2f2f) ![](https://img.shields.io/badge/python-3.8-2f2f2f) +![](https://img.shields.io/badge/license-GPL-2f2f2f) ![](https://img.shields.io/badge/python-3.8-2f2f2f) ![](https://img.shields.io/discord/899324069235810315) This library is heavily based off of/uses code from the following libraries: - [Wavelink](https://github.com/PythonistaGuild/Wavelink) @@ -23,6 +23,9 @@ pip install pomice pip install git+https://github.com/cloudwithax/pomice ``` +# Support +You can join our support server [here](https://discord.gg/r64qjTSHG8) + # Examples In-depth examples are located in the examples folder diff --git a/examples/basic.py b/examples/basic.py new file mode 100644 index 0000000..9f0b646 --- /dev/null +++ b/examples/basic.py @@ -0,0 +1,135 @@ +import pomice +import discord + +from discord.ext import commands + +class MyBot(commands.Bot): + + def __init__(self) -> None: + super().__init__(command_prefix='!', activity=discord.Activity(type=discord.ActivityType.listening, name='to music!')) + + self.add_cog(Music(self)) + + async def on_ready(self) -> None: + print("I'm online!") + await self.cogs["Music"].start_nodes() + + +class Music(commands.Cog): + + def __init__(self, bot) -> None: + self.bot = bot + + # In order to initialize a node, or really do anything in this library, you need to make a node pool + self.pomice = pomice.NodePool() + + async def start_nodes(self): + # You can pass in Spotify credentials to enable Spotify querying. + # If you do not pass in valid Spotify credentials, Spotify querying will not work + + await self.pomice.create_node(bot=self.bot, host='127.0.0.1', port='3030', password='youshallnotpass', identifier='MAIN') + print(f"Node is ready!") + + + + @commands.command(aliases=['connect']) + async def join(self, ctx: commands.Context, *, channel: discord.TextChannel = None) -> None: + + if not channel: + channel = getattr(ctx.author.voice, 'channel', None) + if not channel: + raise commands.CheckFailure('You must be in a voice channel to use this command' + 'without specifying the channel argument.') + + + # With the release of discord.py 1.7, you can now add a compatible VoiceProtocol class as a argument + # in channel.connect(). This library takes advantage of that and is how you initialize a player. + + await ctx.author.voice.channel.connect(cls=pomice.Player) + await ctx.send(f'Joined the voice channel `{channel}`') + + @commands.command(aliases=['dc', 'disconnect']) + async def leave(self, ctx: commands.Context): + + if not ctx.voice_client: + raise commands.CommandError('No player detected') + + player: pomice.Player = ctx.voice_client + + player.destroy() + + await ctx.send("Player has left the channel.") + + @commands.command(aliases=['p']) + async def play(self, ctx, *, search: str) -> None: + + # Checks if the players in the channel before we play anything + if not ctx.voice_client: + await ctx.invoke(self.join) + + + player: pomice.Player = ctx.voice_client + + # If you search a keyword, Pomice will automagically search the result using Youtube + # You can pass in "search_type=" as an argument to change the search type + # i.e: player.get_tracks("query", search_type=SearchType.ytmsearch) + # will search up any keyword results on Youtube Music + + results = player.get_tracks("query") + + if not results: + raise commands.CommandError('No results were found for that search term.') + + if isinstance(results, pomice.Playlist): + await player.play(track=results.tracks[0]) + else: + await player.play(track=results[0]) + + @commands.command() + async def pause(self, ctx): + + if not ctx.voice_client: + raise commands.CommandError('No player detected') + + player: pomice.Player = ctx.voice_client + + if player.is_paused: + await ctx.send("Player is already paused!") + + player.set_pause(pause=True) + + await ctx.send("Player has been paused") + + @commands.command() + async def resume(self, ctx): + + if not ctx.voice_client: + raise commands.CommandError('No player detected') + + player: pomice.Player = ctx.voice_client + + if not player.is_paused: + await ctx.send("Player is already playing!") + + player.set_pause(pause=False) + + await ctx.send("Player has been resumed") + + @commands.command() + async def stop(self, ctx): + + if not ctx.voice_client: + raise commands.CommandError('No player detected') + + player: pomice.Player = ctx.voice_client + + if not player.is_playing: + await ctx.send("Player is already stopped!") + + player.stop() + + await ctx.send("Player has been stopped") + + +bot = MyBot() +bot.run("token here") \ No newline at end of file