-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathjoke.py
More file actions
129 lines (103 loc) · 4.01 KB
/
joke.py
File metadata and controls
129 lines (103 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""Module for the joke extension for the discord bot."""
from __future__ import annotations
from typing import TYPE_CHECKING, Self
import discord
import munch
from core import auxiliary, cogs, extensionconfig
from discord.ext import commands
if TYPE_CHECKING:
import bot
async def setup(bot: bot.TechSupportBot) -> None:
"""Loading the Joke plugin into the bot
Args:
bot (bot.TechSupportBot): The bot object to register the cogs to
"""
config = extensionconfig.ExtensionConfig()
config.add(
key="blacklisted_filters",
datatype="list[str]",
title="Enable filter",
description=(
"Filters all categories listed"
"(nsfw,religious,political,racist,sexist,explicit)"
),
default=["nsfw", "explicit"],
)
config.add(
key="apply_in_nsfw_channels",
datatype="bool",
title="Apply in NSFW Channels",
description=("Toggles whether or not filters are applies in NSFW channels"),
default=False,
)
await bot.add_cog(Joker(bot=bot))
bot.add_extension_config("joke", config)
class Joker(cogs.BaseCog):
"""Class to make up the joke extension.
Attributes:
API_URL (str): The joke API URL
"""
API_URL: str = "https://v2.jokeapi.dev/joke/Any"
async def call_api(
self: Self, ctx: commands.Context, config: munch.Munch
) -> munch.Munch:
"""Calls the joke API and returns the raw response
Args:
ctx (commands.Context): The context in which the joke command was run in
config (munch.Munch): The guild config for the guild where the joke command was run
Returns:
munch.Munch: The reply from the API
"""
url = self.build_url(ctx, config)
response = await self.bot.http_functions.http_call(
"get", url, get_raw_response=True
)
return response
def build_url(self: Self, ctx: commands.Context, config: munch.Munch) -> str:
"""Builds the API URL based on exclusions of categories
Will exclude NSFW jokes if the channel isn't NSFW
Will exclude offensive jokes if the PC jokes config is enabled
Args:
ctx (commands.Context): The context in which the original joke command was run in
config (munch.Munch): The config for the guild where the original command was run
Returns:
str: The URL, properly formatted and ready to be called
"""
blacklist_flags = []
if (
config.extensions.joke.apply_in_nsfw_channels.value
or not ctx.channel.is_nsfw()
):
blacklist_flags = config.extensions.joke.blacklisted_filters.value
blacklists = ",".join(blacklist_flags)
url = f"{self.API_URL}?blacklistFlags={blacklists}&format=txt"
return url
def generate_embed(self: Self, joke_text: str) -> discord.Embed:
"""Makes a fancy embed showing the joke recieved from the API
Args:
joke_text (str): The raw text of the joke from the API
Returns:
discord.Embed: The formatted embed, ready to bt sent
"""
embed = discord.Embed(description=joke_text)
embed.set_author(name="Joke", icon_url=self.bot.user.display_avatar.url)
embed.color = discord.Color.random()
return embed
@auxiliary.with_typing
@commands.command(
name="joke",
brief="Tells a joke",
description="Tells a random joke",
usage="",
)
async def joke(self: Self, ctx: commands.Context) -> None:
"""Discord entry point for the joke command
This will get a joke and send it in the channel the command was called in
Args:
ctx (commands.Context): The context in which the command was run in
"""
config = self.bot.guild_configs[str(ctx.guild.id)]
response = await self.call_api(ctx, config)
text = response["text"]
embed = self.generate_embed(text)
await ctx.send(embed=embed)