Skip to content

Commit

Permalink
Upgrading stability in errors occurrences
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelSolVargas committed Jul 25, 2022
1 parent b904c75 commit f27dc1d
Show file tree
Hide file tree
Showing 13 changed files with 310 additions and 155 deletions.
1 change: 1 addition & 0 deletions Config/Configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def __init__(self) -> None:
'[ERROR] -> You must create and .env file with all required fields, see documentation for help')

self.CLEANER_MESSAGES_QUANT = 5
self.ACQUIRE_LOCK_TIMEOUT = 10
self.COMMANDS_PATH = 'DiscordCogs'
self.VC_TIMEOUT = 600

Expand Down
1 change: 1 addition & 0 deletions Config/Messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def __init__(self) -> None:
self.DOWNLOADING_ERROR = "❌ It's impossible to download and play this video"
self.EXTRACTING_ERROR = '❌ An error ocurred while searching for the songs'

self.ERROR_IN_PROCESS = "❌ Due to a internal error your player was restarted, skipping the song."
self.MY_ERROR_BAD_COMMAND = 'This string serves to verify if some error was raised by myself on purpose'
self.BAD_COMMAND_TITLE = 'Misuse of command'
self.BAD_COMMAND = f'❌ Bad usage of this command, type {configs.BOT_PREFIX}help "command" to understand the command better'
Expand Down
227 changes: 136 additions & 91 deletions DiscordCogs/MusicCog.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,155 +35,200 @@ def __init__(self, bot) -> None:

@commands.command(name="play", help=helper.HELP_PLAY, description=helper.HELP_PLAY_LONG, aliases=['p', 'tocar'])
async def play(self, ctx: Context, *args) -> None:
controller = PlayHandler(ctx, self.__bot)

response = await controller.run(args)
if response is not None:
view1 = EmbedView(response)
view2 = EmoteView(response)
await view1.run()
await view2.run()
try:
controller = PlayHandler(ctx, self.__bot)

response = await controller.run(args)
if response is not None:
view1 = EmbedView(response)
view2 = EmoteView(response)
await view1.run()
await view2.run()
except Exception as e:
print(f'[ERROR IN COG] -> {e}')

@commands.command(name="queue", help=helper.HELP_QUEUE, description=helper.HELP_QUEUE_LONG, aliases=['q', 'fila', 'musicas'])
async def queue(self, ctx: Context) -> None:
controller = QueueHandler(ctx, self.__bot)
try:
controller = QueueHandler(ctx, self.__bot)

response = await controller.run()
view2 = EmbedView(response)
await view2.run()
response = await controller.run()
view2 = EmbedView(response)
await view2.run()
except Exception as e:
print(f'[ERROR IN COG] -> {e}')

@commands.command(name="skip", help=helper.HELP_SKIP, description=helper.HELP_SKIP_LONG, aliases=['s', 'pular', 'next'])
async def skip(self, ctx: Context) -> None:
controller = SkipHandler(ctx, self.__bot)
try:
controller = SkipHandler(ctx, self.__bot)

response = await controller.run()
if response.success:
view = EmoteView(response)
else:
view = EmbedView(response)
response = await controller.run()
if response.success:
view = EmoteView(response)
else:
view = EmbedView(response)

await view.run()
await view.run()
except Exception as e:
print(f'[ERROR IN COG] -> {e}')

@commands.command(name='stop', help=helper.HELP_STOP, description=helper.HELP_STOP_LONG, aliases=['parar'])
async def stop(self, ctx: Context) -> None:
controller = StopHandler(ctx, self.__bot)
try:
controller = StopHandler(ctx, self.__bot)

response = await controller.run()
if response.success:
view = EmoteView(response)
else:
view = EmbedView(response)
response = await controller.run()
if response.success:
view = EmoteView(response)
else:
view = EmbedView(response)

await view.run()
await view.run()
except Exception as e:
print(f'[ERROR IN COG] -> {e}')

@commands.command(name='pause', help=helper.HELP_PAUSE, description=helper.HELP_PAUSE_LONG, aliases=['pausar', 'pare'])
async def pause(self, ctx: Context) -> None:
controller = PauseHandler(ctx, self.__bot)
try:
controller = PauseHandler(ctx, self.__bot)

response = await controller.run()
view1 = EmoteView(response)
view2 = EmbedView(response)
await view1.run()
await view2.run()
response = await controller.run()
view1 = EmoteView(response)
view2 = EmbedView(response)
await view1.run()
await view2.run()
except Exception as e:
print(f'[ERROR IN COG] -> {e}')

@commands.command(name='resume', help=helper.HELP_RESUME, description=helper.HELP_RESUME_LONG, aliases=['soltar', 'despausar'])
async def resume(self, ctx: Context) -> None:
controller = ResumeHandler(ctx, self.__bot)
try:
controller = ResumeHandler(ctx, self.__bot)

response = await controller.run()
view1 = EmoteView(response)
view2 = EmbedView(response)
await view1.run()
await view2.run()
response = await controller.run()
view1 = EmoteView(response)
view2 = EmbedView(response)
await view1.run()
await view2.run()
except Exception as e:
print(f'[ERROR IN COG] -> {e}')

@commands.command(name='prev', help=helper.HELP_PREV, description=helper.HELP_PREV_LONG, aliases=['anterior', 'return', 'previous'])
async def prev(self, ctx: Context) -> None:
controller = PrevHandler(ctx, self.__bot)
try:
controller = PrevHandler(ctx, self.__bot)

response = await controller.run()
if response is not None:
view1 = EmbedView(response)
view2 = EmoteView(response)
await view1.run()
await view2.run()
except Exception as e:
print(f'[ERROR IN COG] -> {e}')

response = await controller.run()
if response is not None:
@commands.command(name='history', help=helper.HELP_HISTORY, description=helper.HELP_HISTORY_LONG, aliases=['historico', 'anteriores', 'hist'])
async def history(self, ctx: Context) -> None:
try:
controller = HistoryHandler(ctx, self.__bot)

response = await controller.run()
view1 = EmbedView(response)
view2 = EmoteView(response)
await view1.run()
await view2.run()

@commands.command(name='history', help=helper.HELP_HISTORY, description=helper.HELP_HISTORY_LONG, aliases=['historico', 'anteriores', 'hist'])
async def history(self, ctx: Context) -> None:
controller = HistoryHandler(ctx, self.__bot)

response = await controller.run()
view1 = EmbedView(response)
view2 = EmoteView(response)
await view1.run()
await view2.run()
except Exception as e:
print(f'[ERROR IN COG] -> {e}')

@commands.command(name='loop', help=helper.HELP_LOOP, description=helper.HELP_LOOP_LONG, aliases=['l', 'repeat'])
async def loop(self, ctx: Context, args='') -> None:
controller = LoopHandler(ctx, self.__bot)
try:
controller = LoopHandler(ctx, self.__bot)

response = await controller.run(args)
view1 = EmoteView(response)
view2 = EmbedView(response)
await view1.run()
await view2.run()
response = await controller.run(args)
view1 = EmoteView(response)
view2 = EmbedView(response)
await view1.run()
await view2.run()
except Exception as e:
print(f'[ERROR IN COG] -> {e}')

@commands.command(name='clear', help=helper.HELP_CLEAR, description=helper.HELP_CLEAR_LONG, aliases=['c', 'limpar'])
async def clear(self, ctx: Context) -> None:
controller = ClearHandler(ctx, self.__bot)
try:
controller = ClearHandler(ctx, self.__bot)

response = await controller.run()
view = EmoteView(response)
await view.run()
response = await controller.run()
view = EmoteView(response)
await view.run()
except Exception as e:
print(f'[ERROR IN COG] -> {e}')

@commands.command(name='np', help=helper.HELP_NP, description=helper.HELP_NP_LONG, aliases=['playing', 'now', 'this'])
async def now_playing(self, ctx: Context) -> None:
controller = NowPlayingHandler(ctx, self.__bot)
try:
controller = NowPlayingHandler(ctx, self.__bot)

response = await controller.run()
view1 = EmbedView(response)
view2 = EmoteView(response)
await view1.run()
await view2.run()
response = await controller.run()
view1 = EmbedView(response)
view2 = EmoteView(response)
await view1.run()
await view2.run()
except Exception as e:
print(f'[ERROR IN COG] -> {e}')

@commands.command(name='shuffle', help=helper.HELP_SHUFFLE, description=helper.HELP_SHUFFLE_LONG, aliases=['aleatorio', 'misturar'])
async def shuffle(self, ctx: Context) -> None:
controller = ShuffleHandler(ctx, self.__bot)
try:
controller = ShuffleHandler(ctx, self.__bot)

response = await controller.run()
view1 = EmbedView(response)
view2 = EmoteView(response)
await view1.run()
await view2.run()
response = await controller.run()
view1 = EmbedView(response)
view2 = EmoteView(response)
await view1.run()
await view2.run()
except Exception as e:
print(f'[ERROR IN COG] -> {e}')

@commands.command(name='move', help=helper.HELP_MOVE, description=helper.HELP_MOVE_LONG, aliases=['m', 'mover'])
async def move(self, ctx: Context, pos1, pos2='1') -> None:
controller = MoveHandler(ctx, self.__bot)
try:
controller = MoveHandler(ctx, self.__bot)

response = await controller.run(pos1, pos2)
view1 = EmbedView(response)
view2 = EmoteView(response)
await view1.run()
await view2.run()
response = await controller.run(pos1, pos2)
view1 = EmbedView(response)
view2 = EmoteView(response)
await view1.run()
await view2.run()
except Exception as e:
print(f'[ERROR IN COG] -> {e}')

@commands.command(name='remove', help=helper.HELP_REMOVE, description=helper.HELP_REMOVE_LONG, aliases=['remover'])
async def remove(self, ctx: Context, position) -> None:
controller = RemoveHandler(ctx, self.__bot)
try:
controller = RemoveHandler(ctx, self.__bot)

response = await controller.run(position)
view1 = EmbedView(response)
view2 = EmoteView(response)
await view1.run()
await view2.run()
response = await controller.run(position)
view1 = EmbedView(response)
view2 = EmoteView(response)
await view1.run()
await view2.run()
except Exception as e:
print(f'[ERROR IN COG] -> {e}')

@commands.command(name='reset', help=helper.HELP_RESET, description=helper.HELP_RESET_LONG, aliases=['resetar'])
async def reset(self, ctx: Context) -> None:
controller = ResetHandler(ctx, self.__bot)
try:
controller = ResetHandler(ctx, self.__bot)

response = await controller.run()
view1 = EmbedView(response)
view2 = EmoteView(response)
await view1.run()
await view2.run()
response = await controller.run()
view1 = EmbedView(response)
view2 = EmoteView(response)
await view1.run()
await view2.run()
except Exception as e:
print(f'[ERROR IN COG] -> {e}')


def setup(bot):
Expand Down
11 changes: 9 additions & 2 deletions Handlers/ClearHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ async def run(self) -> HandlerResponse:
if processInfo:
# Clear the playlist
playlist = processInfo.getPlaylist()
with processInfo.getLock():
processLock = processInfo.getLock()
acquired = processLock.acquire(timeout=self.config.ACQUIRE_LOCK_TIMEOUT)
if acquired:
playlist.clear()

processLock.release()
processLock.release()
else:
processManager.resetProcess(self.guild, self.ctx)
embed = self.embeds.PLAYER_RESTARTED()
return HandlerResponse(self.ctx, embed)
return HandlerResponse(self.ctx)
10 changes: 9 additions & 1 deletion Handlers/HistoryHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@ async def run(self) -> HandlerResponse:
processManager = ProcessManager()
processInfo = processManager.getRunningPlayerInfo(self.guild)
if processInfo:
with processInfo.getLock():
processLock = processInfo.getLock()
acquired = processLock.acquire(timeout=self.config.ACQUIRE_LOCK_TIMEOUT)
if acquired:
playlist = processInfo.getPlaylist()
history = playlist.getSongsHistory()
processLock.release()
else:
# If the player doesn't respond in time we restart it
processManager.resetProcess(self.guild, self.ctx)
embed = self.embeds.PLAYER_RESTARTED()
return HandlerResponse(self.ctx, embed)
else:
history = []

Expand Down
16 changes: 11 additions & 5 deletions Handlers/LoopHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ async def run(self, args: str) -> HandlerResponse:

playlist = processInfo.getPlaylist()

with processInfo.getLock():
processLock = processInfo.getLock()
acquired = processLock.acquire(timeout=self.config.ACQUIRE_LOCK_TIMEOUT)
if acquired:
if args == '' or args is None:
playlist.loop_all()
embed = self.embeds.LOOP_ALL_ACTIVATED()
return HandlerResponse(self.ctx, embed)

args = args.lower()
error = None
if playlist.getCurrentSong() is None:
embed = self.embeds.NOT_PLAYING()
error = BadCommandUsage()
Expand All @@ -36,16 +39,19 @@ async def run(self, args: str) -> HandlerResponse:
if args == 'one':
playlist.loop_one()
embed = self.embeds.LOOP_ONE_ACTIVATED()
return HandlerResponse(self.ctx, embed)
elif args == 'all':
playlist.loop_all()
embed = self.embeds.LOOP_ALL_ACTIVATED()
return HandlerResponse(self.ctx, embed)
elif args == 'off':
playlist.loop_off()
embed = self.embeds.LOOP_DISABLE()
return HandlerResponse(self.ctx, embed)
else:
error = BadCommandUsage()
embed = self.embeds.BAD_LOOP_USE()
return HandlerResponse(self.ctx, embed, error)

processLock.release()
return HandlerResponse(self.ctx, embed)
else:
processManager.resetProcess(self.guild, self.ctx)
embed = self.embeds.PLAYER_RESTARTED()
return HandlerResponse(self.ctx, embed)
Loading

0 comments on commit f27dc1d

Please sign in to comment.