insert2-cogs/wordpress.py

106 lines
3.6 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
2024-08-11 19:14:26 +00:00
import traceback
import sys
2024-08-05 19:12:11 +00:00
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
2024-08-11 19:14:26 +00:00
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)
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))