dragon-bot/app/cogs/markov.py
Luke Robles e6c6bd5627
All checks were successful
Build and push / changes (push) Successful in 2s
Build and push / Lint-Python (push) Successful in 1s
Build and push / Build-and-Push-Docker (push) Successful in 13s
Build and push / sync-argocd-app (push) Successful in 2s
Build and push / post-failure-to-discord (push) Has been skipped
Build and push / post-success-to-discord (push) Successful in 2s
more fstrings
2025-05-07 15:39:50 -07:00

46 lines
1.4 KiB
Python
Executable File

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: discord.ApplicationContext, user: discord.Member):
await ctx.defer()
temp_message = await ctx.respond(
f"Generating a markov chain for {user.mention}..."
)
# 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))
user_model.to_json()
dummy = []
for i in range(random.randint(3, 9)):
dummy.append(str(user_model.make_sentence(max_words=100, tries=900)))
await temp_message.edit(
f"Heres a markov chain based on {user.mention}'s shitposts"
+ "\n\n"
+ " ".join(dummy)
)
def setup(bot):
bot.add_cog(Markov(bot))