-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
65 lines (48 loc) · 1.58 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
57
58
59
60
61
62
63
64
65
from typing import Final
import os
from dotenv import load_dotenv
from discord import Intents, Client, Message
from responses import get_response
# Load environment variables (.env file) -> dc_token
load_dotenv()
TOKEN: Final[str] = os.getenv("DISCORD_TOKEN")
# Bot setup (intents)
intents: Intents = Intents.default()
intents.message_content = True # NOQA
client: Client = Client(intents=intents)
# Message Functionality
async def send_message(message: Message, user_message: str) -> None:
if not user_message:
print("Message was empty because intents were not enabled properly")
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)
# handling the starting of the bot
@client.event
async def on_ready() -> None:
print(f"{client.user} has connected to Discord!")
# handling the incoming messages
@client.event
async def on_message(message: Message) -> None:
if message.author == client.user:
return
username: str = str(message.author).split("#")[0]
user_message: str = message.content
channel: str = str(message.channel)
print(f"[{channel}] {username}: {user_message}")
await send_message(message, user_message)
# Run the bot with the token
def main() -> None:
client.run(token=TOKEN)
if __name__ == "__main__":
main()
print(get_response("test"))