async def roles(self, ctx: Context, role_name: str = None): server = ctx.guild if not server: raise OutOfServer() roles = filter_public_roles(server.roles) if role_name: role = find_role(role_name, roles, raise_exception=True) if role in ctx.author.roles: await ctx.author.remove_roles(role) await ctx.author.send( embed=alert( f"Removed the role {role.name} at {ctx.guild}" ).get_embed() ) else: await ctx.author.add_roles(role) await ctx.author.send( embed=alert( f"Added the role {role.name} at {ctx.guild}" ).get_embed() ) else: await ctx.send(embed=list_roles(roles))
async def on_command_error(self, ctx: Context, error): if isinstance(error, CommandNotFound): await ctx.send(embed=alert(COMMAND_NOT_FOUND_RESPONSE).get_embed()) elif isinstance(error, BaseUserError): if error.private: await ctx.author.send(embed=alert(error.message).get_embed()) else: await ctx.send(embed=alert(error.message).get_embed()) else: log(f"{error} in {traceback.format_exc()}") await ctx.send(mention_admin(ctx, INTERNAL_ERROR_RESPONSE))
async def play(self, ctx: Context, *, query: str = None): if not ctx.guild: raise OutOfServer() if not ctx.author.voice: raise OutOfVoiceChannel() guild: Guild = ctx.guild clients = self.clients if guild.id not in clients: client: VoiceClient = await ctx.author.voice.channel.connect() clients[guild.id] = client else: client = clients[guild.id] if not query: if client.is_paused(): client.resume() await ctx.send(embed=alert("▶️ Resumed").get_embed()) return raise InvalidArgs("Tell me what to play!") await ctx.send(embed=alert(f"🔎 Searching for '{query}'").get_embed()) results = YoutubeSearch(query).to_dict() url = f"https://youtube.com{results[0]['url_suffix']}" title = results[0]["title"] player = await YTDLSource.from_url(url, loop=self.bot.loop) if results[0]["thumbnails"]: image = results[0]["thumbnails"][0] else: image = None await ctx.send(embed=Embed( f"Now playing {title}", description=url, colour=Colour.red(), image=image, ).get_embed()) if client.is_playing(): client.stop() client.play(player, after=lambda e: print("Player error: %s" % e) if e else None)
async def stop(self, ctx: Context): if not ctx.guild: raise OutOfServer() guild: Guild = ctx.guild clients = self.clients if guild.id in clients: client: VoiceClient = clients[guild.id] if client.is_paused() or client.is_playing(): client.stop() await ctx.send(embed=alert("⏹ Stoped").get_embed())
async def disconnect(self, ctx: Context): if not ctx.guild: raise OutOfServer() guild: Guild = ctx.guild clients = self.clients if guild.id in clients: client: VoiceClient = guild.voice_client del clients[guild.id] await client.disconnect() await ctx.send(embed=alert("❌ Disconnected").get_embed())
async def on_reaction_add(self, reaction: Reaction, user: User): message: Message = reaction.message if user == self.bot.user: return if message.id in self.games: g = self.games[message.id] e = reaction.emoji if e == UP: g.up() if e == DOWN: g.down() if e == RIGHT: g.right() if e == LEFT: g.left() if g.win(): await message.edit(content=str(g.board), embed=alert("VICTORY!").get_embed()) await message.clear_reactions() del self.games[message.id] else: await message.edit(content=str(g.board), delete_after=TIMEOUT)