Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

posts and user apps #8

Merged
merged 4 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
33 changes: 0 additions & 33 deletions cogs/purgecog.py

This file was deleted.

56 changes: 0 additions & 56 deletions cogs/shutdowncog.py

This file was deleted.

140 changes: 29 additions & 111 deletions cogs/stashcog.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import discord
from discord import app_commands
from discord.ext import commands
from discord_webhook import DiscordWebhook

from util.const import *
from util.msgutil import *
from util.artstash import anydownload, anymkwebhook
from util.urlparser import cleanurl
from stash.stash_vxtwitter import vx_jsonget, tw_markdown
from posts.supported import anypost
from util.whook import threadhook_send
from util.urlparser import downloadpath


class StashCog(commands.Cog):
Expand All @@ -26,103 +25,28 @@ async def ctxdown(self, interaction: discord.Interaction, message: discord.Messa

await interaction.response.defer(ephemeral=True)

embed = await anydownload(message.content)
# FIXME: this needs to be reworked
embed = None
if embed is None:
embed = errorembed(
"The message must start with a valid link\n"
"Currently supported sites: twitter, e621, e926, pixiv, reddit"
)
embed = errorembed("This action is not supported yet")
await interaction.followup.send(embed=embed, ephemeral=True)

# bulk download downloadables from channel history
@app_commands.command(name="stash", description="bulk download old messages")
@app_commands.describe(count="amount of messages processed")
async def download_history(self, interaction: discord.Interaction, count: int):
if not await devcheck(interaction):
return

await interaction.response.defer(ephemeral=True)

dlcount = 0
async for message in interaction.channel.history(limit=count):
if message.author.bot:
continue
embed = await anydownload(message.content)
if embed is not None:
dlcount += 1
await message.add_reaction("🔽")

embed = discord.Embed(title=f"Downloaded {dlcount} posts", color=0x009AFE)
embed.set_footer(text=downloadpath)
await interaction.followup.send(embed=embed)

# save any file to stash
@app_commands.command(name="save", description="save any file to stash")
async def save_file(
self, interaction: discord.Interaction, file: discord.Attachment, filename: str
):
if not await devcheck(interaction):
return

await interaction.response.defer()

ext = file.filename.split(".")[-1]
filename += "." + ext

await file.save(downloadpath + filename)

embed = discord.Embed(title=f"File saved", color=0x009AFE)
embed.add_field(
name=filename,
value=f"{round(os.path.getsize(downloadpath + filename)/1024, 1)} KB",
)
embed.set_image(url=file.url)
embed.set_footer(text=downloadpath)

await interaction.followup.send(embed=embed, delete_after=(30 * 60))


# twitter markdown with extra steps
@app_commands.command(name="twitter", description="send twitter as markdown")
async def save_file(
@app_commands.command(name="impersonate", description="send twitter as markdown")
async def impersonate(
self, interaction: discord.Interaction, link: str, impersonate: bool = True
):
if not await devcheck(interaction):
return

await interaction.response.defer(ephemeral=True)

vxjson = vx_jsonget(cleanurl(link))
if vxjson is None:
await interaction.followup.send(embed=errorembed("Invalid link"), ephemeral=True)
return


webhook = DiscordWebhook(
url=webhookurl,
content="",
avatar_url=interaction.user.display_avatar.url if not impersonate else vxjson["user_profile_image_url"].replace("_normal", ""),
username=interaction.user.display_name if not impersonate else vxjson["user_screen_name"],
)


if impersonate:
webhook.content = f'[Brought to you by {interaction.user.display_name}](<{vxjson["tweetURL"]}>)'
else:
handle = "\@" + vxjson["user_screen_name"].replace("_", "\_")
webhook.content = f'{handle} on [Twitter](<{vxjson["tweetURL"]}>)'

for glink in vxjson["mediaURLs"]:
webhook.content += f' [{"-" if glink.split(".")[-1] == "png" else "~"}]({glink})'

# remove the last word (duplicate link)
webhook.content += "\n" + " ".join(vxjson["text"].split(" ")[:-1])


if interaction.channel.type == discord.ChannelType.public_thread:
webhook.thread_id = interaction.channel.id
post = anypost(link)
await post.fetch()

await threadhook_send(interaction.channel, self.bot, post.webhook_message(), post.webhook_username(), post.webhook_avatar())

webhook.execute()
await interaction.followup.send("✅", ephemeral=True)

@commands.Cog.listener()
Expand All @@ -133,33 +57,27 @@ async def on_message(self, message: discord.Message):

# auto download from #to-stash
if message.channel.id == tostash_chid:
embed = await anydownload(message.content)
if embed is not None:
await message.delete()
await message.channel.send(embed=embed, delete_after=(30 * 60))

# turn twitter and e6 links to better markdowns using webhooks
# now with thread support
elif (
message.channel.id == tomarkdown_chid
or message.channel.type == discord.ChannelType.public_thread
and message.channel.parent_id == tomarkdown_chid
):
firstlink = message.content.split(" ")[0]
post = anypost(firstlink)
if not post:
return

await post.fetch()
embed = post.download(downloadpath)

webhook = DiscordWebhook(
url=webhookurl,
content="",
avatar_url=message.author.display_avatar.url,
username=message.author.display_name,
)
await message.delete()
await message.channel.send(embed=embed, delete_after=(30 * 60))

if message.channel.type == discord.ChannelType.public_thread:
webhook.thread_id = message.channel.id
# embed to markdown
else:
firstlink = message.content.split(" ")[0]
post = anypost(firstlink)
if not post:
return

if await anymkwebhook(firstlink, webhook):
webhook.execute()
await message.delete()
await post.fetch()
await threadhook_send(message.channel, self.bot, post.webhook_message(True), message.author.display_name, message.author.display_avatar)
await message.delete()


async def setup(bot: commands.Bot) -> None:
Expand Down
61 changes: 59 additions & 2 deletions cogs/statuscog.py → cogs/utilcog.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from util.msgutil import devcheck


class StatusCog(commands.Cog):
class UtilCog(commands.Cog):
def __init__(self, bot: commands.Bot) -> None:
self.bot = bot
print("Loaded", __class__.__name__)
Expand Down Expand Up @@ -64,6 +64,63 @@ async def setstatus(self, interaction: discord.Interaction, status: app_commands

await interaction.response.send_message(content=content, ephemeral=True)

# purge her own messages
@app_commands.allowed_installs(guilds=True, users=True)
@app_commands.allowed_contexts(guilds=True, dms=True, private_channels=True)
@app_commands.command(name="purr", description="purge her own messages")
@app_commands.describe(limit="number of messages to fetch")
async def purge_self(self, interaction: discord.Interaction, limit: int):
if not await devcheck(interaction):
return

await interaction.response.defer()

deleted = await interaction.channel.purge(
limit=limit, check=lambda message: message.author.id == self.bot.user.id
)
await interaction.followup.send(
f"Purrged {len(deleted)} messages", ephemeral=True
)

# force shutdown
@app_commands.allowed_installs(guilds=True, users=True)
@app_commands.allowed_contexts(guilds=True, dms=True, private_channels=True)
@app_commands.command(name="panik", description="shut down the app")
async def panic(self, interaction: discord.Interaction):
if not await devcheck(interaction):
return
await interaction.response.send_message(view=Panik(self.bot), ephemeral=False)

async def setup(bot: commands.Bot) -> None:
await bot.add_cog(StatusCog(bot))
await bot.add_cog(UtilCog(bot))

# view for panik shutdown
class Panik(discord.ui.View):
bot: commands.Bot

def __init__(self, bot: commands.Bot):
Panik.bot = bot
super().__init__()

@discord.ui.button(emoji="✔", style=discord.ButtonStyle.green)
async def shutdown(
self, interaction: discord.Interaction, button: discord.ui.Button
):
if not await devcheck(interaction):
return
button.disabled = True
await interaction.response.edit_message(view=self)
await interaction.message.delete()

await Panik.bot.close()
await Panik.bot.http.close()
await Panik.bot.session.close()
Panik.bot.loop.close()

@discord.ui.button(emoji="✖", style=discord.ButtonStyle.red)
async def cancel(self, interaction: discord.Interaction, button: discord.ui.Button):
if not await devcheck(interaction):
return
button.disabled = True
await interaction.response.edit_message(view=self)
await interaction.message.delete()
31 changes: 31 additions & 0 deletions posts/esixpost.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from discord_webhook import DiscordWebhook
import e621
import os

from posts.post import Post, PostType

esix = e621.E621(("kapucni", os.getenv("E621TOKEN")))

class EsixPost(Post):
_platform = "E621"
_prefix = ""

async def fetch(self):
self._id = int(self._url.split("/")[4])
epost = esix.posts.get(self._id)
# remove conditional dnp from artists
if "conditional_dnp" in epost.tags.artist:
epost.tags.artist.remove("conditional_dnp")

# if it really has no artist
if epost.tags.artist == []:
epost.tags.artist.append("unknown")


self._author = epost.tags.artist[0]
self._author_icon = ""
self._media = [epost.file.url]

self._type = PostType.IMAGE if epost.file.ext in ["png", "jpg", "jpeg", "gif"] else PostType.VIDEO

await super().fetch()
Loading
Loading