insert2-cogs/wordpress.py
2024-08-11 15:14:26 -04:00

106 lines
No EOL
3.6 KiB
Python

import nextcord
from nextcord.ext import commands
from nextcord import TextInputStyle
from dotenv import load_dotenv
from random import randint
import aiosqlite as sqlite3
import requests
import json
import traceback
import sys
from datetime import datetime
import base64
from urllib.parse import urlparse
import os
class BlogModal(nextcord.ui.Modal):
def __init__(self,endpoint,credentials):
self.endpoint = endpoint
self.credentials = credentials
super().__init__(
f"create a post"
)
self.name = nextcord.ui.TextInput(
label="Title",
placeholder="Why pasta is better than bread",
style=TextInputStyle.short,
max_length=100,
)
self.add_item(self.name)
self.author = nextcord.ui.TextInput(
label="Author",
placeholder="insertapp",
style=TextInputStyle.short,
max_length=32,
)
self.add_item(self.author)
self.catergory = nextcord.ui.TextInput(
label="catergory id, if you don't know put 1",
placeholder="1",
style=TextInputStyle.short,
max_length=2,
)
self.add_item(self.catergory)
self.content = nextcord.ui.TextInput(
label="content",
placeholder="words...",
style=TextInputStyle.paragraph,
max_length=4000,
)
self.add_item(self.content)
self.contenttwo = nextcord.ui.TextInput(
label="contenttwo",
placeholder="optional more words...",
required = False,
style=TextInputStyle.paragraph,
max_length=4000,
)
self.add_item(self.contenttwo)
async def callback(self, interaction: nextcord.Interaction) -> None:
token = base64.b64encode(self.credentials.encode())
dt = datetime.now()
date = dt.isoformat()
fromatedtitle = f"{self.name.value} by {self.author.value}"
formatedcontent = str(self.content.value)+" " + str(self.contenttwo.value)
catergory = int(self.catergory.value)
header = {'Authorization': 'Basic ' + token.decode('utf-8')}
post = {
'title' : fromatedtitle,
'status' : 'pending',
'content' : formatedcontent,
'categories': catergory,
'date' : date
}
responce = requests.post(self.endpoint, headers=header, json=post)
await interaction.response.send_message(f"<@666378959184855042> someone posted to the blog, you should probably approve it {responce.status_code}")
async def on_error(self, error: nextcord.DiscordException, interaction: nextcord.Interaction):
error = traceback.format_exc()
print(error, file=sys.stderr)
message = f"```py\n{error[-1800:]}\n```\n Contact the bot owner if the error persists"
try:
await interaction.send(message, ephemeral=True)
except:
try:
await interaction.followup.send(message, ephemeral=True)
except:
await interaction.response.send_message(message, ephemeral=True)
class wordpress(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
@nextcord.slash_command(
name="blog",
description="create a blog post",
)
async def blog(self, interaction: nextcord.Interaction):
await interaction.response.send_modal(BlogModal("https://pastablog.insertapp.net/wp-json/wp/v2/posts",self.bot.blogcredentials))
def setup(bot: commands.Bot):
bot.add_cog(wordpress(bot))