made responses functions

This commit is contained in:
Honza 2024-08-07 17:43:15 +02:00
parent 5324f40205
commit f604d1dad8
3 changed files with 101 additions and 0 deletions

1
config_example/token.txt Normal file
View File

@ -0,0 +1 @@
just bots token :)

38
karel_main.py Normal file
View File

@ -0,0 +1,38 @@
import discord, random
from discord.ext import commands
import responses # type: ignore
prefix = "Karle "
client = commands.Bot(command_prefix=prefix, intents=discord.Intents.all())
#
#
############################################################################bot login
@client.event
async def on_ready():
print(f"[ONREADY ] logged in as: {client.user}")
#myloop.start()
#
#
############################################################################ on message event
@client.event
async def on_message(message):
#reply chance
if message.author != client.user and message.content.startswith(prefix) == False and random.randint(1,1) == random.randint(1,1):
odpoved = responses.response_generator(message.content)
if odpoved != None:
await message.reply(odpoved)
await client.process_commands(message)
#
#
############################################################################pomoc command "Karle pomoc"
@client.command()
async def pomoc(ctx):
await ctx.send("susik")
TOKEN = open("config/token.txt", 'r').read()
client.run(TOKEN)

62
responses.py Normal file
View File

@ -0,0 +1,62 @@
import random, os
def Response_type_determinator(response_type_dict, msg_content):
for type in response_type_dict.values():
for slovo in type.slova:
if type.fn_podminka(slovo, msg_content) == True:
return(random.choice(type.responses))
class Response():
def __init__(self, slova, podminka, responses):
self.slova = slova
self.podminka = podminka
self.responses = responses
def fn_podminka(self, slovo, msg_content):
if self.podminka == "obsahuje":
return(slovo in msg_content)
elif self.podminka == "konec":
return(msg_content.endswith(slovo))
elif self.podminka == "zacatek":
return(msg_content.startswith(slovo))
def get_response_types():
directory = os.walk("config/responses")
response_dict = {}
for paths in directory:
for path in paths[2]:
content = open(f"config/responses/{path}", "r").readlines()
slova = content[0].split(", ")
responses = content[1].split(", ")
path = path[:-4].split("_")
value = path[0]
podminka = path[1]
response_dict[value] = Response(slova, podminka, responses)
return(response_dict)
mista = Response(["kde", "where"], "zacatek", ["tady", "tam", "tam ne", "tamhle"])
cas = Response(["kdy", "v kolik", "when"], "zacatek", ["ted ne", "nikdy", "ve 12:00", "zitra", "dnes vecer", "v 9"])
response_types = [mista, cas]
def response_generator(msg_content):
moznosti = ["determinator"]
x = random.choice(moznosti)
if x == "determinator":
odpoved = Response_type_determinator(get_response_types(), msg_content)
else:
return(None)
return(odpoved)