-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
65 lines (46 loc) · 1.9 KB
/
bot.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
# Import the necessary modules
import discord as dc
import responses as res
import APIs_keys_url as aku
# Define an asynchronous function to send a message to the user
async def send_message(message, user_message, is_private):
try:
response = res.get_response(user_message)
if is_private:
await message.author.send(response)
else:
await message.channel.send(response)
except Exception as e:
print(e)
# Define the function to run the Discord bot
def run_discord_bot():
TOKEN = aku.token
intents = dc.Intents.default()
intents.message_content = True
client = dc.Client(intents=intents)
# Define an event handler for when the bot is ready and connected to the server
@client.event
async def on_ready():
print(f"{client.user} is now running!!!")
# Define an event handler for when a message is received
@client.event
async def on_message(message):
# Ignore messages sent by the bot itself to prevent infinite loops
if message.author == client.user:
return
username = str(message.author)
user_message = str(message.content)
channel = str(message.channel)
# Print the user's message and channel for debugging purposes
print(f'{username} said: "{user_message}" ({channel})')
# Check if the user's message starts with a question mark '?'
if user_message[0] == '?':
# Remove the question mark from the message
user_message = user_message[1:]
# Send the user's message as a private response
await send_message(message, user_message, is_private=True)
else:
# Send the user's message as a public response
await send_message(message, user_message, is_private=False)
# Run the bot with the provided token
client.run(TOKEN)