dragon-bot/app/cogs/markov.py
2022-09-18 20:59:38 -07:00

45 lines
1.2 KiB
Python

from discord.ext import commands
import core_utils
import discord
import markovify
class Markov(commands.Cog):
def __init__(self, bot):
self.bot: commands.Bot = bot
@commands.command(name="markov")
async def markov(self, ctx: commands.Context, user: discord.Member):
# 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
and message.content[0] != "!"
):
authors_mesages.append(message.content)
# Make the model
user_model = markovify.Text(". ".join(authors_mesages))
model_json = user_model.to_json()
# f = open("/tmp/model.json", "a")
# f.write(model_json)
# f.close()
dummy = []
for i in range(10):
dummy.append(str(user_model.make_sentence(max_words=25, tries=100)))
await ctx.send(
"Heres a markov chain based on %s's shitposts"
+ user.mention
+ "\n\n"
+ " ".join(dummy)
)
def setup(bot):
bot.add_cog(Markov(bot))