-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
63 lines (47 loc) · 1.54 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
from typing import Final
import os
from dotenv import load_dotenv
from discord import Intents, Client, Message
from responses import get_response
# STEP 0: LOAD OUR TOKEN FROM SOMEWHERE SAFE
load_dotenv()
TOKEN: Final = os.getenv("DISCORD_TOKEN")
# STEP 1: BOT SETUP
intents = Intents.default()
intents.message_content = True
client = Client(intents=intents)
# STEP 2: MESSAGE FUNTIONALITY
async def send_message(message: Message, user_message: str) -> None:
if not user_message:
print("(Message was empty because intents were not enabled, probably)")
return
if is_private := user_message[0] == "?":
user_message = user_message[1:]
try:
response = get_response(
user_message, str(message.author), message.author.mention, message.channel
)
await message.author.send(
response
) if is_private else await message.channel.send(response)
except Exception as e:
print(e)
# STEP 3: HANDLING THE STARTUP FOR OUR BOT
@client.event
async def on_ready() -> None:
print(f"{client.user} is now runing!")
# STEP 4: HANDLING INCOMING MESSAGES
@client.event
async def on_message(message) -> None:
if message.author == client.user:
return
username = str(message.author)
user_message = str(message.content)
channel = str(message.channel)
print(f'[{channel}] {username}: "{user_message}"')
await send_message(message, user_message)
# STEP 5: MAIN ENTRY POINT
def main():
client.run(token=TOKEN)
if __name__ == "__main__":
main()