def sync_song_conclusion(self, ctx, error=None): if len(self.parent.guilds[ctx.guild.id].song_queue.queue) == 0: self.parent.guilds[ctx.guild.id].now_playing = None if error is not None: self.parent.log.error(str(error)) function = asyncio.run_coroutine_threadsafe( self.parent.send_error_message(ctx, str(error)), self.bot.loop) try: function.result() except Exception as e: self.parent.log.error(e) function = asyncio.run_coroutine_threadsafe( self.parent.clear_presence(ctx), self.bot.loop) try: function.result() except Exception as e: self.parent.log.error(traceback.format_exc()) self.parent.log.error(logging_manager.debug_info(str(e))) function = asyncio.run_coroutine_threadsafe(self.empty_channel(ctx), self.bot.loop) try: function.result() except Exception as e: self.parent.log.error(traceback.print_exc()) self.parent.log.error(logging_manager.debug_info(str(e))) function = asyncio.run_coroutine_threadsafe(self.pre_player(ctx), self.bot.loop) try: function.result() except Exception as e: self.parent.log.error(logging_manager.debug_info(str(e)))
async def join_check(self, ctx, url): if url is None: embed = discord.Embed( title="You need to enter something to play.", url="https://d.chulte.de", color=0x00FFCC ) await ctx.send(embed=embed) return False self.dictionary = self.dictionary try: if self.dictionary[ctx.guild.id]["voice_channel"] is None: self.dictionary[ctx.guild.id]["voice_channel"] = ctx.author.voice.channel except Exception as e: self.log.warning(logging_manager.debug_info("channel_join " + str(e))) embed = discord.Embed(title="You need to be in a channel.", color=0x00FFCC, url="https://d.chulte.de") self.dictionary[ctx.guild.id]["voice_channel"] = None await ctx.send(embed=embed) return False try: if ctx.me.voice.channel != ctx.author.voice.channel: embed = discord.Embed( title="You need to be in the same channel as the bot.", color=0x00FFCC, url="https://d.chulte.de" ) await ctx.send(embed=embed) return False except AttributeError: pass return True
async def spotify_playlist(self, playlist_url): token = await self.request_token() playlist_id = playlist_url.split("playlist/")[1] if "?" in playlist_id: playlist_id = playlist_id.split("?si")[0] url = "https://api.spotify.com/v1/playlists/" + playlist_id + "/tracks?limit=100&offset=0" header = {"Authorization": "Bearer " + token} result = await self.request_get(url, header) js = JSON.loads(result) t_list = [] more = True while more is True: try: for track in js["items"]: t_list.append( track["track"]["album"]["artists"][0]["name"] + " - " + track["track"]["name"]) if js["next"] is None: more = False else: url = js["next"] result = await self.request_get(url, header) js = JSON.loads(result) except KeyError as key_error: self.log.warning( logging_manager.debug_info(str(key_error) + " " + str(js))) if hasattr(js, "error"): self.token = "" more = False return t_list
async def send(self): if self.song is None: return if not self.no_embed_mode: if self.calculate_recurrences(): embed = discord.Embed(title=self.title, color=0x00FFCC, url=self.song.link) embed.set_author(name="Currently Playing:") embed.add_field(name="░░░░░░░░░░░░░░░░░░░░░░░░░", value=" 0%") await self.message.edit(embed=embed) asyncio.ensure_future(self.update()) else: embed = discord.Embed(title=self.title, color=0x00FFCC, url=self.song.link) embed.set_author(name="Currently Playing:") await self.message.edit(embed=embed) await self.message.add_reaction( "\N{BLACK RIGHT-POINTING TRIANGLE WITH DOUBLE VERTICAL BAR}" # pause play ) await self.message.add_reaction( "\N{BLACK RIGHT-POINTING DOUBLE TRIANGLE}" # fast forward ) await self.reaction_waiter() else: try: await self.message.edit(content=self.title) except discord.errors.NotFound as p: self.log.warning(logging_manager.debug_info(p))
async def delete_message(message: discord.Message, delay: int = None): try: await message.delete(delay=delay) except (discord.HTTPException, discord.Forbidden) as e: logging_manager.LoggingManager().warning( logging_manager.debug_info(e) )
async def rename(self, ctx, *, name: str): """ Renames the bot. :param ctx: :param name: :return: """ try: if ctx.guild.me.guild_permissions.administrator is False: await self.send_error_message( ctx, "You need to be an Administrator to execute this action.", ) return except AttributeError as attribute_error: self.log.error( logging_manager.debug_info( "AttributeError " + str(attribute_error) ) ) try: if len(name) > 32: await self.send_error_message( ctx, "Name too long. 32 chars is the limit." ) return bot_user = ctx.guild.me await bot_user.edit(nick=name) await self.send_embed_message( ctx, "Rename to **" + name + "** successful." ) except Exception as raised_exception: await self.send_error_message( ctx, "An Error occurred: " + str(raised_exception) )
async def rename(self, ctx, *, name: str): try: if ctx.guild.me.guild_permissions.administrator is False: embed = discord.Embed( title="You need to be an Administrator to execute this action.", color=0x00FFCC, url="https://d.chulte.de", ) await ctx.send(embed=embed) return except AttributeError as ae: self.log.error(logging_manager.debug_info("AttributeError " + str(ae))) try: if len(name) > 32: embed = discord.Embed( title="Name too long. 32 chars is the limit.", url="https://d.chulte.de", color=0x00FFCC ) await ctx.send(embed=embed) me = ctx.guild.me await me.edit(nick=name) embed = discord.Embed( title="Rename to **" + name + "** successful.", url="https://d.chulte.de", color=0x00FFCC ) await ctx.send(embed=embed) except Exception as e: embed = discord.Embed(title="An Error occurred: " + str(e), url="https://d.chulte.de", color=0x00FFCC) await ctx.send(embed=embed)
async def join_channel(self, ctx): if self.dictionary[ctx.guild.id]["voice_client"] is None: try: if ( ctx.author.voice.channel.user_limit <= len(ctx.author.voice.channel.members) and ctx.author.voice.channel.user_limit != 0 ): if ctx.guild.me.guild_permissions.administrator is True: self.dictionary[ctx.guild.id]["voice_client"] = await ctx.author.voice.channel.connect( timeout=60, reconnect=True ) else: embed = discord.Embed( title="Error while joining your channel. :frowning: (1)", url="https://d.chulte.de", color=0x00FFCC, ) await ctx.send(embed=embed) return False else: self.dictionary[ctx.guild.id]["voice_client"] = await ctx.author.voice.channel.connect( timeout=60, reconnect=True ) except (TimeoutError, discord.HTTPException, discord.ClientException, discord.DiscordException) as e: self.log.warning(logging_manager.debug_info("channel_join " + str(e))) self.dictionary[ctx.guild.id]["voice_channel"] = None embed = discord.Embed( title="Error while joining your channel. :frowning: (2)", url="https://d.chulte.de", color=0x00FFCC ) await ctx.send(embed=embed) return False return True
async def join_channel(self, ctx): if self.parent.guilds[ctx.guild.id].voice_client is None: try: if (ctx.author.voice.channel.user_limit <= len( ctx.author.voice.channel.members) and ctx.author.voice.channel.user_limit != 0): if ctx.guild.me.guild_permissions.administrator is False: await self.parent.send_embed_message( ctx, "Error while joining your channel. :frowning: (1)", ) return False else: self.parent.guilds[ ctx.guild. id].voice_client = await bot.node_controller.NodeVoiceClient.NodeVoiceChannel.from_channel( ctx.author.voice.channel, self.parent.node_controller).connect() except ( TimeoutError, discord.HTTPException, discord.ClientException, discord.DiscordException, ) as e: self.parent.log.warning( logging_manager.debug_info("channel_join " + str(e))) self.parent.guilds[ctx.guild.id].voice_channel = None await self.parent.send_embed_message( ctx, "Error while joining your channel. :frowning: (2)") return False except NoNodeReadyException as nn: await self.parent.send_error_message(ctx, str(nn)) return False return True
async def _update(self, ctx: commands.Context) -> None: if self._stop is True: return try: voice_client = self.parent.guilds[ctx.guild.id].voice_client if not voice_client.is_paused(): now_time = round(self.bytes_read / 192000) finish_second = int( self.parent.guilds[ctx.guild.id].now_playing.duration) description = ( "`" + time.strftime("%H:%M:%S", time.gmtime(now_time)) + " / " + time.strftime( "%H:%M:%S", time.gmtime(self.parent.guilds[ ctx.guild.id].now_playing.duration), ) + "`") percentage = int((now_time / finish_second) * 100) if percentage > 100: return count = percentage / 4 hashes = "" while count > 0: hashes += self.parent.guilds[ctx.guild.id].full count -= 1 while len(hashes) < 25: hashes += self.parent.guilds[ctx.guild.id].empty hashes += " " + str(percentage) + "%" embed2 = discord.Embed(title=self._title, color=0x00FFCC, url=self._song.link) embed2.set_author( name="Currently Playing:", icon_url="https://i.imgur.com/dbS6H3k.gif", ) embed2.add_field(name=hashes, value=description) try: await self.message.edit(embed=embed2) except (discord.NotFound, TypeError): return else: if self._stop is False: while voice_client.is_paused(): await asyncio.sleep(0.1) await self._update(ctx=ctx) except ( TypeError, AttributeError, discord.HTTPException, RecursionError, ) as thrown_exception: self.parent.log.warning( logging_manager.debug_info(thrown_exception)) return await asyncio.sleep(5) if self._stop is False: await self._update(ctx=ctx)
def song_conclusion(self, ctx): self.dictionary[ctx.guild.id]["now_playing_song"] = None function = asyncio.run_coroutine_threadsafe(self.clear_presence(ctx), self.bot.loop) try: function.result() except Exception as e: self.log.error(logging_manager.debug_info(str(e))) function = asyncio.run_coroutine_threadsafe(self.empty_channel(ctx), self.bot.loop) try: function.result() except Exception as e: self.log.error(logging_manager.debug_info(str(e))) function = asyncio.run_coroutine_threadsafe(self.pre_player(ctx), self.bot.loop) try: function.result() except Exception as e: self.log.error(logging_manager.debug_info(str(e)))
async def add_to_queue(self, url, ctx, first_index_push=False, playskip=False, shuffle=False): if playskip: self.parent.guilds[ctx.guild.id].song_queue = Queue() songs: list = await self.extract_infos(url=url, ctx=ctx) for __song in songs: __song: Song __song.guild_id = ctx.guild.id if len(songs) != 0: song_1: Song = songs.__getitem__(0) if isinstance(song_1, Error): await self.parent.send_error_message(ctx=ctx, message=songs[0].reason) return if len(songs) > 1: if shuffle: random.shuffle(songs) self.parent.guilds[ctx.guild.id].song_queue.queue.extend(songs) await self.parent.send_embed_message( ctx=ctx, message=":asterisk: Added " + str(len(songs)) + " Tracks to Queue. :asterisk:", ) elif len(songs) == 1: if first_index_push: self.parent.guilds[ctx.guild.id].song_queue.queue.extendleft( songs) else: self.parent.guilds[ctx.guild.id].song_queue.queue.extend(songs) title = "" if songs[0].title is not None: title = songs[0].title else: try: title = songs[0].link except AttributeError: pass if self.parent.guilds[ctx.guild.id].voice_client.is_playing(): if not playskip: await self.parent.send_embed_message( ctx, ":asterisk: Added **" + title + "** to Queue.") try: if playskip: if self.parent.guilds[ctx.guild.id].voice_client is not None: if self.parent.guilds[ ctx.guild.id].voice_client.is_playing(): self.parent.guilds[ctx.guild.id].voice_client.stop() if not self.parent.guilds[ctx.guild.id].voice_client.is_playing(): await self.pre_player(ctx) except Exception as e: self.parent.log.error(traceback.format_exc()) self.parent.log.error(logging_manager.debug_info(str(e)))
async def preload_song(self, ctx: commands.Context) -> None: """ Preload of the next song. :param ctx: :return: """ try: if self.guilds[ctx.guild.id].song_queue.qsize() == 0: return i = 0 for item in self.guilds[ctx.guild.id].song_queue.queue: item: Song if item.stream: continue backup_title: str = str(item.title) if item.link is not None: try: type_of_source = Url.determine_source(item.link) if type_of_source == Url.youtube_url: youtube_dict = await self.parent.youtube.youtube_url( item.link, ctx.guild.id) elif type_of_source == Url.soundcloud_track: youtube_dict = await self.parent.soundcloud.soundcloud_track( item.link) else: continue except BasicError: self.parent.log( logging_manager.debug_info(traceback.format_exc())) continue youtube_dict.user = item.user else: if item.title: continue try: youtube_dict = await self._search_song(ctx, item) except BasicError: continue youtube_dict.user = item.user j: int = 0 for _song in self.guilds[ctx.guild.id].song_queue.queue: _song: Song if _song.title != backup_title: j += 1 continue self.guilds[ ctx.guild.id].song_queue.queue[j] = Song.copy_song( youtube_dict, self.guilds[ctx.guild.id].song_queue.queue[j], ) break break i += 1 except IndexError: pass except AttributeError: traceback.print_exc()
def debug(self, msg): if "youtube:search" in msg and "query" in msg: log.debug( logging_manager.debug_info( "[YouTube Search] Searched Term: '" + msg.split('"')[1].split('"')[-1] + "'" ) )
async def info(self, ctx): """ Shows song info. :param ctx: :return: """ self.guilds = self.guilds if self.guilds[ctx.guild.id].now_playing is None: embed = discord.Embed( title="Information", description="Nothing is playing right now.", color=0x00FFCC, url="https://d.chulte.de", ) await ctx.send(embed=embed) return try: embed = discord.Embed( title="Information", color=0x00FFCC, url="https://d.chulte.de" ) song: Song = self.guilds[ctx.guild.id].now_playing embed.add_field( name="Basic Information", inline=False, value=( f"**Name**: `{song.title}`\n" + f"**Url**: `{song.link}`\n" + f"**Duration**: `{datetime.timedelta(seconds=song.duration)}`\n" + f"**User**: `{song.user}`\n" + f"**Term**: `{song.term}`\n" ), ) embed.add_field( name="Stream Information", inline=False, value=( f"**Successful**: `{not song.error.error}`\n" + f"**Codec**: `{song.codec}\n`" + f"**Bitrate**: `{song.abr} kb/s`" ), ) if self.guilds[ctx.guild.id].now_playing.image is not None: embed.set_thumbnail( url=self.guilds[ctx.guild.id].now_playing.image ) await ctx.send(embed=embed) except (KeyError, TypeError) as e: self.log.warning(logging_manager.debug_info(str(e))) embed = discord.Embed( title="Error", description=Errors.info_check, url="https://d.chulte.de", color=0x00FFCC, ) await ctx.send(embed=embed)
async def on_command_error(ctx, error): if "not found" in str(error): embed = discord.Embed(title=str(error), color=0x00FFCC, url="https://github.com/tooxo/Geiler-Musik-Bot/issues") await ctx.send(embed=embed) elif "Invalid Data" in str(error): embed = discord.Embed( title="Error while playback. Try again.", color=0x00FFCC, url="https://github.com/tooxo/Geiler-Musik-Bot/issues", ) await ctx.send(embed=embed) else: log.error(logging_manager.debug_info(str(error)))
async def preload_album_art(self, ctx): try: song_title = self.dictionary[ctx.guild.id]["now_playing_song"]["title"] search_term = self.dictionary[ctx.guild.id]["now_playing_song"]["term"] if song_title == "_": self.dictionary[ctx.guild.id]["now_playing_song"]["image_url"] = await self.lastfm.get_album_art( search_term, search_term ) else: self.dictionary[ctx.guild.id]["now_playing_song"]["image_url"] = await self.lastfm.get_album_art( song_title, search_term ) except (IndexError, TypeError, KeyError, NameError) as e: self.log.warning(logging_manager.debug_info(str(e)))
def check_remove(reaction: discord.Reaction, user: discord.Member): if self.discord_music.bot.user.id != user.id: if same_channel_check(user=user): if (reaction.emoji == "\N{BLACK RIGHT-POINTING TRIANGLE WITH DOUBLE VERTICAL BAR}" ): try: if self.voice_client.is_paused(): self.voice_client.resume() else: self.voice_client.pause() except AttributeError as ae: self.log.warning(logging_manager.debug_info(ae)) return False
async def info(self, ctx): self.dictionary = self.dictionary if self.dictionary[ctx.guild.id]["now_playing_song"] is None: embed = discord.Embed( title="Information", description="Nothing is playing right now.", color=0x00FFCC, url="https://d.chulte.de", ) if "image_url" in self.dictionary[ctx.guild.id]["now_playing_song"]: if self.dictionary[ctx]["now_playing_song"]["image_url"] is not "": embed.set_thumbnail(url=self.dictionary[ctx.guild.id]["now_playing_song"]["image_url"]) await ctx.send(embed=embed) return try: embed = discord.Embed( title="Information", description="Name: " + str(self.dictionary[ctx.guild.id]["now_playing_song"]["title"]) + "\nStreamed from: " + str(self.dictionary[ctx.guild.id]["now_playing_song"]["link"]) + "\nDuration: " + str(self.dictionary[ctx.guild.id]["now_playing_song"]["duration"]) + "\nRequested by: <@!" + str(self.dictionary[ctx.guild.id]["now_playing_song"]["user"].id) + ">\nLoaded in: " + str(round(self.dictionary[ctx.guild.id]["now_playing_song"]["loadtime"], 2)) + " sec." + "\nSearched Term: " + str(self.dictionary[ctx.guild.id]["now_playing_song"]["term"]), color=0x00FFCC, url="https://d.chulte.de", ) await ctx.send(embed=embed) except (KeyError, TypeError) as e: self.log.warning(logging_manager.debug_info(str(e))) embed = discord.Embed( title="Error", description="An error occurred while checking info.", url="https://d.chulte.de", color=0x00FFCC, ) await ctx.send(embed=embed)
async def delete_message( message: discord.Message, delay: int = None ) -> None: """ Deletes a message with an optional delay @param message: @param delay: @return: """ try: await message.delete(delay=delay) except ( discord.HTTPException, discord.Forbidden, discord.NotFound, ) as raised_exceptions: logging_manager.LoggingManager().debug( logging_manager.debug_info(raised_exceptions) )
async def join_channel(self, ctx: commands.Context) -> bool: """ Joins a channel @param ctx: @return: returns if the join process was successful """ if self.guilds[ctx.guild.id].voice_client is None: try: if (ctx.author.voice.channel.user_limit <= len( ctx.author.voice.channel.members) and ctx.author.voice.channel.user_limit != 0): if ctx.guild.me.guild_permissions.administrator is False: await self.parent.send_embed_message( ctx, "Error while joining your channel. :frowning: (1)", ) return False else: self.guilds[ ctx.guild. id].voice_client = await NodeVoiceChannel.from_channel( ctx.author.voice.channel, self.parent.node_controller).connect() self.guilds[ ctx.guild.id].now_playing_message = NowPlayingMessage( self.parent) except ( TimeoutError, discord.HTTPException, discord.ClientException, ) as discord_error: self.parent.log.warning( logging_manager.debug_info("channel_join " + str(discord_error))) self.guilds[ctx.guild.id].voice_channel = None await self.parent.send_embed_message( ctx, "Error while joining your channel. :frowning: (2)") return False except NoNodeReadyException as no_node_ready_exception: await self.parent.send_error_message( ctx, str(no_node_ready_exception)) return False return True
async def on_command_error(ctx: commands.Context, error: Type[Exception]) -> None: """ Called when an error occurred while processing a command @param ctx: @param error: @return: """ if "not found" in str(error): print(error) elif "Invalid Data" in str(error): await DiscordBot.send_error_message( ctx=ctx, message="Error while playback. Try again.") elif isinstance(error, NotSameChannel): await DiscordBot.send_error_message( ctx=ctx, message="You need to be in the same channel as the bot.") elif isinstance(error, UserNotConnected): await DiscordBot.send_error_message( ctx=ctx, message="You need to be in a channel.") elif isinstance(error, NothingPlaying): await DiscordBot.send_error_message(ctx=ctx, message="Nothing is playing.") elif isinstance(error, BotNotConnected): await DiscordBot.send_error_message(ctx=ctx, message="The bot isn't connected.") elif isinstance(error, NoNodeReadyException): await DiscordBot.send_error_message( ctx=ctx, message="Our backend seems to be down right now, " "try again in a few minutes.", ) elif isinstance(error, discord.ext.commands.MissingRequiredArgument): # send help await CLIENT.cogs["Help"].help_command(ctx, str(ctx.command), implicit=False) else: LOG.error(logging_manager.debug_info(str(error))) if hasattr(error, "original"): traceback.print_tb(error.original.__traceback__) else: traceback.print_tb(error.__traceback__)
def __init__(self): self.log = logging_manager.LoggingManager() self.log.debug("[Startup]: Initializing Mongo Module . . .") try: self.host = os.environ["MONGODB_URI"] except KeyError: self.host = "" self.client = motor.motor_asyncio.AsyncIOMotorClient(self.host) try: self.db = eval("self.client." + os.environ["MONGODB_USER"]) except Exception as e: self.log.error(logging_manager.debug_info(str(e))) self.db = "" self.collection = self.db.connectiontime self.most_played_collection = self.db.most_played_collection alternative_host = "mongodb://database:27017" alternative_client = motor.motor_asyncio.AsyncIOMotorClient( alternative_host) self.alternative_db = alternative_client.discordbot
async def update_stats(client): while not client.bot.is_closed(): try: await client.post_guild_count() self.log.debug( "[SERVER COUNT] Posted server count ({})".format( client.guild_count() ) ) await self.bot.change_presence( activity=discord.Activity( type=discord.ActivityType.listening, name=".help on {} servers".format( client.guild_count() ), ) ) except Exception as e: self.log.warning(logging_manager.debug_info(e)) await asyncio.sleep(1800)
async def pause(self, ctx): try: if ctx.me.voice.channel != ctx.author.voice.channel: embed = discord.Embed( title="You need to be in the same channel as the bot.", color=0x00FFCC, url="https://d.chulte.de" ) await ctx.send(embed=embed) return except AttributeError: pass try: if not hasattr(ctx.author.voice, "channel"): embed = discord.Embed(title="You need to be in a channel.", color=0x00FFCC, url="https://d.chulte.de") await ctx.send(embed=embed) return except AttributeError: pass if self.dictionary[ctx.guild.id]["voice_channel"] is None: embed = discord.Embed(title="The bot isn't connected.", color=0x00FFCC, url="https://d.chulte.de") await ctx.send(embed=embed) return self.dictionary = self.dictionary if self.dictionary[ctx.guild.id]["now_playing_song"]["is_paused"] is True: embed = discord.Embed(title="Already Paused.", color=0x00FFCC, url="https://d.chulte.de") await ctx.send(embed=embed) if self.dictionary[ctx.guild.id]["voice_client"] is not None: try: self.dictionary[ctx.guild.id]["voice_client"].pause() embed = discord.Embed(title="Paused! ⏸", color=0x00FFCC, url="https://d.chulte.de") message = await ctx.send(embed=embed) self.dictionary[ctx.guild.id]["now_playing_song"]["pause_time"] = int(time.time()) self.dictionary[ctx.guild.id]["now_playing_song"]["is_paused"] = True await asyncio.sleep(5) await message.delete() await ctx.message.delete() except Exception as e: self.log.error(logging_manager.debug_info(str(e))) embed = discord.Embed( title=":thinking: Nothing is playing... :thinking:", color=0x00FFCC, url="https://d.chulte.de" ) await ctx.send(embed=embed)
async def player(self, ctx: commands.Context, small_dict: Song) -> None: """ Plays a Song. @param ctx: @param small_dict: @return: """ try: self.guilds[ctx.guild.id].unlock_queue() if self.guilds[ctx.guild.id].voice_client is None: return try: small_dict.guild_id = ctx.guild.id await self.guilds[ctx.guild.id].voice_client.play(small_dict) self.guilds[ctx.guild.id].voice_client.set_after( self.song_conclusion, ctx) except discord.ClientException: if ctx.guild.voice_client is None: if self.guilds[ctx.guild.id].voice_channel is not None: self.guilds[ ctx.guild.id].voice_client = await self.guilds[ ctx.guild.id ].voice_channel.connect(timeout=10, reconnect=True) small_dict.guild_id = ctx.guild.id await self.guilds[ctx.guild.id ].voice_client.play(small_dict, ) self.guilds[ctx.guild.id].voice_client.set_after( self.song_conclusion, ctx) self.guilds[ctx.guild.id].now_playing = small_dict if self.guilds[ctx.guild.id].announce: if not self.guilds[ctx.guild.id].now_playing_message: self.guilds[ ctx.guild.id].now_playing_message = NowPlayingMessage( self.parent) await self.guilds[ctx.guild.id ].now_playing_message.new_song(ctx=ctx) except (Exception, discord.ClientException) as discord_exception: self.parent.log.debug( logging_manager.debug_info( traceback.format_exc(discord_exception)))
async def _update_stats(client) -> None: last_count = 0 while not client.bot.is_closed(): try: if client.guild_count() != last_count: await client.post_guild_count() self.log.debug( f"[SERVER COUNT] Posted server count " f"({client.guild_count()})" ) await self.bot.change_presence( activity=discord.Activity( type=discord.ActivityType.listening, name=".help on {} servers".format( client.guild_count() ), ) ) last_count = client.guild_count() except Exception as raised_exception: self.log.warning( logging_manager.debug_info(str(raised_exception)) ) await asyncio.sleep(1800)
async def spotify_playlist(self, playlist_url): token = await self.request_token() playlist = SpotifyType(playlist_url) if not playlist.valid: return [] url = ( "https://api.spotify.com/v1/playlists/" + playlist.id + "/tracks?limit=100&offset=0" ) header = {"Authorization": "Bearer " + token} result = await self.request_get(url, header) js = json.loads(result) t_list = [] more = True while more is True: try: for track in js["items"]: if track["is_local"]: try: t_list.append( SpotifySong( title=track["track"]["artists"][0]["name"] + " - " + track["track"]["name"], image_url=None, song_name=track["track"]["name"], artist=track["track"]["artists"][0]["name"], ) ) except (IndexError, KeyError): # Probably invalid local file continue else: t_list.append( SpotifySong( title=track["track"]["album"]["artists"][0][ "name" ] + " - " + track["track"]["name"], image_url=track["track"]["album"]["images"][0][ "url" ], song_name=track["track"]["name"], artist=track["track"]["artists"][0]["name"], ) ) if js["next"] is None: more = False else: url = js["next"] result = await self.request_get(url, header) js = json.loads(result) except KeyError as key_error: self.log.warning( logging_manager.debug_info(str(key_error) + " " + str(js)) ) if "error" in js: self.token = "" more = False return t_list
async def info(self, ctx: commands.Context) -> None: """ Shows song info. :param ctx: :return: """ use_embeds = environ.get("USE_EMBEDS", "True") == "True" if self.guilds[ctx.guild.id].now_playing is None: await self.send_error_message(ctx, "Nothing is playing.") return try: song: Song = self.guilds[ctx.guild.id].now_playing if not use_embeds: await self._send_message( use_citation=True, content=f"**Basic Information**\n" f"Name: `{song.title}`\n" f"Url: `{song.link}`\n" f"Duration: " f"`{datetime.timedelta(seconds=song.duration)}`\n" f"User: `{song.user}`\n" f"Term: `{song.term}`\n\n" f"**Stream Information**\n" f"Successful: `{True}`\n" f"Codec: `{song.codec}`\n" f"Bitrate: `{song.abr} kb/s`", ctx=ctx, ) else: embed = discord.Embed( title="Information", color=0x00FFCC, url="https://d.chulte.de", ) embed.add_field( name="Basic Information", inline=False, value=( f"**Name**: `{song.title}`\n" + f"**Url**: `{song.link}`\n" + f"**Duration**: " f"`{datetime.timedelta(seconds=song.duration)}`\n" + f"**User**: `{song.user}`\n" + f"**Term**: `{song.term}`\n" ), ) embed.add_field( name="Stream Information", inline=False, value=( f"**Successful**: `{True}`\n" + f"**Codec**: `{song.codec}\n`" + f"**Bitrate**: `{song.abr} kb/s`" ), ) if self.guilds[ctx.guild.id].now_playing.image is not None: embed.set_thumbnail( url=self.guilds[ctx.guild.id].now_playing.image ) await ctx.send(embed=embed) except (KeyError, TypeError) as raised_exception: self.log.warning(logging_manager.debug_info(str(raised_exception))) await self.send_error_message(ctx, Errors.info_check)
async def spotify_playlist(self, playlist_url) -> List[Song]: """ Extracts all songs from a spotify playlist. @param playlist_url: Spotify Playlist Url @return: List of Songs @rtype: List[Song] """ token = await self._request_token() playlist = SpotifyType(playlist_url) if not playlist.valid: raise PlaylistExtractionException() url = ("https://api.spotify.com/v1/playlists/" + playlist.id + "/tracks?limit=100&offset=0") header = {"Authorization": "Bearer " + token} result = await self._request_get(url, header) json_result = json.loads(result) if "error" in json_result: raise PlaylistExtractionException() t_list = [] more = True while more is True: try: for track in json_result["items"]: if track["is_local"]: try: t_list.append( Song( title=track["track"]["artists"][0]["name"] + " - " + track["track"]["name"], image_url=None, song_name=track["track"]["name"], artist=track["track"]["artists"][0] ["name"], )) except ( IndexError, KeyError, TypeError, ): # pragma: no cover # Probably invalid local file continue else: try: t_list.append( Song( title=track["track"]["album"]["artists"][0] ["name"] + " - " + track["track"]["name"], image_url=track["track"]["album"]["images"] [0]["url"], song_name=track["track"]["name"], artist=track["track"]["artists"][0] ["name"], )) except ( IndexError, KeyError, TypeError, ): # catch preemtively to prevent # no-response pass if json_result["next"] is None: more = False else: url = json_result["next"] result = await self._request_get(url, header) json_result = json.loads(result) except KeyError as key_error: self.log.warning( logging_manager.debug_info( str(key_error) + " " + str(json_result))) if "error" in json_result: self.token = "" more = False if not t_list: raise PlaylistExtractionException() return t_list