import discord from discord.ext import commands, tasks import json import markovify import nltk from nltk.tokenize import word_tokenize import random import os import time import re from dotenv import load_dotenv import psutil load_dotenv() # download NLTK data files nltk.download('punkt') MEMORY_FILE = "memory.json" MEMORY_LOADED_FILE = "MEMORY_LOADED" MESSAGEAIID = int(os.getenv("MESSAGEAIID")) def load_memory(): data = [] # Try to load data from MEMORY_FILE try: with open(MEMORY_FILE, "r") as f: data = json.load(f) except FileNotFoundError: pass return data # Save memory data to MEMORY_FILE def save_memory(memory): with open(MEMORY_FILE, "w") as f: json.dump(memory, f, indent=4) def train_markov_model(memory, additional_data=None): if not memory: return None filtered_memory = [line for line in memory if isinstance(line, str)] if additional_data: filtered_memory.extend(line for line in additional_data if isinstance(line, str)) if not filtered_memory: return None text = "\n".join(filtered_memory) model = markovify.NewlineText(text, state_size=2) return model #this doesnt work and im extremely pissed and mad def append_mentions_to_18digit_integer(message): pattern = r'\b\d{18}\b' return re.sub(pattern, lambda match: f"<@{match.group(0)}>", message) def preprocess_message(message): message = append_mentions_to_18digit_integer(message) tokens = word_tokenize(message) tokens = [token for token in tokens if token.isalnum()] return " ".join(tokens) intents = discord.Intents.default() intents.messages = True intents.message_content = True bot = commands.Bot(command_prefix="g!", intents=intents) memory = load_memory() markov_model = train_markov_model(memory) generated_sentences = set() used_words = set() @bot.event async def on_ready(): print(f"Logged in as {bot.user}") post_message.start() @bot.command() async def mem(ctx): process = psutil.Process(os.getpid()) mem = process.memory_info().rss await ctx.send(f"Total memory used: {mem / 1024 / 1024:.2f} MB") positive_keywords = ["happy", "good", "great", "amazing", "awesome", "joy", "love", "fantastic", "positive", "cheerful", "victory", "favorite", "lmao", "lol", "xd", "XD", "xD", "Xd"] positive_gifs = [ "https://tenor.com/view/chill-guy-my-new-character-gif-2777893510283028272", "https://tenor.com/view/goodnight-goodnight-friends-weezer-weezer-goodnight-gif-7322052181075806988" ] def is_positive(sentence): sentence_lower = sentence.lower() return any(keyword in sentence_lower for keyword in positive_keywords) def improve_sentence_coherence(sentence): sentence = sentence.replace(" i ", " I ") return sentence def rephrase_for_coherence(sentence): words = sentence.split() coherent_sentence = " ".join(words) return coherent_sentence bot.help_command = None @bot.event async def on_message(message): global memory, markov_model, last_random_talk_time if message.author.bot and message.author.id != bot.user.id: if markov_model: response = None for _ in range(10): response = markov_model.make_sentence(tries=100) if response and response not in generated_sentences: response = improve_sentence_coherence(response) generated_sentences.add(response) break if response: async with message.channel.typing(): cleaned_response = re.sub(r'[^\w\s]', '', response).lower() coherent_response = rephrase_for_coherence(cleaned_response) if random.random() < 0.9: if is_positive(coherent_response): gif_url = random.choice(positive_gifs) combined_message = f"{coherent_response}\n[jif]({gif_url})" await message.channel.send(combined_message) else: await message.channel.send(coherent_response) else: await message.channel.send(coherent_response) else: await message.channel.send("I have nothing to say right now!") else: await message.channel.send("I need to learn more from messages before I can talk.") if message.content and message.author.id != bot.user.id : formatted_message = append_mentions_to_18digit_integer(message.content) cleaned_message = preprocess_message(formatted_message) if cleaned_message: memory.append(cleaned_message) save_memory(memory) markov_model = train_markov_model(memory) # Still process commands even from humans if message.content and not message.author.bot: await bot.process_commands(message) @bot.command() async def ping(ctx): await ctx.defer() #stolen from my expect bot very proud latency = round(bot.latency * 1000) LOLembed = discord.Embed( title="Pong!!", description=( f"The Beretta fires fast and won't make you feel any better!\n" f"`Bot Latency: {latency}ms`\n" ), color=discord.Color.blue() ) LOLembed.set_footer(text=f"Requested by {ctx.author.name}", icon_url=ctx.author.avatar.url) await ctx.send(embed=LOLembed) # use ctx.send instead of respond because it has nothing to respond to and its not a slash command @bot.command() async def show_memory(ctx): memory = load_memory() memory_text = json.dumps(memory, indent=4) if len(memory_text) > 1024: with open(MEMORY_FILE, "r") as f: await ctx.send(" ", file=discord.File(f, MEMORY_FILE)) else: embed = discord.Embed(title="Memory Contents", description="The bot's memory.", color=discord.Color.blue()) embed.add_field(name="Memory Data", value=f"```json\n{memory_text}\n```", inline=False) await ctx.send(embed=embed) def improve_sentence_coherence(sentence): sentence = sentence.replace(" i ", " I ") return sentence @tasks.loop(minutes=60) async def post_message(): channel_id = 1296141985253691433 channel = bot.get_channel(channel_id) if channel and markov_model: response = None for _ in range(10): response = markov_model.make_sentence(tries=100) if response and response not in generated_sentences: generated_sentences.add(response) break if response: await channel.send(response) # run the bot TOKEN = os.getenv("DISCORDBOTTOKEN", "0") bot.run(TOKEN)