1
|
|
|
from random import randint |
2
|
|
|
from typing import Tuple |
3
|
|
|
|
4
|
|
|
import aiohttp |
5
|
|
|
|
6
|
|
|
from pincer import Client, command |
7
|
|
|
from pincer.exceptions import CommandCooldownError |
8
|
|
|
from pincer.objects import Embed, MessageContext, Message, InteractionFlags |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class Bot(Client): |
12
|
|
|
MEME_URL = "https://some-random-api.ml/meme" |
13
|
|
|
|
14
|
|
|
@staticmethod |
15
|
|
|
def random_color(): |
16
|
|
|
return int(hex(randint(0, 16581375)), 0) |
17
|
|
|
|
18
|
|
|
async def get_meme(self) -> Tuple[str, str]: |
19
|
|
|
async with aiohttp.ClientSession() as session: |
20
|
|
|
async with session.get(self.MEME_URL) as response: |
21
|
|
|
data = await response.json() |
22
|
|
|
|
23
|
|
|
caption = data.get("caption", "Oops, something went wrong!") |
24
|
|
|
image = data.get("image") |
25
|
|
|
return caption, image |
26
|
|
|
|
27
|
|
|
@Client.event |
28
|
|
|
async def on_ready(self): |
29
|
|
|
print("Successfully created discord client on", self.bot) |
30
|
|
|
|
31
|
|
|
@command( |
32
|
|
|
cooldown=1, |
33
|
|
|
cooldown_scale=3 |
34
|
|
|
) |
35
|
|
|
async def meme(self): |
36
|
|
|
caption, image = await self.get_meme() |
37
|
|
|
|
38
|
|
|
return Embed(caption, color=self.random_color()) \ |
39
|
|
|
.set_image(image) \ |
40
|
|
|
.set_footer("Provided by some-random-api.ml", |
41
|
|
|
"https://i.some-random-api.ml/logo.png") |
42
|
|
|
|
43
|
|
|
@Client.event |
44
|
|
|
async def on_command_error(self, ctx: MessageContext, error: Exception): |
45
|
|
|
if isinstance(error, CommandCooldownError): |
46
|
|
|
return Message( |
47
|
|
|
embeds=[ |
48
|
|
|
Embed( |
49
|
|
|
"Oops...", |
50
|
|
|
f"The `{ctx.command.app.name}` command can only be used" |
51
|
|
|
f" `{ctx.command.cooldown}` time*(s)* every " |
52
|
|
|
f"`{ctx.command.cooldown_scale}` second*(s)*!", |
53
|
|
|
self.random_color() |
54
|
|
|
) |
55
|
|
|
], |
56
|
|
|
flags=InteractionFlags.EPHEMERAL |
57
|
|
|
) |
58
|
|
|
raise error |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
if __name__ == "__main__": |
62
|
|
|
Bot("XXXYOURBOTTOKENHEREXXX").run() |
63
|
|
|
|