Skip to content

Commit

Permalink
bot is somewhat fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
mytja committed Jun 4, 2024
1 parent 7426a0b commit 5bbd04e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 39 deletions.
70 changes: 38 additions & 32 deletions commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import List

import discord
from discord.ext import commands
import asyncio
Expand Down Expand Up @@ -38,7 +40,7 @@ async def update():
elif not server.voiceConnection.is_playing():
''' Track Completed '''
print(server.queueIndex, len(server.queue))
if server.queueIndex >= len(server.queue)-1:
if server.queueIndex + 1 >= len(server.queue):
await server.disconnect()
await Embed().channel_leave(server.context)
continue
Expand All @@ -63,7 +65,7 @@ async def update():
continue
''' Track Playback '''
track = server.queue[server.queueIndex]
print(f"Playing track {track}")
#print(f"Playing track {track}")
url = track["requested_formats"][-1]["url"]
print(f"Fetched URL {url}")
try:
Expand All @@ -73,7 +75,7 @@ async def update():
Commands.listenUpdates(), Commands.bot.loop
),
)
print(f"Played track {track}")
#print(f"Played track {track}")
''' Run mainloop after playback completion. '''
except:
try:
Expand Down Expand Up @@ -105,26 +107,10 @@ async def update():
)


class Commands(commands.Cog):
""" Static Members """
bot = None
recognisedServers: list = []

@staticmethod
async def listenUpdates():
await Lifecycle.update()

def __init__(self, bot):
self.bot = bot
self.embed = Embed()
self.youtubeMusic = YouTubeMusic()


class Server:
def __init__(self, context, serverId, textChannel, voiceChannel):
def __init__(self, context, serverId, voiceChannel):
self.context = context
self.serverId = serverId
self.textChannel = textChannel
self.voiceChannel = voiceChannel
self.voiceConnection = None
self.queueIndex = -1
Expand All @@ -142,6 +128,9 @@ async def disconnect(self):
self.queueIndex = -1
self.modifiedQueueIndex = None

def change_context(self, context):
self.context = context

def get_latency(self) -> float:
return self.voiceConnection.average_latency

Expand All @@ -158,28 +147,45 @@ def stop(self):
async def get(context, connect: bool = False):
asyncio.ensure_future(context.message.add_reaction('👀'))
for server in Commands.recognisedServers:
if server.serverId == context.message.guild.id:
if context.author.voice:
server.voiceChannel = context.author.voice.channel
if server.voiceConnection:
await server.voiceConnection.move_to(server.voiceChannel)
elif connect:
if server.serverId != context.message.guild.id:
continue
if context.author.voice:
server.voiceChannel = context.author.voice.channel
if server.voiceConnection:
await server.voiceConnection.move_to(server.voiceChannel)
elif connect:
try:
await server.connect()
if connect and not server.voiceConnection:
return None
server.textChannel = Commands.bot.get_channel(context.message.channel.id)
return server
except Exception as e:
continue
if connect and not server.voiceConnection:
return None
server.change_context(context)
return server
serverId = context.message.guild.id
textChannel = Commands.bot.get_channel(context.message.channel.id)
if context.author.voice is None:
return None
voiceChannel = context.author.voice.channel
Commands.recognisedServers.append(
Server(
context,
serverId,
textChannel,
voiceChannel,
)
)
return Commands.recognisedServers[-1]


class Commands(commands.Cog):
""" Static Members """
bot = None
recognisedServers: List[Server] = []

@staticmethod
async def listenUpdates():
await Lifecycle.update()

def __init__(self, bot):
self.bot = bot
self.embed = Embed()
self.youtubeMusic = YouTubeMusic()
2 changes: 1 addition & 1 deletion commands/playback.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def changeChannel(self, ctx):
await self.embed.exception(
ctx,
'Internal Error',
'Oops, this command was recently removed. You don\'t to setup a channel before playback, just hop into a voice channel & get started with music playback. ℹ',
'Oops, this command was recently removed. You don\'t need to setup a channel before playback, just hop into a voice channel & get started with music playback. ℹ',
'❌'
)
return None
Expand Down
2 changes: 1 addition & 1 deletion constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ class Constants:
''' Static Members '''
version = 'beta-1.0.0'
status = '🎵 Watching for -help!'
description = 'Hello! 👋\nI am Harmonoid Music Bot.\nI can play music for you & get lyrics. 🎉\nI play music from both YouTube music & YouTube unlike other bots.\nYou may join our discord server from the link below to provide feedback or just chill with us.\n\nThankyou\nHarmonoid developers.'
description = 'Hello! 👋\nI am Harmonoid Music Bot.\nI can play music for you & get lyrics. 🎉\nI play music from both YouTube music & YouTube unlike other bots.\nYou may join our discord server from the link below to provide feedback or just chill with us.\n\nThank you\nHarmonoid developers.'
2 changes: 1 addition & 1 deletion scripts/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async def download(self, videoName: str) -> dict:
videoId = self.__getVideoId(videoName)
with yt_dlp.YoutubeDL() as ytd:
video = ytd.extract_info(f'https://youtu.be/{videoId}', download=False)
print(video)
#print(video)
return video

def __getVideoId(self, videoLink: str) -> str:
Expand Down
8 changes: 4 additions & 4 deletions source/embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ async def addedToQueue(self, context, track):
await self.__createEmbed(
context,
'Added To Queue',
f'**[{video["title"]}]({video["link"]})**',
f'**[{video["title"]}](https://youtu.be/{video["id"]})**',
video['thumbnails'][-1]['url'],
[
EmbedField('Channel', video['channel']['name'], False),
EmbedField('Duration', Method.formatDuration(video["duration"]["secondsText"]), True),
EmbedField('Channel', video['uploader'], False),
EmbedField('Duration', video["duration_string"], True),
],
'🎶',
True,
Expand Down Expand Up @@ -162,7 +162,7 @@ async def queue(self, context, queue, queueIndex):
if 'trackName' in query.keys():
queueString += f' {index + 1}. {query["trackName"]} - _{", ".join(query["trackArtistNames"])}_\n {Method.formatDuration(query["trackDuration"])}\n'
else:
queueString += f' {index + 1}. {query["title"]} - _{query["channel"]["name"]}_\n {Method.formatDuration(query["duration"]["secondsText"])}\n'
queueString += f' {index + 1}. {query["title"]} - _{query["uploader"]}_\n {query["duration_string"]}\n'
await self.__createEmbed(
context,
'Queue',
Expand Down

0 comments on commit 5bbd04e

Please sign in to comment.