-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathkanye.py
More file actions
143 lines (117 loc) · 4.5 KB
/
kanye.py
File metadata and controls
143 lines (117 loc) · 4.5 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""Module for the kanye extension for the discord bot."""
from __future__ import annotations
import asyncio
import random
from typing import TYPE_CHECKING, Self
import discord
import munch
from core import auxiliary, cogs, extensionconfig
from discord.ext import commands
from functions import holidays
if TYPE_CHECKING:
import bot
async def setup(bot: bot.TechSupportBot) -> None:
"""Loading the Kanye plugin into the bot
Args:
bot (bot.TechSupportBot): The bot object to register the cogs to
"""
config = extensionconfig.ExtensionConfig()
config.add(
key="channel",
datatype="int",
title="Kanye Channel ID",
description="The ID of the channel the Kanye West quote should appear in",
default=None,
)
config.add(
key="min_wait",
datatype="int",
title="Min wait (hours)",
description="The minimum number of hours to wait between Kanye events",
default=24,
)
config.add(
key="max_wait",
datatype="int",
title="Max wait (hours)",
description="The minimum number of hours to wait between Kanye events",
default=48,
)
await bot.add_cog(KanyeQuotes(bot=bot, extension_name="kanye"))
bot.add_extension_config("kanye", config)
class KanyeQuotes(cogs.LoopCog):
"""Class to get the Kanye quotes from the api.
Attributes:
API_URL (str): The Kanye API URL
KANYE_PICS (list[str]): The list of Kanye pics to pick from randomly
"""
API_URL: str = "https://api.kanye.rest"
KANYE_PICS: list[str] = [
"https://i.imgur.com/ITmTXGz.jpg",
"https://i.imgur.com/o8BkPrL.jpg",
"https://i.imgur.com/sA5qP3F.jpg",
"https://i.imgur.com/1fX29Y3.jpg",
"https://i.imgur.com/g1o2Gro.jpg",
]
def generate_themed_embed(self: Self, quote: str) -> discord.Embed:
"""Generates a themed embed for the kayne plugin
Includes adding the quote, changing the color, and adding an icon
Args:
quote (str): The quote to put in the embed
Returns:
discord.Embed: The formatted embed, ready to be sent
"""
embed = auxiliary.generate_basic_embed(
title=f'"{quote}"',
description="Kanye West",
color=discord.Color.dark_gold(),
url=random.choice(self.KANYE_PICS),
)
return embed
async def get_quote(self: Self) -> str:
"""Calls the kanye API to get a quote, returns just the quote from the response
Returns:
str: The raw quote from the API, without any special formatting
"""
response = await self.bot.http_functions.http_call("get", self.API_URL)
return response.get("quote")
async def execute(self: Self, config: munch.Munch, guild: discord.Guild) -> None:
"""The main entry point for the loop for kanye
This is executed automatically and shouldn't be called manually
Args:
config (munch.Munch): The guild config where the loop is taking place
guild (discord.Guild): The guild where the loop is taking place
"""
if holidays.isGuildClosed(self.bot, guild):
return
quote = await self.get_quote()
embed = self.generate_themed_embed(quote=quote)
channel = guild.get_channel(int(config.extensions.kanye.channel.value))
if not channel:
return
await channel.send(embed=embed)
async def wait(self: Self, config: munch.Munch, _: discord.Guild) -> None:
"""This sleeps a random amount of time between Kanye quotes
Args:
config (munch.Munch): The guild config where the loop is taking place
"""
await asyncio.sleep(
random.randint(
config.extensions.kanye.min_wait.value * 3600,
config.extensions.kanye.max_wait.value * 3600,
)
)
@auxiliary.with_typing
@commands.command(
brief="Gets a Kanye West quote",
description="Gets a random Kanye West quote from the Kanye West API",
)
async def kanye(self: Self, ctx: commands.Context) -> None:
"""Entry point and logic for the .kanye discord command
This allows for printing of a Kanye quote outside of the loop
Args:
ctx (commands.Context): The context in which the command was run
"""
quote = await self.get_quote()
embed = self.generate_themed_embed(quote=quote)
await ctx.send(embed=embed)