persistent knock-knock jokes

main
Gabriel 2 years ago
parent baaee8a5f6
commit 5d04ccd27a

@ -1,6 +1,4 @@
import random
user = []
jokes = [
["Testing","Testing you!"],
["singing pokemon","Jiggalypuff! 🎙️"]
@ -9,21 +7,23 @@ jokes = [
def handle_command(state,message):
for command in commands:
if command in message:
result = commands[command](state,message)
print("Bot: "+result["response"])
return result["state"]
else:
if len(user) > 0:
try:
result = commands[user[0]](state,message)
print("Bot: "+result["response"])
return result["state"]
except:
print(e)
print("Error saving state:",message)
pass
return state
if message.find(command)==0:
result = commands[command](state,message)
return result
if len(state) > 0:
try:
result = commands[state[0]](state,message)
return result
except Exception as e:
return {
"state":[],
"response":f"Error⚠ \n {e}"
}
return {
"response":None,
"state":state
}
"""
Knock-knock implimentation
@ -98,19 +98,14 @@ def joke(state,message):
return {
"response":"I don't know any jokes...",
"state":[]
}
try:
if state[0] != "joke":
state = []
except:
pass
}
if len(state) == 0:
return {
"response":"Knock knock",
"state":["joke"]
}
if len(state) == 1:
elif len(state) == 1:
if message.find("there?") > 0 :
#get random joke index
state.append(random.randint(0,len(jokes)-1))
@ -123,7 +118,7 @@ def joke(state,message):
"response":"ask, \"who's there?\"",
"state":state
}
if len(state) == 2:
elif len(state) == 2:
if message.find("who?") > 0:
return {
"response":jokes[state[1]][1],
@ -135,7 +130,10 @@ def joke(state,message):
"state":state
}
return state
return {
"response":None,
"state":[]
}
commands = {

@ -1,27 +1,27 @@
{
"username":"bot",
"password":"hunter2",
"homeserver":"http://localhost:8008",
"faq":{
"header":"# Simple example FAQ",
"questions":[
{
"question":"# What is the meaning of life?",
"answer":"42.",
"key_phrases":["meaning","life"]
"username": "bot",
"password": "hunter2",
"homeserver": "http://localhost:8008",
"faq": {
"header": "# Simple example FAQ",
"questions": [{
"question": "# What is the meaning of life?",
"answer": "42.",
"key_phrases": ["meaning", "life"]
},
{
"question":"# Who is the coolest pokemon?",
"answer":"Mewtwo! 🐈",
"key_phrases":["coolest","pokemon"]
"question": "# Who is the coolest pokemon?",
"answer": "Mewtwo! 🐈",
"key_phrases": ["coolest", "pokemon"]
},
{
"question":"# What is the coolest programming language?",
"answer":"🐍 python!",
"key_phrases":["coolest","programming","language"]
"question": "# What is the coolest programming language?",
"answer": "🐍 python!",
"key_phrases": ["coolest", "programming", "language"]
}
],
"footer":"Brought to you by Nerve\nSee the [code](https://codeberg.org/gabe/Nerve)"
"footer": "Brought to you by Nerve\nSee the [code](https://codeberg.org/gabe/Nerve)"
}
},
"users": {}
}

@ -5,6 +5,7 @@ import commands
data = json.loads(open('data.json').read())
dfaq = data["faq"]
userstate = data['users']
faq_text = dfaq["header"] + "".join(["\n"+q["question"]+"\n"+q["answer"]
for q in dfaq["questions"]]) + "\n\n"+dfaq["footer"]
@ -21,9 +22,31 @@ def load_faq():
dfaq = json.loads(open('data.json').read())["faq"]
return dfaq["header"] + "".join(["\n"+q["question"]+"\n"+q["answer"] for q in dfaq["questions"]]) + "\n\n"+dfaq["footer"]
def save_userstate():
with open('data.json','w') as f:
f.write(json.dumps(data,indent=4))
@bot.listener.on_message_event
async def faq(room, message):
if message.sender not in userstate:
userstate[message.sender] = {"state":[]}
save_userstate()
state = userstate[message.sender]['state']
result = commands.handle_command(state,message.body)
if result['response']!= None:
state = result['state']
userstate[message.sender]['state'] = result['state']
save_userstate()
await bot.api.send_markdown_message(
room.room_id,
result['response']
)
match = botlib.MessageMatch(room, message, bot, PREFIX)
#print(f"{message.sender} : {message.body}")
if match.is_not_from_this_bot() and match.prefix() and match.command(
"faq"):
await bot.api.send_markdown_message(
@ -31,6 +54,7 @@ async def faq(room, message):
load_faq())
@bot.listener.on_custom_event(nio.InviteMemberEvent)
async def example(room, event):
if event.membership == "join":

Loading…
Cancel
Save