Last active
November 1, 2024 09:20
-
-
Save svandragt/cce1f4777b0242303d2d89d146925ff0 to your computer and use it in GitHub Desktop.
Script to post a discord message to WordPress when a heart emoji is used
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import discord | |
from discord.ext import commands | |
import requests | |
import json | |
# Create a bot instance with a command prefix | |
bot = commands.Bot(command_prefix='!') | |
# WordPress credentials | |
WORDPRESS_URL = 'https://yourwordpresssite.com/wp-json/wp/v2/posts' | |
USERNAME = 'your_username' | |
APPLICATION_PASSWORD = 'your_application_password' # Use Application Passwords or JWT token | |
def post_to_wordpress(title, content): | |
"""Posts content to WordPress and returns the response.""" | |
post_data = { | |
'title': title, | |
'content': content, | |
'status': 'publish' # Change to 'draft' if you want to save it as a draft | |
} | |
# Make the POST request to the WordPress REST API | |
response = requests.post( | |
WORDPRESS_URL, | |
auth=(USERNAME, APPLICATION_PASSWORD), | |
headers={'Content-Type': 'application/json'}, | |
data=json.dumps(post_data) | |
) | |
return response | |
# Event: When the bot is ready | |
@bot.event | |
async def on_ready(): | |
print(f'Logged in as {bot.user.name} (ID: {bot.user.id})') | |
print('------') | |
# Command: Post a story to WordPress | |
@bot.command() | |
async def post_story(ctx, *, story_content): | |
response = post_to_wordpress('New Story from Discord', story_content) | |
# Check the response | |
if response.status_code == 201: | |
await ctx.send('Story posted successfully! React with ❤️ to post this story to WordPress.') | |
else: | |
await ctx.send(f'Failed to post story: {response.status_code} - {response.text}') | |
# Event: When a reaction is added | |
@bot.event | |
async def on_reaction_add(reaction, user): | |
# Check if the reaction is a heart emoji and the user is not the bot | |
if reaction.emoji == '❤️' and user != bot.user: | |
# Get the message content | |
message_content = reaction.message.content | |
# Post the message content to WordPress | |
response = post_to_wordpress('New Story from Discord', message_content) | |
# Check the response | |
if response.status_code == 201: | |
await reaction.message.channel.send('Story posted to WordPress successfully!') | |
else: | |
await reaction.message.channel.send(f'Failed to post story: {response.status_code} - {response.text}') | |
# Run the bot with the token | |
bot.run('YOUR_BOT_TOKEN') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment