Skip to content

Commit

Permalink
Add custom bot help
Browse files Browse the repository at this point in the history
  • Loading branch information
TheShubhendra committed Oct 3, 2021
1 parent 7073df3 commit 8075f61
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 15 deletions.
8 changes: 6 additions & 2 deletions vidya/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import glob
import logging

from decouple import config

from .bot import Vidya
from .ext.help import VidyaHelpCommand

TOKEN = config("TOKEN")
LOGGING = int(config("LOGGING", 20))
Expand All @@ -33,11 +33,15 @@


vidya = Vidya(
command_prefix= ["vid", "Vid",] ,
command_prefix=[
"vid",
"Vid",
],
case_insensitive=True,
strip_after_prefix=True,
database_url=DATABASE_URL,
redis_url=REDIS_URL,
help_command=VidyaHelpCommand(),
)
for cog in glob.glob("vidya/cogs/*.py"):
vidya.load_extension(cog[:-3].replace("/", "."))
Expand Down
3 changes: 2 additions & 1 deletion vidya/cogs/chemistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ async def element(
ctx: Context,
element: Optional[Union[int, str]],
):
"""Fetch details about an Element."""
if element is None:
await ctx.send(
"Please send element name (`case sensitive`)\
Expand All @@ -49,7 +50,7 @@ async def element(
try:
el = get_element(element)
await ctx.send(
embed= await self.embed.element(el),
embed=await self.embed.element(el),
)
except sqlalchemy.exc.NoResultFound:
if isinstance(element, int):
Expand Down
14 changes: 3 additions & 11 deletions vidya/cogs/currency.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vidya - A Discord bot to play quizzes and learn with fun.
# Copyright (C) 2021 Shubhendra Kushwaha
# Email: [email protected]
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
Expand All @@ -19,7 +10,7 @@
Cog,
Context,
command,
)
)


class Currency(Cog):
Expand All @@ -30,6 +21,7 @@ def __init__(self, bot):

@command()
async def daily(self, ctx: Context):
"""Earn Daily Reward."""
ttl = await self.redis.ttl(
f"daily:{ctx.author.id}",
)
Expand Down
1 change: 1 addition & 0 deletions vidya/cogs/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(self, bot):

@command()
async def profile(self, ctx: Context, user: Optional[User]):
"""Shows profile."""
if user is None:
user = ctx.author
embed = await self.embed.profile(user)
Expand Down
1 change: 1 addition & 0 deletions vidya/cogs/quiz.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(self, bot: "Vidya"):

@command(aliases=["quiz"])
async def play(self, ctx: Context):
"""Get a quiz."""
quizzes = await self.quiz.fetch()
for quiz in quizzes:
await self.quiz.send(ctx, quiz)
Expand Down
38 changes: 37 additions & 1 deletion vidya/ext/embeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import asyncio
import sys

from typing import Dict, List

import aiohttp
import discord
import pip
from discord import Colour, Embed, User
from discord.ext.commands import Command, Cog
from mendeleev import Element

from vidya.api import OpenTDBQuiz, Word
Expand Down Expand Up @@ -259,3 +260,38 @@ def func(x):
if res.status == 200:
embed.set_image(url=url)
return embed

def command_help(
self,
command: Command,
prefix: str = "vid ",
):
embed = self.default(title=f"{command.qualified_name} command help")
embed.add_field(
name="Help",
value=command.help if command.help else command.short_doc,
)
if command.usage is not None:
embed.add_field(
name="Example",
value=command.usage,
)
return embed

def bot_help(
self,
mapping: Dict[Cog, List[Command]],
prefix: str = "vid ",
) -> Embed:
embed = self.default(title="Help Menu")
for cog, commands in mapping.items():
if commands is None or len(commands) < 1:
continue
cmd_str = ""
for command in commands:
cmd_str += f"{prefix}{command.qualified_name} - {command.short_doc}\n"
embed.add_field(
name=getattr(cog, "qualified_name", "Other"),
value=cmd_str,
)
return embed
44 changes: 44 additions & 0 deletions vidya/ext/help.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vidya - A Discord bot to play quizzes and learn with fun.
# Copyright (C) 2021 Shubhendra Kushwaha
# Email: [email protected]
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import Dict, List
from discord.ext.commands import (
Command,
Cog,
HelpCommand,
)

Mapping = Dict[Cog, List[Command]]


class VidyaHelpCommand(HelpCommand):
"""Custom help command."""

def __init__(self):
super().__init__()

async def send_bot_help(
self,
mapping: Mapping,
):
"""Method to send help for all commands."""
embed = self.context.bot.embed.bot_help(mapping)
await self.context.send(embed=embed)

async def send_command_help(self, command: Command):
"""Method to send help for a specific command."""
destination = self.get_destination()
await destination.send(embed=self.context.bot.embed.command_help(command))
26 changes: 26 additions & 0 deletions vidya/ext/quiz.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,32 @@ def __init__(self, bot: "Vidya"):
self.embed = self.bot.embed
self.db = self.bot.db
self.opentdb = OpenTDB()
self.categories = {
9: "General Knowledge",
10: "Books",
11: "Film",
12: "Music",
13: "Musicals & Theatres",
14: "Television",
15: "Video Games",
16: "Board Games",
17: "Science & Nature",
18: "Computers",
19: "Mathematics",
20: "Mythology",
21: "Sports",
22: "Geography",
23: "History",
24: "Politics",
25: "Art",
26: "Celebrities",
27: "Animals",
28: "Vehicles",
29: "Comics",
30: "Science: Gadgets",
31: "Japanese Anime & Manga",
32: "Cartoon & Animations",
}

async def fetch(self, *args, **kwargs):
quizzes = await self.opentdb.fetch(*args, **kwargs)
Expand Down

0 comments on commit 8075f61

Please sign in to comment.