-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtruthdare.py
More file actions
202 lines (157 loc) · 7.66 KB
/
truthdare.py
File metadata and controls
202 lines (157 loc) · 7.66 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import random, discord, os, re
from typing import Tuple, Any
from pprint import pprint
from discord.ext import commands
from pathlib import Path
note_re = re.compile("\[.*?]")
small_re = re.compile("\(.*?\)")
def read_file_lines(file) -> []:
lines = []
with open(file) as f:
file_lines = f.readlines()
for line in file_lines:
# Remove all notes in the line
line = note_re.sub("", line)
line = line.strip()
if line.isspace():
continue
lines.append(line)
return lines
def read_folder_into_map(folder) -> {}:
ret = {}
for subdir, dirs, files in os.walk(folder):
for file in files:
ret[str(Path(file).with_suffix(""))] = read_file_lines(os.path.join(subdir, file))
return ret
truths = read_folder_into_map("truths")
dares = read_folder_into_map("dares")
truth_cats = [value for value in truths if value not in dares]
dare_cats = [value for value in dares if value not in truths]
both_cats = [value for value in list(truths.keys()) if value in list(dares.keys())]
# Pre-calculate truth or dare chances for no category and all categories
categories_truthordare_chance = {}
random_category_truthordare_chance = 0.5
total_count_truths = 0
total_count_dares = 0
for category in both_cats:
categories_truthordare_chance[category] = len(truths[category]) / (len(dares[category]) + len(truths[category]))
total_count_truths += len(truths[category])
total_count_dares += len(dares[category])
print(f"category {category} chance: {categories_truthordare_chance[category]}")
for category in truth_cats:
total_count_truths += len(truths[category])
for category in dare_cats:
total_count_dares += len(dares[category])
print(f"total number of truths: {total_count_truths}; total number of dares: {total_count_dares}")
random_category_truthordare_chance = total_count_truths / (total_count_dares + total_count_truths)
print(f"random category truth chance: {random_category_truthordare_chance}")
def generate_truth(category=None) -> tuple[str, str]:
if category is None:
category = random.choice(list(truths.keys()))
return random.choice(truths[category]), category
def generate_dare(category=None) -> tuple[str, str]:
if category is None:
category = random.choice(list(dares.keys()))
return random.choice(dares[category]), category
def build_embed(message, category, type, requestor, category_was_random) -> discord.Embed:
small_text = None
small_text_check = small_re.findall(message)
if len(small_text_check):
small_text = " ".join(small_text_check)
embed = discord.Embed(title=small_re.sub("", message).strip(), description=small_text)
embed.set_author(name=f"Requested by {requestor.name}", icon_url=requestor.avatar.url)
embed.set_footer(text=f"Type: {type} Category: {category}" + (" (Random)" if category_was_random else ""))
return embed
class TruthDareInteractions(discord.ui.View):
def __init__(self, *, timeout=None, category=None):
super().__init__(timeout=timeout)
self.category = category
async def disable_buttons(self, interaction: discord.Interaction) -> None:
for child in self.children:
child.disabled = True
await interaction.response.edit_message(view=self)
@discord.ui.button(label="Truth", style=discord.ButtonStyle.green)
async def truth_button(self, interaction: discord.Interaction, button: discord.ui.Button):
await self.disable_buttons(interaction)
message, category = generate_truth(self.category)
await interaction.followup.send(
embed=build_embed(message, category, "TRUTH", interaction.user, self.category is None),
view=TruthDareInteractions(category=self.category)
)
@discord.ui.button(label="Dare", style=discord.ButtonStyle.red)
async def dare_button(self, interaction: discord.Interaction, button: discord.ui.Button):
await self.disable_buttons(interaction)
message, category = generate_dare(self.category)
await interaction.followup.send(
embed=build_embed(message, category, "DARE", interaction.user, self.category is None),
view=TruthDareInteractions(category=self.category)
)
@discord.ui.button(label="Random", style=discord.ButtonStyle.blurple)
async def random_button(self, interaction: discord.Interaction, button: discord.ui.Button):
if self.category is None:
chance = random_category_truthordare_chance
else:
chance = categories_truthordare_chance[self.category]
if random.random() < chance:
chosen_generator = generate_truth
chosen_type = "TRUTH (RANDOM)"
else:
chosen_generator = generate_dare
chosen_type = "DARE (RANDOM)"
await self.disable_buttons(interaction)
message, category = chosen_generator(self.category)
await interaction.followup.send(
embed=build_embed(message, category, chosen_type, interaction.user, self.category is None),
view=TruthDareInteractions(category=self.category)
)
class TruthDareCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.hybrid_command()
async def truth(self, ctx, category=None):
if category is not None and category not in truths:
return await ctx.send(f"Invalid category: {category}", ephemeral=True)
message, this_category = generate_truth(category=category)
await ctx.send(
embed=build_embed(message, this_category, "TRUTH", ctx.interaction.user, category is None),
view=TruthDareInteractions(category=category)
)
@commands.hybrid_command()
async def dare(self, ctx, category=None):
if category is not None and category not in dares:
return await ctx.send(f"Invalid category: {category}", ephemeral=True)
message, this_category = generate_dare(category=category)
await ctx.send(
embed=build_embed(message, this_category, "DARE", ctx.interaction.user, category is None),
view=TruthDareInteractions(category=category)
)
@commands.hybrid_command()
async def random_choice(self, ctx, category=None):
if category is not None and (category not in truths or category not in dares):
return await ctx.send(f"Invalid category: {category} (For random truths or dares the category must have"
f"both truths and dares)", ephemeral=True)
if category is None:
chance = random_category_truthordare_chance
else:
chance = categories_truthordare_chance[category]
if random.random() < chance:
chosen_generator = generate_truth
chosen_type = "TRUTH (RANDOM)"
else:
chosen_generator = generate_dare
chosen_type = "DARE (RANDOM)"
message, this_category = chosen_generator(category=category)
await ctx.send(
embed=build_embed(message, this_category, chosen_type, ctx.interaction.user, category is None),
view=TruthDareInteractions(category=category)
)
@commands.hybrid_command()
async def truthdare_categories(self, ctx):
embed = discord.Embed(title="List of categories")
if len(both_cats):
embed.add_field(name="Both Truths and Dares", value=("> " + "\n> ".join(both_cats)))
if len(truth_cats):
embed.add_field(name="Only Truths", value=("> " + "\n> ".join(truth_cats)))
if len(dare_cats):
embed.add_field(name="Only Dares", value=("> " + "\n> ".join(dare_cats)))
await ctx.send(embed=embed)