From d6eba39a082fab737980934c15d5e58b88819a56 Mon Sep 17 00:00:00 2001 From: Luke Robles Date: Wed, 5 Jul 2023 18:46:12 -0700 Subject: [PATCH] Adding markov back to the bot --- app/cogs/markov.py | 45 ++++++++++++++++++++++++++++++++++++++++++++ app/requirements.txt | 1 + 2 files changed, 46 insertions(+) create mode 100644 app/cogs/markov.py diff --git a/app/cogs/markov.py b/app/cogs/markov.py new file mode 100644 index 00000000..f7120399 --- /dev/null +++ b/app/cogs/markov.py @@ -0,0 +1,45 @@ +from discord.ext import commands +from discord import option +import discord +import markovify +import random + + +class Markov(commands.Cog): + def __init__(self, bot): + self.bot: commands.Bot = bot + + @commands.slash_command( + guild_ids=None, + name="markov", + description="Generate a markov chain based on a user in the server", + ) + @option("User", description="Choose a member", input_type="user") + async def markov(self, ctx: commands.Context, user: discord.Member): + await ctx.defer() + temp_message = await ctx.followup.send( + "Just a moment, generating autism" + ) + + # Get messages from passed in user + authors_mesages = [] + for message in await ctx.history(limit=20000).flatten(): + if message.author.id == user.id and "https" not in message.content: + authors_mesages.append(message.content) + + # Make the model + user_model = markovify.Text(". ".join(authors_mesages)) + model_json = user_model.to_json() + + dummy = [] + for i in range(random.randint(3, 9)): + dummy.append(str(user_model.make_sentence(max_words=25, tries=900))) + await temp_message.edit( + "Heres a markov chain based on %s's shitposts" % user.mention + + "\n\n" + + " ".join(dummy) + ) + + +def setup(bot): + bot.add_cog(Markov(bot)) diff --git a/app/requirements.txt b/app/requirements.txt index 72d66c4e..f95f2cd3 100755 --- a/app/requirements.txt +++ b/app/requirements.txt @@ -10,3 +10,4 @@ owotext pandas requests wolframalpha +markovify \ No newline at end of file