Skip to content

Commit

Permalink
D. J.:
Browse files Browse the repository at this point in the history
- Improved ``!help`` command by replacing it with ``discord.Embed`` (#17)
  • Loading branch information
Dennis Jabs committed Sep 16, 2024
1 parent 3ce6f88 commit 0b4f8ad
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 28 deletions.
23 changes: 11 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ This project aims to build a versatile music bot for the Discord server ``Wixoss
## Features 🎶
Our bot currently supports the following commands:

| Commands | |
|:---------|-------------------------------------------------------------------------------------|
| !add | Adds a song (YouTube URL) to the playlist. |
| !join | Joins the current voice channel of the command author. |
| !leave | Leaves the voice channel. |
| !pause | Pauses the currently playing song. |
| !play | Starts playing the next song in the playlist. |
| !remove | Removes the first n songs from the playlist (excluding the currently playing song). |
| !reset | Stops the current song and clears the entire playlist. |
| !show | Displays the songs in the playlist (including the currently playing song). |
| !skip | Skips the current song. |
| !volume | Adjusts the volume of the audio playback. |
| Commands | |
|:---------|------------------------------------------------------------------|
| !join | Joins the voice channel of the author. |
| !leave | Leaves the voice channel. |
| !add | Adds an audio source (YouTube URL) to the playlist. |
| !play | Starts playing the audio source from the playlist. |
| !pause | Pauses the currently played audio source. |
| !skip | Skips the currently played audio source in the playlist. |
| !reset | Stops the currently played audio source and clears the playlist. |
| !show | Shows the audio sources from the playlist. |
| !volume | Changes the volume of the music player. |

## Run the Bot on a Linux Server with ``screen`` 💻
This guide explains how to run the discord bot even after closing your SSH connection using the ``screen`` package from Linux.
Expand Down
6 changes: 4 additions & 2 deletions discord_bot/command/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from discord_bot.command.music import Music, MusicState
from discord_bot.command.help import Help
from discord_bot.command.music import Music

del help # type: ignore[name-defined] # noqa: F821
del music # type: ignore[name-defined] # noqa: F821

__all__ = [
"Help",
"Music",
"MusicState",
]
24 changes: 24 additions & 0 deletions discord_bot/command/help.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from discord.ext import commands


class Help(commands.HelpCommand):
"""To display help information for all cogs and commands."""

async def send_bot_help(
self, mapping: dict[commands.Cog | None, list[commands.Command]]
):
"""
Sends the help message for the bot.
Args:
mapping (dict[commands.Cog | None, list[commands.Command]]):
A dictionary containing all cogs and commands.
"""
# Send the embed
channel = self.get_destination()

# Iterate through all cogs and commands
for cog, _ in mapping.items():
if cog is not None:
embed = cog.help_information()
await channel.send(embed=embed)
62 changes: 54 additions & 8 deletions discord_bot/command/music.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def __init__(
whitelisted_text_channels: list[str],
volume: int = 50,
disconnect_timeout: int = 600,
**kwargs,
):
if not roles_valid(whitelisted_roles):
raise ValueError(
Expand Down Expand Up @@ -210,7 +211,7 @@ async def _before_join(self, ctx: commands.Context):
@commands.command(aliases=["Join"])
async def join(self, ctx: commands.Context):
"""
Joins to the current voice channel of the author.
Joins the voice channel of the author.
Args:
ctx (commands.Context):
Expand Down Expand Up @@ -369,7 +370,7 @@ async def _before_play(self, ctx: commands.Context):
@commands.command(aliases=["Play"])
async def play(self, ctx: commands.Context):
"""
Start playing the next audio song from the playlist.
Starts playing the audio source from the playlist.
Args:
ctx (commands.Context):
Expand Down Expand Up @@ -432,7 +433,7 @@ async def _before_pause(self, ctx: commands.Context):
@commands.command(aliases=["Pause"])
async def pause(self, ctx: commands.Context):
"""
Pauses the current played audio source.
Pauses the currently played audio source.
Args:
ctx (commands.Context):
Expand Down Expand Up @@ -466,7 +467,7 @@ async def _before_skip(self, ctx: commands.Context):
@commands.command(aliases=["Skip"])
async def skip(self, ctx: commands.Context):
"""
Skips the currently playing audio source in the playlist.
Skips the currently played audio source in the playlist.
Args:
ctx (commands.Context):
Expand All @@ -490,7 +491,7 @@ async def _before_reset(self, ctx: commands.Context):
@commands.command(aliases=["Reset"])
async def reset(self, ctx: commands.Context):
"""
Stops the currently playing audio source and clears the playlist.
Stops the currently played audio source and clears the playlist.
Args:
ctx (commands.Context):
Expand Down Expand Up @@ -529,7 +530,7 @@ async def _before_show(self, ctx: commands.Context):
@commands.command(aliases=["Show"])
async def show(self, ctx: commands.Context):
"""
Displays the audio sources from the playlist.
Shows the audio sources from the playlist.
Args:
ctx (commands.Context):
Expand Down Expand Up @@ -570,14 +571,14 @@ async def _before_volume(self, ctx: commands.Context, *, volume: int):
@commands.command(aliases=["Volume"])
async def volume(self, ctx: commands.Context, *, volume: int):
"""
Changes the volume of the audio playback.
Changes the volume of the music player.
Args:
ctx (commands.Context):
The discord context
volume (int):
The volume of the audio playback
The new volume
"""
await self._before_volume(ctx=ctx, volume=volume)

Expand All @@ -588,3 +589,48 @@ async def volume(self, ctx: commands.Context, *, volume: int):
return await ctx.send(f"✅ Changed volume to ``{self.curr_volume}``!")
# Case: New volume is the same as before
await ctx.send(f"⚠️ Already using volume ``{self.curr_volume}``!")

@staticmethod
def help_information() -> discord.Embed:
"""Returns the help information of the music commands."""
embed = discord.Embed(title="Music:", color=discord.Color.blue())
embed.add_field(
name="!join", value="Joins the voice channel of the author.", inline=False
)
embed.add_field(name="!leave", value="Leaves the voice channel.", inline=False)
embed.add_field(
name="!add <url>",
value="Adds an audio source (YouTube URL) to the playlist.",
inline=False,
)
embed.add_field(
name="!play",
value="Starts playing the audio source from the playlist.",
inline=False,
)
embed.add_field(
name="!pause",
value="Pauses the currently played audio source.",
inline=False,
)
embed.add_field(
name="!skip",
value="Skips the currently played audio source in the playlist.",
inline=False,
)
embed.add_field(
name="!reset",
value="Stops the currently played audio source and clears the playlist.",
inline=False,
)
embed.add_field(
name="!show",
value="Shows the audio sources from the playlist.",
inline=False,
)
embed.add_field(
name="!volume <volume>",
value="Changes the volume of the music player.",
inline=False,
)
return embed
10 changes: 4 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
import discord
from discord.ext import commands

from discord_bot.command import Music
from discord_bot.command import Help, Music


async def main(client: commands.Bot, **kwargs):
"""Starting point of the bot."""
async with client:
await client.add_cog(Music(client, **kwargs))
await client.start(
token=os.environ["__DISCORD_API_KEY__"],
)
await client.start(token=os.environ["__DISCORD_API_KEY__"])


if __name__ == "__main__":
Expand All @@ -24,8 +22,8 @@ async def main(client: commands.Bot, **kwargs):
# Get all intents
intents = discord.Intents.all()

# Change the default behavior of !help by disabling parameter descriptions
help_command = commands.DefaultHelpCommand(show_parameter_descriptions=False)
# Create the help command
help_command = Help()

# Create the bot
bot = commands.Bot(
Expand Down

0 comments on commit 0b4f8ad

Please sign in to comment.