implemented general help command and command specific help
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -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/
|
||||
|
||||
18
bot.py
Normal file
18
bot.py
Normal file
@@ -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))
|
||||
23
cogs/music/main.py
Normal file
23
cogs/music/main.py
Normal file
@@ -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`")
|
||||
15
config.py
15
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":
|
||||
|
||||
79
help.py
79
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 <command>` or `help <category>` 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)
|
||||
|
||||
17
main.py
17
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"))
|
||||
|
||||
Reference in New Issue
Block a user