Memegen slash commands + autocompletion

This commit is contained in:
Stijn De Clercq 2022-02-06 17:58:57 +01:00
parent a71232e292
commit 93ede132a2
5 changed files with 104 additions and 31 deletions

View file

@ -1,15 +1,16 @@
from data.embeds.xkcd import XKCDEmbed
from data.menus import leaderboards
from decorators import help
import discord
from discord.ext import commands
from enums.help_categories import Category
from functions import checks, stringFormatters
from functions.database import memes, trump, dadjoke
from functions.memes import generate
import json
import random
import requests
from discord.ext import commands
from data.embeds.xkcd import XKCDEmbed
from data.menus.memes import MemesList
from decorators import help
from enums.help_categories import Category
from functions import checks
from functions.database import memes, trump, dadjoke
from functions.memes import generate
class Fun(commands.Cog):
@ -98,17 +99,10 @@ class Fun(commands.Cog):
if result is None:
return await ctx.send("Deze meme staat niet in de database.")
# Convert to list to support item assignment
fields = list(fields)
generated = generate(result, fields)
# If the request was successful, remove the message calling it
if generated["success"]:
await self.utilsCog.removeMessage(ctx.message)
# Send the meme's url or the error message
await ctx.send(generated["message"])
await ctx.reply(generated["message"], mention_author=False)
@commands.command(name="Memes")
@commands.check(checks.allowedChannels)
@ -118,16 +112,7 @@ class Fun(commands.Cog):
Command that shows a list of memes in the database.
:param ctx: Discord Context
"""
memeList = memes.getAllMemes()
# Turn the list into a list of [Name: fields]
memeList = [": ".join([stringFormatters.title_case(meme[1]),
str(meme[2])]) for meme in sorted(memeList, key=lambda x: x[1])]
pages = paginated_leaderboard.Pages(
source=paginated_leaderboard.Source(memeList, "Memes", discord.Colour.blue()),
clear_reactions_after=True)
await pages.start(ctx)
return await MemesList(ctx=ctx).send()
@commands.command(name="Pjoke")
@help.Category(category=Category.Fun)

View file

@ -24,7 +24,7 @@ class FootballSlash(commands.Cog):
@_jpl_group.command(name="table", description="Huidige rangschikking")
async def _jpl_table_slash(self, ctx: ApplicationContext):
await ctx.response.defer()
await ctx.respond(get_table())
await ctx.send_followup(get_table())
@_jpl_group.command(name="update", description="Update de code voor deze competitie (owner-only)", default_permission=False)
@permissions.is_owner()

View file

@ -1,10 +1,34 @@
from discord.ext import commands
from discord.commands import slash_command, ApplicationContext, Option
from discord.commands import slash_command, ApplicationContext, Option, AutocompleteContext
from functions.database import memes
from functions.database.memes import getAllMemes
from data.embeds.xkcd import XKCDEmbed
from data.menus.memes import MemesList
from functions.memes import generate
from functions.stringFormatters import title_case
from startup.didier import Didier
all_memes = getAllMemes()
def autocomplete_memes(ctx: AutocompleteContext) -> list[str]:
starting = []
containing = []
val = ctx.value.lower()
# First show matches that start with this word, then matches that contain it
for meme in all_memes:
if meme[1].startswith(val):
starting.append(title_case(meme[1]))
elif val in meme[1]:
containing.append(title_case(meme[1]))
return [*starting, *containing]
class FunSlash(commands.Cog):
def __init__(self, client: Didier):
self.client: Didier = client
@ -15,6 +39,32 @@ class FunSlash(commands.Cog):
):
return await ctx.respond(embed=XKCDEmbed(num).create())
@slash_command(name="memes", description="Lijst van memegen-memes")
async def _memes_slash(self, ctx: ApplicationContext):
return await MemesList(ctx=ctx).respond()
@slash_command(name="memegen", description="Genereer memes")
async def _memegen_slash(self, ctx: ApplicationContext,
meme: Option(str, description="Naam van de template", required=True, autocomplete=autocomplete_memes),
field1: Option(str, required=True),
field2: Option(str, required=False, default=""),
field3: Option(str, required=False, default=""),
field4: Option(str, required=False, default="")):
# Get the meme info that corresponds to this name
result: memes.Meme = memes.getMeme(meme)
# No meme found
if result is None:
return await ctx.respond("Deze meme staat niet in de database.", ephemeral=True)
await ctx.response.defer()
fields = (field1, field2, field3, field4)
generated = generate(result, fields)
# Send generated meme or error message
await ctx.send_followup(generated["message"])
def setup(client: Didier):
client.add_cog(FunSlash(client))