hello am trying to get my discord bot to remeber what its learned after i shut it down but each time i boot it up again to test it forget everything its learned
heres the code
from discord.ext import commands
# Load intents from intents.json
with open('intents.json', 'r') as intents_file:
intents_data = json.load(intents_file)
intents = intents_data.get('intents', [])
# Load custom responses from custom_responses.json
with open('custom_responses.json', 'r') as custom_responses_file:
custom_responses = json.load(custom_responses_file)
except FileNotFoundError:
custom_responses = {} # Initialize an empty dictionary if the file doesn't exist
bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())
print(f'Logged in as {bot.user.name} ({bot.user.id})')
async def on_message(message):
return # Ignore messages from other bots
# Check for custom responses first
if message.content.lower() in custom_responses:
await message.channel.send(custom_responses[message.content.lower()])
# If no custom response, check predefined intents
if any(pattern.lower() in message.content.lower() for pattern in intent['patterns']):
response = random.choice(intent['responses'])
`your text` await message.channel.send(response)
await message.channel.send("I'm not sure how to respond. Could you provide a custom response?")
await bot.process_commands(message)
async def learn(ctx, *, response: str):
Command to add a custom response.
Usage: !learn <user_input> <custom_response>
Example: !learn favorite_color Blue
user_input, custom_response = response.split(maxsplit=1)
custom_responses[user_input.lower()] = custom_response
await ctx.send(f"Learned: '{user_input}' -> '{custom_response}'")
async def on_disconnect():
# Save custom responses to custom_responses.json when the bot disconnects
with open('custom_responses.json', 'w') as custom_responses_file:
json.dump(custom_responses, custom_responses_file, indent=4)
print("Bot is starting...")
loop = asyncio.get_event_loop()
loop.run_until_complete(bot.start('TOKEN'))
<code>import discord
from discord.ext import commands
import json
import asyncio
import random
# Load intents from intents.json
with open('intents.json', 'r') as intents_file:
intents_data = json.load(intents_file)
intents = intents_data.get('intents', [])
# Load custom responses from custom_responses.json
try:
with open('custom_responses.json', 'r') as custom_responses_file:
custom_responses = json.load(custom_responses_file)
except FileNotFoundError:
custom_responses = {} # Initialize an empty dictionary if the file doesn't exist
# Initialize the bot
bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name} ({bot.user.id})')
@bot.event
async def on_message(message):
if message.author.bot:
return # Ignore messages from other bots
# Check for custom responses first
if message.content.lower() in custom_responses:
await message.channel.send(custom_responses[message.content.lower()])
else:
# If no custom response, check predefined intents
for intent in intents:
if any(pattern.lower() in message.content.lower() for pattern in intent['patterns']):
response = random.choice(intent['responses'])
`your text` await message.channel.send(response)
break
else:
await message.channel.send("I'm not sure how to respond. Could you provide a custom response?")
await bot.process_commands(message)
@bot.command()
async def learn(ctx, *, response: str):
"""
Command to add a custom response.
Usage: !learn <user_input> <custom_response>
Example: !learn favorite_color Blue
"""
user_input, custom_response = response.split(maxsplit=1)
custom_responses[user_input.lower()] = custom_response
await ctx.send(f"Learned: '{user_input}' -> '{custom_response}'")
@bot.event
async def on_disconnect():
# Save custom responses to custom_responses.json when the bot disconnects
with open('custom_responses.json', 'w') as custom_responses_file:
json.dump(custom_responses, custom_responses_file, indent=4)
print("Bot is starting...")
loop = asyncio.get_event_loop()
loop.run_until_complete(bot.start('TOKEN'))
</code>
import discord
from discord.ext import commands
import json
import asyncio
import random
# Load intents from intents.json
with open('intents.json', 'r') as intents_file:
intents_data = json.load(intents_file)
intents = intents_data.get('intents', [])
# Load custom responses from custom_responses.json
try:
with open('custom_responses.json', 'r') as custom_responses_file:
custom_responses = json.load(custom_responses_file)
except FileNotFoundError:
custom_responses = {} # Initialize an empty dictionary if the file doesn't exist
# Initialize the bot
bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name} ({bot.user.id})')
@bot.event
async def on_message(message):
if message.author.bot:
return # Ignore messages from other bots
# Check for custom responses first
if message.content.lower() in custom_responses:
await message.channel.send(custom_responses[message.content.lower()])
else:
# If no custom response, check predefined intents
for intent in intents:
if any(pattern.lower() in message.content.lower() for pattern in intent['patterns']):
response = random.choice(intent['responses'])
`your text` await message.channel.send(response)
break
else:
await message.channel.send("I'm not sure how to respond. Could you provide a custom response?")
await bot.process_commands(message)
@bot.command()
async def learn(ctx, *, response: str):
"""
Command to add a custom response.
Usage: !learn <user_input> <custom_response>
Example: !learn favorite_color Blue
"""
user_input, custom_response = response.split(maxsplit=1)
custom_responses[user_input.lower()] = custom_response
await ctx.send(f"Learned: '{user_input}' -> '{custom_response}'")
@bot.event
async def on_disconnect():
# Save custom responses to custom_responses.json when the bot disconnects
with open('custom_responses.json', 'w') as custom_responses_file:
json.dump(custom_responses, custom_responses_file, indent=4)
print("Bot is starting...")
loop = asyncio.get_event_loop()
loop.run_until_complete(bot.start('TOKEN'))
then you got the custom_responses.json
{
“user_input_1”: “custom_response_1”,
“user_input_2”: “custom_response_2”
}
then the intents.json
{
“intents”: [
"https://www.youtube.com/watch?v=dQw4w9WgXcQ&pp=ygUXbmV2ZXIgZ29ubmEgZ2l2ZSB5b3UgdXA%3D"
"Hi there, how can I help?"
<code> {
"tag": "google",
"patterns": [
"google",
"search",
"internet"
],
"responses": [
"https://www.youtube.com/watch?v=dQw4w9WgXcQ&pp=ygUXbmV2ZXIgZ29ubmEgZ2l2ZSB5b3UgdXA%3D"
]
},
{
"tag": "greeting",
"patterns": [
"Hi there",
"How are you",
"Is anyone there?",
"Hey",
"Hola",
"Hello",
"Good day",
"Namaste",
"yo"
],
"responses": [
"Hello",
"Good to see you again",
"Hi there, how can I help?"
],
"context": [
""
]
}
</code>
{
"tag": "google",
"patterns": [
"google",
"search",
"internet"
],
"responses": [
"https://www.youtube.com/watch?v=dQw4w9WgXcQ&pp=ygUXbmV2ZXIgZ29ubmEgZ2l2ZSB5b3UgdXA%3D"
]
},
{
"tag": "greeting",
"patterns": [
"Hi there",
"How are you",
"Is anyone there?",
"Hey",
"Hola",
"Hello",
"Good day",
"Namaste",
"yo"
],
"responses": [
"Hello",
"Good to see you again",
"Hi there, how can I help?"
],
"context": [
""
]
}
etc
any help would be helpful to me thanks you for you time as well
i want it to remember what you tell it like if you say !learn cat cat is good next time you boot it you say cat i want it to say cat is good