persistent knock-knock jokes

This commit is contained in:
Gabriel 2022-03-07 04:24:40 -05:00
parent baaee8a5f6
commit 5d04ccd27a
3 changed files with 66 additions and 44 deletions

View file

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

View file

@ -1,27 +1,27 @@
{ {
"username":"bot", "username": "bot",
"password":"hunter2", "password": "hunter2",
"homeserver":"http://localhost:8008", "homeserver": "http://localhost:8008",
"faq":{ "faq": {
"header":"# Simple example FAQ", "header": "# Simple example FAQ",
"questions":[ "questions": [{
{ "question": "# What is the meaning of life?",
"question":"# What is the meaning of life?", "answer": "42.",
"answer":"42.", "key_phrases": ["meaning", "life"]
"key_phrases":["meaning","life"]
}, },
{ {
"question":"# Who is the coolest pokemon?", "question": "# Who is the coolest pokemon?",
"answer":"Mewtwo! 🐈", "answer": "Mewtwo! 🐈",
"key_phrases":["coolest","pokemon"] "key_phrases": ["coolest", "pokemon"]
}, },
{ {
"question":"# What is the coolest programming language?", "question": "# What is the coolest programming language?",
"answer":"🐍 python!", "answer": "🐍 python!",
"key_phrases":["coolest","programming","language"] "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": {}
} }

24
main.py
View file

@ -5,6 +5,7 @@ import commands
data = json.loads(open('data.json').read()) data = json.loads(open('data.json').read())
dfaq = data["faq"] dfaq = data["faq"]
userstate = data['users']
faq_text = dfaq["header"] + "".join(["\n"+q["question"]+"\n"+q["answer"] faq_text = dfaq["header"] + "".join(["\n"+q["question"]+"\n"+q["answer"]
for q in dfaq["questions"]]) + "\n\n"+dfaq["footer"] for q in dfaq["questions"]]) + "\n\n"+dfaq["footer"]
@ -21,9 +22,31 @@ def load_faq():
dfaq = json.loads(open('data.json').read())["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"] 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 @bot.listener.on_message_event
async def faq(room, message): 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) 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( if match.is_not_from_this_bot() and match.prefix() and match.command(
"faq"): "faq"):
await bot.api.send_markdown_message( await bot.api.send_markdown_message(
@ -31,6 +54,7 @@ async def faq(room, message):
load_faq()) load_faq())
@bot.listener.on_custom_event(nio.InviteMemberEvent) @bot.listener.on_custom_event(nio.InviteMemberEvent)
async def example(room, event): async def example(room, event):
if event.membership == "join": if event.membership == "join":