-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
57 lines (43 loc) · 1.48 KB
/
main.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
from typing import Final
import os
from dotenv import load_dotenv
from discord import Intents, Client, Message
from responses import get_response
# 1. Asegurar el TOKEN de cualquien lado
load_dotenv()
TOKEN: Final[str] = os.getenv('DISCORD_TOKEN')
# 2. Configuracion del Bot
intents = Intents = Intents.default()
intents.message_content = True # NOQA
client: Client = Client(intents=intents)
# 3. Funcionalidad de los mensajes
async def send_message(message: Message, user_message: str) -> None:
if not user_message:
print('(El mensaje estaba vacio porque no estaban activados los intents)')
return
if is_private := user_message[0] == '?':
user_message = user_message[1:]
try:
response: str = get_response(user_message)
await message.author.send(response) if is_private else await message.channel.send(response)
except Exception as e:
print(e)
# 4. Lanzamiento del Bot
@client.event
async def on_ready() -> None:
print(f'{client.user} is now running!')
# 5. Manejo de mensajes entrantes
@client.event
async def on_message(message: Message) -> None:
if message.author == client.user:
return
username: str = str(message.author)
user_message: str = message.content
channel: str = str(message.channel)
print(f'[{channel}] {username}: "{user_message}"')
await send_message(message, user_message)
# 6. Uso del TOKEN
def main() -> None:
client.run(token=TOKEN)
if __name__ == '__main__':
main()