insert2-cogs/wordpress.py

91 lines
3 KiB
Python
Raw Normal View History

2024-08-07 19:20:39 +00:00
import nextcord
from nextcord.ext import commands
from nextcord import TextInputStyle
2024-08-05 19:12:11 +00:00
from dotenv import load_dotenv
from random import randint
import aiosqlite as sqlite3
import requests
import json
from datetime import datetime
import base64
from urllib.parse import urlparse
import os
2024-08-07 19:20:39 +00:00
class BlogModal(nextcord.ui.Modal):
2024-08-05 19:12:11 +00:00
def __init__(self,endpoint,credentials):
self.endpoint = endpoint
self.credentials = credentials
2024-08-07 19:20:39 +00:00
super().__init__(
f"create a post"
)
2024-08-05 19:12:11 +00:00
2024-08-07 19:20:39 +00:00
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:
2024-08-05 19:12:11 +00:00
token = base64.b64encode(self.credentials.encode())
dt = datetime.now()
date = dt.isoformat()
2024-08-07 19:20:39 +00:00
fromatedtitle = f"{self.name.value} by {self.author.value}"
formatedcontent = str(self.content.value)+" " + str(self.contenttwo.value)
catergory = int(self.catergory.value)
2024-08-05 19:12:11 +00:00
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)
2024-08-07 19:20:39 +00:00
await interaction.response.send_message(f"<@666378959184855042> someone posted to the blog, you should probably approve it {responce.status_code}")
2024-08-05 19:12:11 +00:00
class wordpress(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
2024-08-07 19:20:39 +00:00
@nextcord.slash_command(
2024-08-05 19:12:11 +00:00
name="blog",
description="create a blog post",
)
2024-08-07 19:20:39 +00:00
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))
2024-08-05 19:12:11 +00:00
def setup(bot: commands.Bot):
bot.add_cog(wordpress(bot))