Skip to content

Commit e321b6a

Browse files
author
Federico
committed
Created a discord bot
1 parent 913d0a8 commit e321b6a

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"hello": "Hey there!",
3+
"how are you?": "I am good, thanks!",
4+
"do you know what the time is?": "Not at all!",
5+
"what time is it?": "No clue!",
6+
"what can you do?": "I can answer questions!",
7+
"ok": "Great."
8+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from discord import Intents, Client
2+
3+
import responses
4+
5+
6+
def run_bot(token: str):
7+
"""Run our Discord Bot with the token provided"""
8+
9+
# Basic setup
10+
intents = Intents.default()
11+
intents.message_content = True
12+
client = Client(intents=intents)
13+
knowledge: dict = responses.load_knowledge('knowledge.json')
14+
15+
@client.event
16+
async def on_ready():
17+
"""Print a startup message for our bot when it goes online."""
18+
19+
print(f'{client.user} is now running!')
20+
21+
@client.event
22+
async def on_message(message):
23+
"""Handle incoming messages from our server."""
24+
25+
# client.user is the bot, the bot shouldn't respond to itself
26+
if message.author == client.user:
27+
return
28+
29+
# If the user wrote something, send a response to the channel
30+
if message.content:
31+
print(f'({message.channel}) {message.author}: "{message.content}"')
32+
response: str = responses.get_response(message.content, knowledge=knowledge)
33+
await message.channel.send(response)
34+
else:
35+
print('!!!Could not read the message, make sure you enabled intents!!!')
36+
37+
# Run the bot with our token
38+
client.run(token=token)
39+
40+
41+
if __name__ == '__main__':
42+
run_bot(token='YOUR_KEY')
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from difflib import get_close_matches
2+
import json
3+
4+
5+
def get_best_match(user_question: str, questions: dict) -> str | None:
6+
"""Compares the user message similarity to the ones in the dictionary"""
7+
8+
questions: list[str] = [q for q in questions]
9+
matches: list = get_close_matches(user_question, questions, n=1, cutoff=0.6)
10+
11+
# Return the first best match, else return None
12+
if matches:
13+
return matches[0]
14+
15+
16+
def get_response(message: str, knowledge: dict) -> str:
17+
while True:
18+
# Finds the best match, otherwise returns None
19+
best_match: str | None = get_best_match(message, knowledge)
20+
21+
# Gets the best match from the knowledge base
22+
if answer := knowledge.get(best_match):
23+
return answer
24+
else:
25+
return 'I don\'t understand... Could you try rephrasing that?'
26+
27+
28+
def load_knowledge(file: str) -> dict:
29+
with open(file, 'r') as f:
30+
return json.load(f)
31+
32+
33+
# if __name__ == "__main__":
34+
# test_knowledge: dict = load_knowledge('knowledge.json')
35+
# test_response: str = get_response('hello', knowledge=test_knowledge)
36+
# print(test_response)
37+

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,5 @@ beautifulsoup4~=4.12.2
5858
pypdf2~=3.0.1
5959
pygame~=2.4.0
6060
customtkinter~=5.1.3
61-
geopy~=2.3.0
61+
geopy~=2.3.0
62+
discord~=2.3.0

0 commit comments

Comments
 (0)