mirror of https://github.com/stijndcl/didier
Separate loading slash commands from normal commands, split slash commands to separate cogs, make init_extensions support directories
parent
ed0649c953
commit
831459a321
|
@ -1,30 +1,8 @@
|
||||||
import discord
|
import discord
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from dislash import slash_command, SlashInteraction, Option, OptionType
|
|
||||||
from decorators import help
|
from decorators import help
|
||||||
from enums.help_categories import Category
|
from enums.help_categories import Category
|
||||||
from functions.scrapers.google import google_search, SearchResult
|
from functions.scrapers.google import google_search, create_google_embed
|
||||||
|
|
||||||
|
|
||||||
def _create_google_embed(result: SearchResult) -> discord.Embed:
|
|
||||||
embed = discord.Embed(colour=discord.Colour.blue())
|
|
||||||
embed.set_author(name="Google Search")
|
|
||||||
|
|
||||||
# Empty list of results
|
|
||||||
if len(result.results) == 0:
|
|
||||||
embed.colour = discord.Colour.red()
|
|
||||||
embed.description = "Geen resultaten gevonden."
|
|
||||||
return embed
|
|
||||||
|
|
||||||
# Add results into a field
|
|
||||||
links = []
|
|
||||||
|
|
||||||
for index, link in enumerate(result.results):
|
|
||||||
links.append(f"{index + 1}: {link}")
|
|
||||||
|
|
||||||
embed.description = "\n".join(links)
|
|
||||||
|
|
||||||
return embed
|
|
||||||
|
|
||||||
|
|
||||||
class Google(commands.Cog):
|
class Google(commands.Cog):
|
||||||
|
@ -35,22 +13,6 @@ class Google(commands.Cog):
|
||||||
def cog_check(self, ctx):
|
def cog_check(self, ctx):
|
||||||
return not self.client.locked
|
return not self.client.locked
|
||||||
|
|
||||||
@slash_command(name="google",
|
|
||||||
description="Google search",
|
|
||||||
options=[
|
|
||||||
Option("query", "Search query", OptionType.STRING, required=True)
|
|
||||||
],
|
|
||||||
guild_ids=[880175869841277008]
|
|
||||||
)
|
|
||||||
async def _google_slash(self, interaction: SlashInteraction, query: str):
|
|
||||||
result = google_search(query)
|
|
||||||
|
|
||||||
if not result.results:
|
|
||||||
return await interaction.reply("Er ging iets fout (Response {})".format(result.status_code))
|
|
||||||
|
|
||||||
embed = _create_google_embed(result)
|
|
||||||
await interaction.reply(embed=embed)
|
|
||||||
|
|
||||||
@commands.command(name="Google", aliases=["Gtfm", "Search"], usage="[Query]", case_insensitive=True)
|
@commands.command(name="Google", aliases=["Gtfm", "Search"], usage="[Query]", case_insensitive=True)
|
||||||
@help.Category(Category.Other)
|
@help.Category(Category.Other)
|
||||||
async def google(self, ctx, *query):
|
async def google(self, ctx, *query):
|
||||||
|
@ -62,7 +24,7 @@ class Google(commands.Cog):
|
||||||
if not result.results:
|
if not result.results:
|
||||||
return await ctx.send("Er ging iets fout (Response {})".format(result.status_code))
|
return await ctx.send("Er ging iets fout (Response {})".format(result.status_code))
|
||||||
|
|
||||||
embed = _create_google_embed(result)
|
embed = create_google_embed(result)
|
||||||
await ctx.reply(embed=embed, mention_author=False)
|
await ctx.reply(embed=embed, mention_author=False)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
from discord.ext import commands
|
||||||
|
from dislash import slash_command, SlashInteraction, Option, OptionType
|
||||||
|
from functions.scrapers.google import google_search, create_google_embed
|
||||||
|
from startup.didier import Didier
|
||||||
|
|
||||||
|
|
||||||
|
class GoogleSlash(commands.Cog):
|
||||||
|
def __init__(self, client: Didier):
|
||||||
|
self.client: Didier = client
|
||||||
|
|
||||||
|
@slash_command(name="google",
|
||||||
|
description="Google search",
|
||||||
|
options=[
|
||||||
|
Option("query", "Search query", OptionType.STRING, required=True)
|
||||||
|
],
|
||||||
|
guild_ids=[728361030404538488, 880175869841277008]
|
||||||
|
)
|
||||||
|
async def _google_slash(self, interaction: SlashInteraction, query: str):
|
||||||
|
result = google_search(query)
|
||||||
|
|
||||||
|
if not result.results:
|
||||||
|
return await interaction.reply("Er ging iets fout (Response {})".format(result.status_code))
|
||||||
|
|
||||||
|
embed = create_google_embed(result)
|
||||||
|
print("got here")
|
||||||
|
await interaction.reply(embed=embed)
|
||||||
|
|
||||||
|
|
||||||
|
def setup(client: Didier):
|
||||||
|
client.add_cog(GoogleSlash(client))
|
|
@ -1,5 +1,6 @@
|
||||||
from typing import Optional, List
|
from typing import Optional, List
|
||||||
|
|
||||||
|
import discord
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from requests import get
|
from requests import get
|
||||||
|
@ -55,3 +56,24 @@ def google_search(query) -> SearchResult:
|
||||||
links.append(f"[{title}]({link})")
|
links.append(f"[{title}]({link})")
|
||||||
|
|
||||||
return SearchResult(200, links[:10])
|
return SearchResult(200, links[:10])
|
||||||
|
|
||||||
|
|
||||||
|
def create_google_embed(result: SearchResult) -> discord.Embed:
|
||||||
|
embed = discord.Embed(colour=discord.Colour.blue())
|
||||||
|
embed.set_author(name="Google Search")
|
||||||
|
|
||||||
|
# Empty list of results
|
||||||
|
if len(result.results) == 0:
|
||||||
|
embed.colour = discord.Colour.red()
|
||||||
|
embed.description = "Geen resultaten gevonden."
|
||||||
|
return embed
|
||||||
|
|
||||||
|
# Add results into a field
|
||||||
|
links = []
|
||||||
|
|
||||||
|
for index, link in enumerate(result.results):
|
||||||
|
links.append(f"{index + 1}: {link}")
|
||||||
|
|
||||||
|
embed.description = "\n".join(links)
|
||||||
|
|
||||||
|
return embed
|
||||||
|
|
|
@ -47,9 +47,24 @@ class Didier(commands.Bot):
|
||||||
self.load_extension(f"cogs.{ext}")
|
self.load_extension(f"cogs.{ext}")
|
||||||
|
|
||||||
# Load all remaining cogs
|
# Load all remaining cogs
|
||||||
for file in os.listdir("./cogs"):
|
self._init_directory("./cogs")
|
||||||
if file.endswith(".py") and not (file.startswith(self._preload)):
|
|
||||||
self.load_extension("cogs.{}".format(file[:-3]))
|
def _init_directory(self, path: str):
|
||||||
|
"""
|
||||||
|
Load all cogs from a directory
|
||||||
|
"""
|
||||||
|
# Path to pass into load_extension
|
||||||
|
load_path = path[2:].replace("/", ".")
|
||||||
|
|
||||||
|
for file in os.listdir(path):
|
||||||
|
# Python file
|
||||||
|
if file.endswith(".py"):
|
||||||
|
if not file.startswith(self._preload):
|
||||||
|
self.load_extension(f"{load_path}.{file[:-3]}")
|
||||||
|
elif os.path.isdir(new_path := f"{path}/{file}"):
|
||||||
|
# Subdirectory
|
||||||
|
# Also walrus operator hype
|
||||||
|
self._init_directory(new_path)
|
||||||
|
|
||||||
async def on_ipc_ready(self):
|
async def on_ipc_ready(self):
|
||||||
print("IPC server is ready.")
|
print("IPC server is ready.")
|
||||||
|
|
Loading…
Reference in New Issue