6
6
import os
7
7
import random
8
8
import re
9
- from datetime import datetime
9
+ from datetime import datetime , time
10
10
from io import BytesIO
11
11
from random import randint
12
12
13
13
import aiohttp
14
14
import disnake
15
- from disnake .ext import commands
15
+ from disnake .ext import commands , tasks
16
16
17
17
import utils
18
18
from cogs .base import Base
19
19
from rubbergod import Rubbergod
20
20
from utils import cooldowns
21
21
from utils .errors import ApiError
22
22
23
+ from . import features
23
24
from .messages_cz import MessagesCZ
24
25
25
26
fuchs_path = "cogs/fun/fuchs/"
@@ -30,9 +31,12 @@ class Fun(Base, commands.Cog):
30
31
def __init__ (self , bot : Rubbergod ):
31
32
super ().__init__ ()
32
33
self .bot = bot
34
+ self .xkcd_url : str = "https://xkcd.com"
35
+ self .total_xkcd_posts : int = 0
33
36
34
- def custom_footer (self , author , url ) -> str :
35
- return f"📩 { author } | { url } • { datetime .now ().strftime ('%d.%m.%Y %H:%M' )} "
37
+ async def update_xkcd_posts (self ):
38
+ xkcd_post = await features .get_xkcd (self .bot .rubbergod_session , f"{ self .xkcd_url } /info.0.json" )
39
+ self .total_xkcd_posts = xkcd_post ["num" ]
36
40
37
41
async def get_image (self , inter , url ) -> tuple [BytesIO , str ]:
38
42
async with aiohttp .ClientSession () as session :
@@ -80,7 +84,7 @@ async def cat(self, inter: disnake.ApplicationCommandInteraction):
80
84
fact_response = await self .get_fact ("https://meowfacts.herokuapp.com/" , "data" )
81
85
82
86
image_embed = disnake .Embed (color = disnake .Color .blue ())
83
- image_embed .set_footer (text = self .custom_footer (inter .author , "thecatapi.com" ))
87
+ image_embed .set_footer (text = features .custom_footer (inter .author , "thecatapi.com" ))
84
88
image_embed .set_image (file = image_file )
85
89
embeds : list [disnake .Embed ] = [image_embed ]
86
90
@@ -90,7 +94,7 @@ async def cat(self, inter: disnake.ApplicationCommandInteraction):
90
94
description = fact_response ,
91
95
color = disnake .Color .blue (),
92
96
)
93
- fact_embed .set_footer (text = self .custom_footer (inter .author , "thecatapi.com" ))
97
+ fact_embed .set_footer (text = features .custom_footer (inter .author , "thecatapi.com" ))
94
98
embeds .append (fact_embed )
95
99
96
100
await inter .send (embeds = embeds )
@@ -109,7 +113,7 @@ async def dog(self, inter: disnake.ApplicationCommandInteraction):
109
113
fact_response = await self .get_fact ("https://dogapi.dog/api/facts/" , "facts" )
110
114
111
115
image_embed = disnake .Embed (color = disnake .Color .blue ())
112
- image_embed .set_footer (text = self .custom_footer (inter .author , "thedogapi.com" ))
116
+ image_embed .set_footer (text = features .custom_footer (inter .author , "thedogapi.com" ))
113
117
image_embed .set_image (file = image_file )
114
118
embeds : list [disnake .Embed ] = [image_embed ]
115
119
@@ -119,7 +123,7 @@ async def dog(self, inter: disnake.ApplicationCommandInteraction):
119
123
description = fact_response ,
120
124
color = disnake .Color .blue (),
121
125
)
122
- fact_embed .set_footer (text = self .custom_footer (inter .author , "thedogapi.com" ))
126
+ fact_embed .set_footer (text = features .custom_footer (inter .author , "thedogapi.com" ))
123
127
embeds .append (fact_embed )
124
128
125
129
await inter .send (embeds = embeds )
@@ -134,7 +138,7 @@ async def fox(self, inter: disnake.ApplicationCommandInteraction):
134
138
image_file = disnake .File (image_bytes , filename = file_name )
135
139
136
140
embed = disnake .Embed (color = disnake .Color .blue ())
137
- embed .set_footer (text = self .custom_footer (inter .author , "randomfox.ca" ))
141
+ embed .set_footer (text = features .custom_footer (inter .author , "randomfox.ca" ))
138
142
embed .set_image (file = image_file )
139
143
140
144
await inter .send (embed = embed )
@@ -149,7 +153,7 @@ async def duck(self, inter: disnake.ApplicationCommandInteraction):
149
153
image_file = disnake .File (image_bytes , filename = file_name )
150
154
151
155
embed = disnake .Embed (color = disnake .Color .blue ())
152
- embed .set_footer (text = self .custom_footer (inter .author , "random-d.uk" ))
156
+ embed .set_footer (text = features .custom_footer (inter .author , "random-d.uk" ))
153
157
embed .set_image (file = image_file )
154
158
155
159
await inter .send (embed = embed )
@@ -202,7 +206,7 @@ async def dadjoke(self, inter: disnake.ApplicationCommandInteraction, *, keyword
202
206
color = disnake .Color .blue (),
203
207
url = "https://icanhazdadjoke.com/j/" + result ["id" ],
204
208
)
205
- embed .set_footer (text = self .custom_footer (inter .author , "icanhazdadjoke.com" ))
209
+ embed .set_footer (text = features .custom_footer (inter .author , "icanhazdadjoke.com" ))
206
210
207
211
await inter .send (embed = embed )
208
212
@@ -224,7 +228,7 @@ async def yo_mamajoke(self, inter: disnake.ApplicationCommandInteraction):
224
228
color = disnake .Color .blue (),
225
229
url = "https://www.yomama-jokes.com" ,
226
230
)
227
- embed .set_footer (text = self .custom_footer (inter .author , "https://www.yomama-jokes.com/" ))
231
+ embed .set_footer (text = features .custom_footer (inter .author , "https://www.yomama-jokes.com/" ))
228
232
229
233
await inter .send (embed = embed )
230
234
@@ -256,3 +260,41 @@ async def fuchs(
256
260
257
261
with open (fuchs_path + str (index ) + ".png" , "rb" ) as fp :
258
262
await inter .send (embed = embed , file = disnake .File (fp = fp , filename = str (index ) + ".png" ))
263
+
264
+ @cooldowns .default_cooldown
265
+ @commands .slash_command (name = "xkcd" , description = MessagesCZ .xkcd_brief )
266
+ async def xkcd (
267
+ self ,
268
+ inter : disnake .ApplicationCommandInteraction ,
269
+ number : int = commands .Param (default = None , ge = 1 ),
270
+ latest : bool = False ,
271
+ ):
272
+ """Get random XKCD comic.
273
+ If `latest` is specified, get the latest comic.
274
+ If `number` is specified, get the comic with that number.
275
+ If `number` and `latest` is specified, get comic with specified number.
276
+ """
277
+ await inter .response .defer ()
278
+ if not self .total_xkcd_posts :
279
+ await self .update_xkcd_posts ()
280
+
281
+ if number :
282
+ url = f"{ self .xkcd_url } /{ number } "
283
+ elif latest :
284
+ url = f"{ self .xkcd_url } "
285
+ else :
286
+ number = random .randint (1 , self .total_xkcd_posts )
287
+ url = f"{ self .xkcd_url } /{ number } "
288
+
289
+ xkcd_post = await features .get_xkcd (self .bot .rubbergod_session , f"{ url } /info.0.json" )
290
+ embed = await features .create_xkcd_embed (xkcd_post , inter .author , url )
291
+ await inter .send (embed = embed )
292
+
293
+ @tasks .loop (time = time (12 , 0 , tzinfo = utils .general .get_local_zone ()))
294
+ async def xkcd_task_update (self ):
295
+ """Update number of xkcd comics every sunday"""
296
+ if datetime .today ().isoweekday () != 7 :
297
+ # update only on sunday
298
+ return
299
+
300
+ await self .update_xkcd_posts ()
0 commit comments