insert2-cogs/wordpress.py
2024-08-05 18:03:19 -04:00

91 lines
No EOL
3.1 KiB
Python

import disnake
from disnake.ext import commands
from disnake import TextInputStyle
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
class ApplicationModal(disnake.ui.Modal):
def __init__(self,endpoint,credentials):
self.endpoint = endpoint
self.credentials = credentials
components = [
disnake.ui.TextInput(
label="Title",
placeholder="Why pasta is better than bread",
custom_id="title",
style=TextInputStyle.short,
max_length=100,
),
disnake.ui.TextInput(
label="Author",
placeholder="insertapp",
custom_id="author",
style=TextInputStyle.short,
max_length=32,
),
disnake.ui.TextInput(
label="catergory id, if you don't know put 1",
placeholder="1",
custom_id="catergory",
style=TextInputStyle.short,
max_length=2,
),
disnake.ui.TextInput(
label="content",
placeholder="words...",
custom_id="content",
style=TextInputStyle.paragraph,
max_length=4000,
),
disnake.ui.TextInput(
label="content2",
placeholder="optional more words...",
required = False,
custom_id="content2",
style=TextInputStyle.paragraph,
max_length=4000,
),
]
super().__init__(title=f"create a post", components=components)
async def callback(self, inter: disnake.ModalInteraction):
filledoutresponse = inter.text_values
values = list(filledoutresponse.values())
token = base64.b64encode(self.credentials.encode())
dt = datetime.now()
date = dt.isoformat()
fromatedtitle = f"{values[0]} by {values[1]}"
formatedcontent = str(values[3])+" " + str(values[4])
catergory = int(values[2])
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 inter.response.send_message("<@666378959184855042> someone posted to the blog, you should probably approve it")
class wordpress(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
@commands.slash_command(
name="blog",
description="create a blog post",
)
async def blog(self, inter: disnake.ApplicationCommandInteraction):
await inter.response.send_modal(modal=ApplicationModal(self.bot.blogendpoint,self.bot.blogcredentials))
def setup(bot: commands.Bot):
bot.add_cog(wordpress(bot))