nerve/commands.py

161 lines
3.7 KiB
Python
Raw Normal View History

2022-03-03 14:18:07 -05:00
import random
2022-05-18 03:59:32 -04:00
import features
2022-03-03 14:18:07 -05:00
jokes = [
["Testing","Testing you!"],
["singing pokemon","Jiggalypuff! 🎙️"]
]
#jokes = []
2022-05-18 03:59:32 -04:00
def handle_command(state,sender,message):
2022-03-03 14:18:07 -05:00
for command in commands:
2022-03-07 04:24:40 -05:00
if message.find(command)==0:
2022-05-18 03:59:32 -04:00
result = commands[command](state,message,sender)
2022-03-07 04:24:40 -05:00
return result
if len(state) > 0:
try:
2022-05-18 03:59:32 -04:00
result = commands[state[0]](state,message,sender)
2022-03-07 04:24:40 -05:00
return result
except Exception as e:
return {
"state":[],
"response":f"Error⚠ \n {e}"
}
return {
"response":None,
"state":state
}
2022-03-03 14:18:07 -05:00
"""
Knock-knock implimentation
when you receive message, read state.
user: knock knock
bot: who's there? -> bot responds and registers state
(user.state=[knock-knock])
user: {statement}
bot: {statement} who? -> bot responds and stores statement
(user.stage=[knock-knock,{statement}])
user: {punchline}
bot: Nice one! -> bot saves knock-knock joke
"""
2022-05-18 03:59:32 -04:00
def nevermind(state,message,sender):
2022-03-03 14:18:07 -05:00
return {
"response":"starting over.",
"state":[]
}
2022-05-18 03:59:32 -04:00
def knock_knock(state,message,sender):
2022-03-03 14:18:07 -05:00
#return {response:"",state:""}
try:
if state[0] != "knock knock":
#clear the state
state = []
except:
pass
if len(state) == 0:
state.append("knock knock")
return {
"response":"Who's there?",
"state": state
}
if len(state) == 1:
state.append(message)
return {
"response":message + " who?",
"state":state
}
if len(state) == 2:
state.append(message)
jokes.append([
state[1],state[2]
])
print("joke registered.")
return {
"response":"Hah! very funny!",
"state":[]
}
return {
"response":"Cleared.",
"state":[]
}
"""'joke' command
-------------
user:!joke
bot:Knock knock -> bot initializes state
(user.state=[joke])
user: Who's there?
bot: {statement} -> bot responds with chosen joke
(user.state=[joke,chosenjoke])
user: {statement} who?
bot:
"""
2022-05-18 03:59:32 -04:00
def joke(state,message,sender):
2022-03-03 14:18:07 -05:00
if len(jokes) == 0:
return {
"response":"I don't know any jokes...",
"state":[]
2022-03-07 04:24:40 -05:00
}
2022-03-03 14:18:07 -05:00
if len(state) == 0:
return {
"response":"Knock knock",
"state":["joke"]
}
2022-03-07 04:24:40 -05:00
elif len(state) == 1:
2022-03-03 14:18:07 -05:00
if message.find("there?") > 0 :
#get random joke index
state.append(random.randint(0,len(jokes)-1))
return {
"response":jokes[state[1]][0],
"state":state
}
else:
return {
"response":"ask, \"who's there?\"",
"state":state
}
2022-03-07 04:24:40 -05:00
elif len(state) == 2:
2022-03-03 14:18:07 -05:00
if message.find("who?") > 0:
return {
"response":jokes[state[1]][1],
"state":[]
}
else:
return {
"response":"ask, \""+ jokes[state[1]][0] +" who?\"",
"state":state
}
2022-03-07 04:24:40 -05:00
return {
"response":None,
"state":[]
}
2022-05-18 03:59:32 -04:00
"""
2022-03-03 14:18:07 -05:00
2022-05-18 03:59:32 -04:00
"""
def appointments():
return {
"response":None,
state:[]
}
2022-03-03 14:18:07 -05:00
commands = {
"nevermind":nevermind,
"knock knock":knock_knock,
"joke":joke
}
if __name__ == '__main__':
msg = ""
while msg != "exit":
msg = input("user:")
user = handle_command(user,msg)
#print("state:",user)