-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoasts.py
165 lines (147 loc) · 4.93 KB
/
toasts.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
import disnake
from disnake.ext import commands
from config import Config
from os.path import exists
from sound import Sound
import requests
from pydub import AudioSegment
import logging
class ToastLanguage:
def __init__(
self,
name: str,
flag_emote: str,
default_toasts_text: str,
special_toasts_text: str, # 69
recipient_toasts_text: str,
gone_toasts_text: str,
):
self.name = name
self.flag_emote = flag_emote
self.default_toasts_text = default_toasts_text
self.special_toasts_text = special_toasts_text
self.recipient_toasts_text = recipient_toasts_text
self.gone_toasts_text = gone_toasts_text
languages = [
ToastLanguage(
"cs-CZ",
":flag_cz:",
"Toasty číslo",
"Toasty číslo 69 Naaajssss",
"Toasty pro ",
"Toasty došly.",
),
ToastLanguage(
"sk-SK",
":flag_sk:",
"Tousty číslo",
"Tousty číslo 69 Naaajssss",
"Tousty pre ",
"Tousty sú v piči.",
),
ToastLanguage(
"en-US",
":flag_us:",
"Toasts number",
"Toasts number 69 Nice",
"Toasts for",
"Toasts are out of stock.",
),
]
class Toasts(commands.Cog):
def __init__(
self,
bot
):
self.bot = bot
def get_language_names():
names = []
for lang in languages:
names.append(lang.name)
return names
async def autocomp_language_names(
self,
inter: disnake.ApplicationCommandInteraction,
user_input: str
):
return [
lang for lang in languages if user_input.lower() in lang.name
]
def get_language_by_name(self, name: str):
for lang in languages:
if lang.name == name:
return lang
return None
@commands.slash_command(description="Vyhlásí tousty.")
async def toasts(
self,
inter: disnake.ApplicationCommandInteraction,
number: int = commands.Param(
ge=0,
le=200,
name="number",
description="Číslo volaných toustů, 0 = došly",
default=69
),
recipient: str = commands.Param(
name="recipient",
description="Jméno příjemce volaných toustů v 2. pádě (Pro koho? Čeho?)",
default=None
),
lang: str = commands.Param(
name="language",
description="Jazyk pro vyhlášení toustů",
default="cs-CZ",
choices=get_language_names()
)
):
await inter.response.defer()
# Get language object
language = self.get_language_by_name(lang)
# Prepare variables for toasts declaring
text = ". "
if recipient is not None:
# Recipient way
logging.info(f"Going to declare toasts for \"{recipient}\" in \"{lang}\".")
full_filename = f"toasts-for-recipient-{number}.{lang}.wav"
number = -1
text += f"{language.recipient_toasts_text} {recipient}"
response_text = f":arrow_right: \"{recipient}\""
else:
# Number way
logging.info(f"Going to declare toasts {number} in \"{lang}\".")
full_filename = f"toasts-number-{number}.{lang}.wav"
text += f"{language.default_toasts_text} {number}"
response_text = str(number)
if number == 69:
text = f". {language.special_toasts_text}"
response_text = f"{number} <:nepSmug:827833315822141451>"
elif number == 0:
text = f". {language.gone_toasts_text}"
response_text = f"{language.gone_toasts_text} <:sadIvo:567039959257710592>"
# Add flag emote by language to response
response_text += f" {language.flag_emote}"
# If file is not in local directory create it
if not exists(full_filename):
q = requests.utils.quote(text)
request = requests.get(
f"https://translate.google.com/translate_tts?ie=UTF-8&tl={lang}&client=tw-ob&q={q}"
)
open(f"{Config.toasts_sounds_path}/{full_filename[:-4]}.mp3", "wb").write(request.content)
# convert wav to mp3
sound = AudioSegment.from_mp3(f"{Config.toasts_sounds_path}/{full_filename[:-4]}.mp3")
sound.export(f"{Config.toasts_sounds_path}/{full_filename}", format="wav")
# Define sound object
sound_to_play = Sound(
name=text,
files=[
f"{Config.toasts_sounds_path}/{full_filename}"
],
spotify=Config.spotify,
)
# Play the sound object
sound_to_play.play_fade_out()
# Send response message to channel
await inter.followup.send(
content=f":white_check_mark: :sandwich: {response_text}"
)