From 2cd080bf0256c4ee8a9aebadafc3bc971d381f9c Mon Sep 17 00:00:00 2001 From: top1055 <123alexfeetham@gmail.com> Date: Wed, 24 May 2023 01:19:40 +0100 Subject: [PATCH] implemented general help command and command specific help --- .gitignore | 3 ++ bot.py | 18 +++++++++++ cogs/music/main.py | 23 ++++++++++++++ config.py | 15 ++++++++- help.py | 79 ++++++++++++++++++++++++++++++++++++++-------- main.py | 17 ++-------- 6 files changed, 126 insertions(+), 29 deletions(-) create mode 100644 bot.py create mode 100644 cogs/music/main.py diff --git a/.gitignore b/.gitignore index 68bc17f..554e8a0 100644 --- a/.gitignore +++ b/.gitignore @@ -158,3 +158,6 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ + +# My stuff +data/ diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..6e508e5 --- /dev/null +++ b/bot.py @@ -0,0 +1,18 @@ +from discord.ext import commands +import config +from cogs.music.main import music + +cogs = [ + music + ] + +class Astro(commands.Bot): + + # Once the bot is up and running + async def on_ready(self): + # Set the status + await self.change_presence(activity=config.get_status()) + + # Setup commands + for cog in cogs: + await self.add_cog(cog(self)) diff --git a/cogs/music/main.py b/cogs/music/main.py new file mode 100644 index 0000000..525b201 --- /dev/null +++ b/cogs/music/main.py @@ -0,0 +1,23 @@ +import discord +from discord.ext import commands +from discord.ext.commands.context import Context + +import datetime +import pytz + +class music(commands.Cog): + def __init__(self, client): + self.name = "🎶 Music" + self.emoji = "🎶" + self.client = client + + @commands.command( + help="Displays latency from the bot", + aliases=['delay']) + async def ping(self, e: Context): + start_time = datetime.datetime.now(pytz.utc) + end_time = e.message.created_at + + delay = int((end_time - start_time).total_seconds() * 1000) + + await e.send(f"Pong! `{delay}MS`") diff --git a/config.py b/config.py index 8abc6ba..32e87b7 100644 --- a/config.py +++ b/config.py @@ -2,6 +2,7 @@ # This file should parse all configurations within the bot import discord +from discord import Color import json # Read data from JSON file in ./data/config.json @@ -48,6 +49,18 @@ def get_status(): data.get('link') ) +# Get colors from colorscheme +def get_color(color): + data = read_data() + + if data is False or data.get('status') is False: + raise Exception("Missing config data: color") + + # Grab color + string_value = data.get("colorscheme").get(color) + hex_value = Color.from_str(string_value) + return hex_value + # Taking JSON variables and converting them into a presence # Use None url incase not provided @@ -63,7 +76,7 @@ def translate_status(status_type, status_text, status_url=None): return discord.Activity( type=discord.ActivityType.streaming, name=status_text, - url=status_link + url=status_url ) elif status_type == "listening": diff --git a/help.py b/help.py index ab6f26c..171644a 100644 --- a/help.py +++ b/help.py @@ -1,23 +1,76 @@ +from collections.abc import Mapping +from typing import List import discord +from discord.app_commands import Command from discord.ext import commands +from discord.ext.commands.cog import Cog +import config -class AstroHelp(commands.HelpCommand): +class AstroHelp(commands.MinimalHelpCommand): - # Help regular - async def send_bot_help(self, mapping): - await self.context.send("This is help") + def __init__(self): + super().__init__() + self.command_attrs = { + 'name': "help", + 'aliases': ["commands", "?"], + 'cooldown': commands.CooldownMapping.from_cooldown(2, 5.0, commands.BucketType.user) + } - # Help with specific command + # Called when using help no args + async def send_bot_help(self, mapping: Mapping[Cog, List[Command]]): + + # Our embed message + embed = discord.Embed( + title="Help", + color=config.get_color("main")) + embed.add_field(name="", + value="Use `help ` or `help ` for more details", + inline=False) + + embed.set_footer(text=f"Prefix: {self.context.prefix}") + + # grabs iterable of (Cog, list[Command]) + for cog, commands in mapping.items(): + + # Grab commands only the user can access + # Safe to ignore warning + filtered = await self.filter_commands(commands, sort=True) + + # For each command we grab the signature + command_signatures = [ + # Rmove prefix and format command name + f"``{self.get_command_signature(c)[1:]}``" for c in filtered] + + # Check if cog has any commands + if command_signatures: + + # Use get incase cog is None + cog_name = getattr(cog, "name", "No Category") + + # Add cog section to help message + embed.add_field( + name=f"{cog_name}", + value="\n".join(command_signatures), + inline=True) + + # Display message + channel = self.get_destination() + await channel.send(embed=embed) + + + # Help for specific command async def send_command_help(self, command): - await self.context.send(f"You asked for help with: {command}") + embed = discord.Embed( + title=self.get_command_signature(command)[1:], + color=config.get_color("main")) + embed.set_footer(text=f"Prefix: {self.context.prefix}") + embed.add_field(name="Description", value=command.help) - # Help for a group - async def send_group_help(self, group): - await self.context.send(f"This is a group: {group}") + alias = command.aliases + if alias: + embed.add_field(name="Aliases", value=", ".join(alias), inline=False) - - # Help for cog - async def send_cog_help(self, cog): - await self.context.send(f"This is a cog: {cog}") + channel = self.get_destination() + await channel.send(embed=embed) diff --git a/main.py b/main.py index 1138c8d..5df9ce7 100644 --- a/main.py +++ b/main.py @@ -1,22 +1,9 @@ import discord -from discord.ext import commands +from bot import Astro import config import help -cogs = [] - -class Serenity(commands.Bot): - - # Once the bot is up and running - async def on_ready(self): - # Set the status - await self.change_presence(activity=config.get_status()) - - # Setup commands - for cog in cogs: - await cog.setup(self) - -client = Serenity(command_prefix=config.get_prefix(), intents=discord.Intents.all()) +client = Astro(command_prefix=config.get_prefix(), intents=discord.Intents.all()) client.help_command = help.AstroHelp() client.run(config.get_login("dev"))