camphouse.js
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -10,10 +10,5 @@ const axios = require('axios'); | ||
this.token = null; | ||
this.posts = []; | ||
this.commands = {}; | ||
this.interval = null; | ||
} | ||
on(event, callback) { | ||
this.commands[event] = callback; | ||
} | ||
async login(email, password) { | ||
@@ -23,5 +18,2 @@ try { | ||
this.token = response.data.data.token; | ||
if (this.commands.ready) { | ||
this.commands.ready(); | ||
} | ||
this.startPostPolling(); | ||
@@ -33,49 +25,49 @@ } catch (error) { | ||
async fetchPosts() { | ||
async fetchPostsAndComments() { | ||
try { | ||
const response = await axios.get(`${BASE_URL}/posts`, { headers: { Authorization: `Bearer ${this.token}` } }); | ||
const newPosts = response.data.data.posts; | ||
if (newPosts.length > 0) { | ||
this.posts = newPosts; | ||
if (this.commands.newPosts) { | ||
this.commands.newPosts(newPosts); | ||
} | ||
for (const post of newPosts) { | ||
const postId = post.id; | ||
const commentsResponse = await axios.get(`${BASE_URL}/comments/post/${postId}`, { | ||
headers: { Authorization: `Bearer ${this.token}` }, | ||
}); | ||
post.comments = commentsResponse.data.data; | ||
} | ||
return newPosts; | ||
} catch (error) { | ||
console.error('Error fetching posts:', error.message); | ||
console.error('Error fetching posts and comments:', error.message); | ||
return []; | ||
} | ||
} | ||
async createPost(title, content) { | ||
try { | ||
const response = await axios.post( | ||
`${BASE_URL}/posts`, | ||
{ title, content }, | ||
{ headers: { Authorization: `Bearer ${this.token}` } } | ||
); | ||
const newPost = response.data.data; | ||
if (this.commands.postCreated) { | ||
this.commands.postCreated(newPost); | ||
startPostPolling() { | ||
// Poll every 1 second | ||
this.interval = setInterval(async () => { | ||
const newPosts = await this.fetchPostsAndComments(); | ||
if (newPosts.length > 0) { | ||
this.emit('newPosts', newPosts); | ||
} | ||
return newPost; | ||
} catch (error) { | ||
console.error('Error creating post:', error.message); | ||
return null; | ||
} | ||
}, 1000); | ||
} | ||
async startPostPolling() { | ||
// Poll every 30 seconds (adjust as needed) | ||
setInterval(async () => { | ||
await this.fetchPosts(); | ||
}, 30000); | ||
stopPostPolling() { | ||
clearInterval(this.interval); | ||
} | ||
handleInteraction(interaction) { | ||
if (!interaction.isChatInputCommand()) return; | ||
handleInteractionPostCreate(post) { | ||
const command = 's!randomnumber'; // Example command to check for | ||
if (post.content.includes(command)) { | ||
// Handle the command functionality for post creation | ||
// Assuming this is how you create a post (modify as needed) | ||
console.log(`Post ${post.id} contains the command: ${command}`); | ||
} | ||
} | ||
const { commandName } = interaction; | ||
if (this.commands[commandName]) { | ||
this.commands[commandName](interaction); | ||
handleInteractionCommentCreate(comment) { | ||
const command = 's!randomnumber'; // Example command to check for | ||
if (comment.comment.includes(command)) { | ||
// Handle the command functionality for comment creation | ||
// Assuming this is how you create a comment (modify as needed) | ||
console.log(`Comment ${comment._id} contains the command: ${command}`); | ||
} | ||
@@ -82,0 +74,0 @@ } |
{ | ||
"name": "camphouse.js", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A package to interact with the Camphouse API", | ||
@@ -5,0 +5,0 @@ "main": "camphouse.js", |
4357
67