-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
fun.py
129 lines (112 loc) · 5.57 KB
/
fun.py
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
import asyncio
import os
import random
import sys
import aiohttp
import discord
import yaml
from discord.ext import commands
from discord.ext.commands import BucketType
if not os.path.isfile("config.yaml"):
sys.exit("'config.yaml' not found! Please add it and try again.")
else:
with open("config.yaml") as file:
config = yaml.load(file, Loader=yaml.FullLoader)
class Fun(commands.Cog, name="fun"):
def __init__(self, bot):
self.bot = bot
"""
Why 1 and 86400?
-> Because the user should be able to use the command *once* every *86400* seconds
Why BucketType.user?
-> Because the cool down only affects the current user, if you want other types of cool downs, here are they:
- BucketType.default for a global basis.
- BucketType.user for a per-user basis.
- BucketType.server for a per-server basis.
- BucketType.channel for a per-channel basis.
"""
@commands.command(name="dailyfact")
@commands.cooldown(1, 86400, BucketType.user)
async def dailyfact(self, context):
"""
Get a daily fact, command can only be ran once every day per user.
"""
# This will prevent your bot from stopping everything when doing a web request - see: https://discordpy.readthedocs.io/en/stable/faq.html#how-do-i-make-a-web-request
async with aiohttp.ClientSession() as session:
async with session.get("https://uselessfacts.jsph.pl/random.json?language=en") as request:
if request.status == 200:
data = await request.json()
embed = discord.Embed(description=data["text"], color=config["main_color"])
await context.send(embed=embed)
else:
embed = discord.Embed(
title="Error!",
description="There is something wrong with the API, please try again later",
color=config["error"]
)
await context.send(embed=embed)
# We need to reset the cool down since the user didn't got his daily fact.
self.dailyfact.reset_cooldown(context)
@commands.command(name="dick")
async def dick(self, context, member: discord.Member = None):
"""
Get the dick's length of a user or yourself.
"""
if not member:
member = context.author
length = random.randrange(15)
embed = discord.Embed(description=f"8{'=' * length}D", color=config["main_color"])
embed.set_author(name=f"{member.display_name}'s Dick", icon_url=member.avatar_url)
await context.send(embed=embed)
@commands.command(name="rps")
async def rock_paper_scissors(self, context):
choices = {
0: "rock",
1: "paper",
2: "scissors"
}
reactions = {
"🪨": 0,
"🧻": 1,
"✂": 2
}
embed = discord.Embed(title="Please choose", color=config["warning"])
embed.set_author(name=context.author.display_name, icon_url=context.author.avatar_url)
choose_message = await context.send(embed=embed)
for emoji in reactions:
await choose_message.add_reaction(emoji)
def check(reaction, user):
return user == context.message.author and str(reaction) in reactions
try:
reaction, user = await self.bot.wait_for("reaction_add", timeout=10, check=check)
user_choice_emote = reaction.emoji
user_choice_index = reactions[user_choice_emote]
bot_choice_emote = random.choice(list(reactions.keys()))
bot_choice_index = reactions[bot_choice_emote]
result_embed = discord.Embed(color=config["success"])
result_embed.set_author(name=context.author.display_name, icon_url=context.author.avatar_url)
await choose_message.clear_reactions()
if user_choice_index == bot_choice_index:
result_embed.description = f"**That's a draw!**\nYou've chosen {user_choice_emote} and I've chosen {bot_choice_emote}."
result_embed.colour = config["warning"]
elif user_choice_index == 0 and bot_choice_index == 2:
result_embed.description = f"**You won!**\nYou've chosen {user_choice_emote} and I've chosen {bot_choice_emote}."
result_embed.colour = config["success"]
elif user_choice_index == 1 and bot_choice_index == 0:
result_embed.description = f"**You won!**\nYou've chosen {user_choice_emote} and I've chosen {bot_choice_emote}."
result_embed.colour = config["success"]
elif user_choice_index == 2 and bot_choice_index == 1:
result_embed.description = f"**You won!**\nYou've chosen {user_choice_emote} and I've chosen {bot_choice_emote}."
result_embed.colour = config["success"]
else:
result_embed.description = f"**I won!**\nYou've chosen {user_choice_emote} and I've chosen {bot_choice_emote}."
result_embed.colour = config["error"]
await choose_message.add_reaction("🇱")
await choose_message.edit(embed=result_embed)
except asyncio.exceptions.TimeoutError:
await choose_message.clear_reactions()
timeout_embed = discord.Embed(title="Too late", color=config["error"])
timeout_embed.set_author(name=context.author.display_name, icon_url=context.author.avatar_url)
await choose_message.edit(embed=timeout_embed)
def setup(bot):
bot.add_cog(Fun(bot))