Skip to content

Commit

Permalink
Allow /connect to take a channel parameter.
Browse files Browse the repository at this point in the history
- Also, remove type-hinting import in utils.
  • Loading branch information
Sachaa-Thanasius committed Mar 22, 2024
1 parent 666a9ec commit 4d63930
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
26 changes: 19 additions & 7 deletions musicbot/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,38 @@

@app_commands.command(name="connect")
@app_commands.guild_only()
async def muse_connect(itx: discord.Interaction[MusicBot]) -> None:
"""Join a voice channel."""
async def muse_connect(itx: discord.Interaction[MusicBot], channel: discord.VoiceChannel | None = None) -> None:
"""Join a voice channel.
Parameters
----------
channel: discord.VoiceChannel | None, optional
The voice channel to connect to if you aren't currently in a voice channel. Defaults to the current user
channel.
"""

# Known at runtime.
assert itx.guild and isinstance(itx.user, discord.Member)
vc = itx.guild.voice_client
assert isinstance(vc, MusicPlayer | None)

if vc is not None and itx.user.voice is not None:
if vc.channel != itx.user.voice.channel:
# Not sure in what circumstances a member would have a voice state without being in a valid channel.
target_channel = channel or itx.user.voice.channel
if target_channel != vc.channel:
if itx.user.guild_permissions.administrator:
# Not sure in what circumstances a member would have a voice state without being in a valid channel.
await vc.move_to(itx.user.voice.channel)
await itx.response.send_message(f"Joined the {itx.user.voice.channel} channel.")
await vc.move_to(target_channel)
await itx.response.send_message(f"Joined the {target_channel} channel.")
else:
await itx.response.send_message("Voice player is currently being used in another channel.")
else:
await itx.response.send_message("Voice player already connected to this voice channel.")
elif itx.user.voice is None:
await itx.response.send_message("Please join a voice channel and try again.")
if itx.user.guild_permissions.administrator and channel is not None:
await channel.connect(cls=MusicPlayer)
await itx.response.send_message(f"Joined the {channel} channel.")
else:
await itx.response.send_message("Please join a voice channel and try again.")
else:
# Not sure in what circumstances a member would have a voice state without being in a valid channel.
assert itx.user.voice.channel
Expand Down
10 changes: 2 additions & 8 deletions musicbot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@
from collections.abc import Callable, Coroutine
from datetime import timedelta
from pathlib import Path
from typing import TYPE_CHECKING, Any, Concatenate, NamedTuple, ParamSpec, Self, TypeVar
from typing import Any, Concatenate, NamedTuple, ParamSpec, Self, TypeVar

import discord
import wavelink


if TYPE_CHECKING:
from .bot import MusicBot
else:
MusicBot = discord.AutoShardedClient


P = ParamSpec("P")
T = TypeVar("T")
UnboundCommandCallback = Callable[Concatenate[discord.Interaction[Any], P], Coroutine[Any, Any, T]]
Expand Down Expand Up @@ -423,7 +417,7 @@ def ensure_voice_hook(func: UnboundCommandCallback[P, T]) -> UnboundCommandCallb
"""

@functools.wraps(func)
async def callback(itx: discord.Interaction[MusicBot], *args: P.args, **kwargs: P.kwargs) -> T:
async def callback(itx: discord.Interaction, *args: P.args, **kwargs: P.kwargs) -> T:
# Known at runtime in guild-only situation.
assert itx.guild and isinstance(itx.user, discord.Member)
vc = itx.guild.voice_client
Expand Down
8 changes: 6 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ description = "A small Discord bot for playing music."
readme = "README.md"
license = "MIT"
requires-python = ">=3.11"
authors = [{ name = "Sachaa-Thanasius", email = "111999343+Sachaa-Thanasius@users.noreply.github.com" }]
authors = [
{ name = "Sachaa-Thanasius", email = "111999343+Sachaa-Thanasius@users.noreply.github.com" },
]

[project.urls]
Homepage = "https://github.com/SutaHelmIndustries/discord-musicbot"
Expand Down Expand Up @@ -55,7 +57,9 @@ extend-ignore = [
"PLR2004", # Magic value comparison.
"SIM105", # Suppressable exception. I'm not paying the overhead of contextlib.suppress for stylistic choices.
"C90", # McCabe complexity memes.
"ANN101", # Type of self is implied.
"ANN101", # Type of self is implied in most cases.
"ANN102", # Type of cls is implied in most cases.
"ANN204", # Type of magic methods is implied or known in most cases.
"ANN401", # Need Any for args and kwargs.
"PLR", # Complexity things.
# Recommended by ruff when using ruff format.
Expand Down

0 comments on commit 4d63930

Please sign in to comment.