This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
forked from kkrypt0nn/Python-Discord-Bot-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfun-normal.py
175 lines (149 loc) · 6.29 KB
/
fun-normal.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
""""
Copyright © Krypton 2022 - https://github.com/kkrypt0nn (https://krypton.ninja)
Description:
This is a template to create your own discord bot in python.
Version: 4.1
"""
import random
import aiohttp
import disnake
from disnake.ext import commands
from disnake.ext.commands import Context
from helpers import checks
class Choice(disnake.ui.View):
def __init__(self):
super().__init__()
self.choice = None
@disnake.ui.button(label="Heads", style=disnake.ButtonStyle.blurple)
async def confirm(self, button: disnake.ui.Button, interaction: disnake.MessageInteraction):
self.choice = button.label.lower()
self.stop()
@disnake.ui.button(label="Tails", style=disnake.ButtonStyle.blurple)
async def cancel(self, button: disnake.ui.Button, interaction: disnake.MessageInteraction):
self.choice = button.label.lower()
self.stop()
class RockPaperScissors(disnake.ui.Select):
def __init__(self):
options = [
disnake.SelectOption(
label="Scissors", description="You choose scissors.", emoji="🪨"
),
disnake.SelectOption(
label="Rock", description="You choose rock.", emoji="🧻"
),
disnake.SelectOption(
label="paper", description="You choose paper.", emoji="✂"
),
]
super().__init__(
placeholder="Choose...",
min_values=1,
max_values=1,
options=options,
)
async def callback(self, interaction: disnake.MessageInteraction):
choices = {
"rock": 0,
"paper": 1,
"scissors": 2,
}
user_choice = self.values[0].lower()
user_choice_index = choices[user_choice]
bot_choice = random.choice(list(choices.keys()))
bot_choice_index = choices[bot_choice]
result_embed = disnake.Embed(color=0x9C84EF)
result_embed.set_author(name=interaction.author.display_name, icon_url=interaction.author.avatar.url)
if user_choice_index == bot_choice_index:
result_embed.description = f"**That's a draw!**\nYou've chosen {user_choice} and I've chosen {bot_choice}."
result_embed.colour = 0xF59E42
elif user_choice_index == 0 and bot_choice_index == 2:
result_embed.description = f"**You won!**\nYou've chosen {user_choice} and I've chosen {bot_choice}."
result_embed.colour = 0x9C84EF
elif user_choice_index == 1 and bot_choice_index == 0:
result_embed.description = f"**You won!**\nYou've chosen {user_choice} and I've chosen {bot_choice}."
result_embed.colour = 0x9C84EF
elif user_choice_index == 2 and bot_choice_index == 1:
result_embed.description = f"**You won!**\nYou've chosen {user_choice} and I've chosen {bot_choice}."
result_embed.colour = 0x9C84EF
else:
result_embed.description = f"**I won!**\nYou've chosen {user_choice} and I've chosen {bot_choice}."
result_embed.colour = 0xE02B2B
await interaction.response.defer()
await interaction.edit_original_message(embed=result_embed, content=None, view=None)
class RockPaperScissorsView(disnake.ui.View):
def __init__(self):
super().__init__()
self.add_item(RockPaperScissors())
class Fun(commands.Cog, name="fun-normal"):
def __init__(self, bot):
self.bot = bot
@commands.command(
name="randomfact",
description="Get a random fact."
)
@checks.not_blacklisted()
async def randomfact(self, context: Context):
"""
Get a random fact.
:param context: The context in which the command has been executed.
"""
# 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 = disnake.Embed(
description=data["text"],
color=0xD75BF4
)
else:
embed = disnake.Embed(
title="Error!",
description="There is something wrong with the API, please try again later",
color=0xE02B2B
)
await context.send(embed=embed)
@commands.command(
name="coinflip",
description="Make a coin flip, but give your bet before."
)
@checks.not_blacklisted()
async def coinflip(self, context: Context) -> None:
"""
Make a coin flip, but give your bet before.
:param context: The context in which the command has been executed.
"""
buttons = Choice()
embed = disnake.Embed(
description="What is your bet?",
color=0x9C84EF
)
message = await context.send(embed=embed, view=buttons)
await buttons.wait() # We wait for the user to click a button.
result = random.choice(["heads", "tails"])
if buttons.choice == result:
# User guessed correctly
embed = disnake.Embed(
description=f"Correct! You guessed `{buttons.choice}` and I flipped the coin to `{result}`.",
color=0x9C84EF
)
else:
embed = disnake.Embed(
description=f"Woops! You guessed `{buttons.choice}` and I flipped the coin to `{result}`, better luck next time!",
color=0xE02B2B
)
await message.edit(embed=embed, view=None)
@commands.command(
name="rps",
description="Play the rock paper scissors against the bot."
)
@checks.not_blacklisted()
async def rock_paper_scissors(self, context: Context) -> None:
"""
Play the rock paper scissors game against the bot.
:param context: The context in which the command has been executed.
"""
view = RockPaperScissorsView()
await context.send("Please make your choice", view=view)
def setup(bot):
bot.add_cog(Fun(bot))