Skip to content

Commit

Permalink
Merge pull request #93 from Krutyi-4el/develop
Browse files Browse the repository at this point in the history
support replies to messages with files
  • Loading branch information
solaluset authored Jan 27, 2024
2 parents 3994535 + cb4a2ce commit 6886fdd
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 17 deletions.
4 changes: 2 additions & 2 deletions config/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
"STATUS_TEXT": "Music, type {prefix}help",

"NO_GUILD_MESSAGE": "Error: Please use guild channels to interact with the bot",
"USER_NOT_IN_VC_MESSAGE": "Error: Please join the active voice channel to use commands and buttons",
"USER_NOT_IN_VC_MESSAGE": "Error: Please join the (active) voice channel to use commands and buttons",
"WRONG_CHANNEL_MESSAGE": "Error: Please use configured command channel",
"NOT_CONNECTED_MESSAGE": "Error: Bot not connected to any voice channel",
"ALREADY_CONNECTED_MESSAGE": "Error: Already connected to a voice channel",
"NOT_A_DJ": "You are not a DJ",
"USER_MISSING_PERMISSIONS": "You don't have permissions for this",
"CHANNEL_NOT_FOUND_MESSAGE": "Error: Could not find channel",
"VOICE_PERMISSIONS_MISSING": "The bot needs VC permissions connect and speak",
"PLAY_ARGS_MISSING": "You need to specify the track or attach a file.",
"PLAY_ARGS_MISSING": "You need to specify the track, attach a file or reply to a message with the file.",

"ADD_MESSAGE": "To add this bot to your own Server, click [here]({link})",

Expand Down
3 changes: 3 additions & 0 deletions musicbot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,6 @@
print_exc(file=sys.stderr)
print("Set the correct token in config.json", file=sys.stderr)
sys.exit(1)
except RuntimeError as e:
if e.args != ("Event loop is closed",):
raise
29 changes: 23 additions & 6 deletions musicbot/audiocontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from itertools import islice
from inspect import isawaitable
from traceback import print_exc
from typing import TYPE_CHECKING, Coroutine, Optional
from typing import TYPE_CHECKING, Coroutine, Iterable, Optional, Union

import discord
from config import config
Expand Down Expand Up @@ -351,21 +351,38 @@ async def play_song(self, song: Song):

self.preload_queue()

async def process_song(self, track: str) -> Optional[Song]:
async def process_song(
self, track: Union[str, Iterable[str]]
) -> Optional[Song]:
"""Adds the track to the playlist instance
Starts playing if it is the first song"""

loaded_song = await loader.load_song(track)
if isinstance(track, str):
loaded_song = await loader.load_song(track)
else:
loaded_song = []
for t in track:
loaded_t = await loader.load_song(t)
if not loaded_t:
continue
elif isinstance(loaded_t, Song):
loaded_song.append(loaded_t)
else:
loaded_song.extend(loaded_t)
if not loaded_song:
return None
elif isinstance(loaded_song, Song):
self.playlist.add(loaded_song)
else:
for song in loaded_song:
self.playlist.add(song)
loaded_song = Song(
linkutils.Origins.Playlist, linkutils.SiteTypes.UNKNOWN
)
if len(loaded_song) == 1:
# special-case one-item playlists
loaded_song = loaded_song[0]
else:
loaded_song = Song(
linkutils.Origins.Playlist, linkutils.SiteTypes.UNKNOWN
)

if self.current_song is None:
print("Playing {}".format(track))
Expand Down
20 changes: 14 additions & 6 deletions musicbot/commands/music.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Iterable, Union

from discord import Option, Attachment
from discord.ui import View
from discord.ext import commands, bridge
Expand Down Expand Up @@ -61,18 +63,24 @@ async def cog_before_invoke(self, ctx: AudioContext):
async def _play(
self, ctx: AudioContext, *, track: str = None, file: Attachment = None
):
if ctx.message and ctx.message.attachments:
file = ctx.message.attachments[0]
if file is not None:
track = file.url
elif track is None:
if track is None and ctx.message:
if ctx.message.attachments:
track = [file.url for file in ctx.message.attachments]
elif ctx.message.reference and ctx.message.reference.resolved:
track = [
file.url
for file in ctx.message.reference.resolved.attachments
]
if not track:
await ctx.send(config.PLAY_ARGS_MISSING)
return

await ctx.defer()
await self._play_song(ctx, track)

async def _play_song(self, ctx: AudioContext, track: str):
async def _play_song(
self, ctx: AudioContext, track: Union[str, Iterable[str]]
):
# reset timer
await ctx.audiocontroller.timer.start(True)

Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
py-cord[voice] @ https://github.com/Krutyi-4el/pycord/archive/v2.5.6.zip
yt-dlp==2023.11.16
yt-dlp==2023.12.30
aiohttp==3.9.1
beautifulsoup4==4.12.2
beautifulsoup4==4.12.3
spotipy==2.23.0
SQLAlchemy[asyncio]==2.0.23
SQLAlchemy[asyncio]==2.0.25
alembic==1.13.1
aioconsole==0.7.0
./config

0 comments on commit 6886fdd

Please sign in to comment.