-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
128 lines (103 loc) · 3.87 KB
/
main.py
File metadata and controls
128 lines (103 loc) · 3.87 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
import os
import asyncio
from time import sleep
import discord
from discord.ext import commands
from dotenv import load_dotenv
from duck_orm.sql.condition import Condition
from gtts import gTTS
from database.db import database
from models.User import User
from models.Greeting import Greeting
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
ID_ADMIN = os.getenv("ID_ADMIN")
ID_ADMIN_2 = os.getenv("ID_ADMIN_2")
ID_ADMIN_3 = os.getenv("ID_ADMIN_3")
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="$", intents=intents)
language = "pt-br"
greeting_default = "Chegou o disco voador!"
greeting_cattle = "O gado chegou"
greeting_finin = "é o finas"
greeting_king = "O rei chegou pessoal. Abram espaço!"
voice_channel_conntected = None
channel = None
def record_audio(greeting: str):
"""
Method that perform record the audio with greeting of the client.
PARAMS
- greeting: `str` - The Greeting
"""
myobj = gTTS(text=greeting, lang=language, slow=False)
myobj.save("greeting.mp3")
@bot.event
async def on_voice_state_update(client, before, after):
try:
if client in after.channel.members:
roles_in_user = list(filter(lambda role: role.name == "GADO", client.roles))
greetings = await Greeting.find_all(
conditions=[Condition("id_user", "=", str(client.id))]
)
if roles_in_user:
record_audio(greeting_cattle)
elif greetings:
# TODO: User can have multiple greetings
# - Draw the greeting ?
greeting = greetings[0].greeting
record_audio(greeting)
else:
record_audio(greeting_default)
sleep(1.5)
voice_channel_conntected.play(
source=discord.FFmpegPCMAudio(
source="greeting.mp3",
executable="ffmpeg.exe",
)
)
except Exception as ex:
print(f"Ocorreu alguma coisinha: {ex}")
@bot.command(name="vem-aqui")
async def join(ctx, *, channel: discord.VoiceChannel):
global voice_channel_conntected
if ctx.voice_client is not None:
return await ctx.voice_client.move_to(channel)
voice_channel_conntected = await channel.connect()
@bot.command(name="salva-saudacao", help="Salvar a saudação dos reis")
async def save_greeting(client, greeting_user: str):
try:
if len(greeting_user) < 2:
await client.send("A saudação deve ter mais de 2 letras! :(")
else:
author = client.author
await User.save(User(id=str(author.id), name=author.name))
await Greeting.save(
Greeting(greeting=greeting_user, id_user=str(author.id))
)
msg = f"A saudação {greeting_user} foi salva, Sr(a). {author.name}"
await client.send(msg)
except Exception:
msg = "Aconteceu alguma coisa inesperada hein :(\nChama o SUPORTE"
await client.send(msg)
@bot.command(name="saudacao", help="Qual a saudação salva")
async def get_greeting(client):
try:
greetings = await Greeting.find_all(
conditions=[Condition("id_user", "=", str(client.author.id))]
)
greeting = greetings[0].greeting
await client.send(f"A sua Sr. {client.author.name} saudação salva é {greeting}")
except Exception:
msg = "Sem saudação encontrada. Tu tem quem adicionar antes Mula e eu não vou dizer qual o comando!"
await client.send(msg)
@bot.command(name="sai-daqui", help="Sair do canal de voz")
async def leave(client):
await client.voice_client.disconnect()
async def main():
await database.connect()
await User.create()
await Greeting.create()
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(main())
bot.run(TOKEN)