0.5.3 • Published 9 months ago

@headz/discuit v0.5.3

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

Discuit Client

Install

yarn add @headz/discuit

Usage

import { Discuit } from '@headz/discuit';

const discuit = new Discuit();
await discuit.login(process.env.DISCUIT_USERNAME, process.env.DISCUIT_PASSWORD);

// Get the latest posts.
const posts = await discuit.getPosts('latest', 50);
for (let i = 0; i < posts.length; i++) {
  const post = posts[i];
  const comment = await discuit.postComment(post.id, 'Welcome to the community!');
  await discuit.deleteComment(comment.postId, comment.id);
}

// Get the user's notifications.
const notifications = await discuit.getNotifications();
console.log(notifications);

for (let i = 0; i < notifications.length; i++) {
  const notification = notifications[i];
  await discuit.markNotificationAsSeen(notification.id);
  await discuit.deleteNotification(notification.id);
}

await discuit.deleteAllNotifications();

Using the watch method.

import { Discuit } from '@headz/discuit';

const communities = ['news', 'politics', 'sports'];

const discuit = new Discuit();
await discuit.login(process.env.DISCUIT_USERNAME, process.env.DISCUIT_PASSWORD);

// Watch for new posts in the communities.
discuit.watchPosts(communities, async (community, post) => {
  console.log(community, post);

  const comment = await discuit.postComment(post.id, 'Welcome to the community!');
  await discuit.deleteComment(comment.postId, comment.id);
});

Configuration

import { Discuit } from '@headz/discuit';
import winston from 'winston';

const discuit = new Discuit();

// Attach a logger.
discuit.logger = winston.createLogger({
    level: 'debug',
    format: winston.format.json(),
    defaultMeta: { service: 'discuit' },
    transports: [
        new winston.transports.Console({
            format: winston.format.simple(),
        })
    ],
});

// Configure how often the watch() command checks for new posts.
// Don't set this too low or discuit will rate limit the bot.
discuit.watchInterval = 1000 * 60 * 10 // 10 minutes

// How long to wait between callbacks in the watch loop.
discuit.sleepPeriod = 5000;

Methods

The library is still in development, so the API is subject to change.

login(username: string, password: string): Promise

Logs into the server.

import { Discuit } from '@headz/discuit';

const discuit = new Discuit();
await discuit.login('DISCUIT_USERNAME', 'DISCUIT_PASSWORD');

getMe(): Promise<User | null>

Returns the logged-in user.

const user = await discuit.getMe();

getPosts(sort: string, limit: number, next?: string, communityId?: string): Promise<Post[]>

Fetches the latest posts.

const posts = await discuit.getPosts('latest', 50);

getPost(publicId: string): Promise<Post | null>

Returns the details of a post.

const post = await discuit.getPost('12345');

votePost(postId: string, up: boolean): Promise

Votes a post up or down and returns the post. If already voted, then changes the vote.

await discuit.votePost('12345', true);

getPostComments(publicId: string, next?: string, parentId?: string): Promise<{ comments: Comment[]; next: string }>

Returns the comments for the given post.

const comments = await discuit.getPostComments('12345');

getNotifications(): Promise<Notification[]>

Returns all the user's notifications.

const notifications = await discuit.getNotifications();

markNotificationAsSeen(id: number): Promise

Marks a notification as seen.

await discuit.markNotificationAsSeen(80155);

deleteNotification(id: number): Promise

Deletes a notification.

await discuit.deleteNotification(80155);

deleteAllNotifications(): Promise

Deletes all notifications.

await discuit.deleteAllNotifications();

getComment(id: string): Promise<Comment | null>

Returns the comment with the given id.

const comment = await discuit.getComment('12345');

postComment(publicId: number, content: string): Promise

Submits a comment.

const comment = await discuit.postComment(12345, 'Welcome to the community!');

updateComment(publicId: string, commentId: string, content: string): Promise

Updates a comment.

const comment = await discuit.updateComment('12345', '67890', 'Welcome to the community!');

deleteComment(postId: string, commentId: string): Promise

Deletes a comment.

await discuit.deleteComment('12345', '67890');

watchPosts(communities: string[], callback: (community: string, post: Post) => void): Promise

Watches for new posts.

discuit.watchPosts(['news', 'politics', 'sports'], async (community, post) => {
  console.log(community, post);

  const comment = await discuit.postComment(post.id, 'Welcome to the community!');
  await discuit.deleteComment(comment.postId, comment.id);
});

voteComment(commentId: string, up: boolean): Promise

Votes on a comment.

await discuit.voteComment('12345', true);

getCommunities(): Promise<Community[]>

Returns an array of the site communities.

const communities = await discuit.getCommunities();

getCommunity(communityId: string): Promise<Community | null>

Returns the community with the given id.

const community = await discuit.getCommunity('12346');

updateCommunity(communityId: string, community: Partial): Promise

const community = await discuit.updateCommunity('12346', {
  nsfw: true,
  about: 'My community description.',
});

joinCommunity(communityId: string, leave: boolean): Promise

Make the authenticated user join or leave a community.

await discuit.joinCommunity('12346', false);

getCommunityMods(communityId: string): Promise<User[]>

Returns the moderators of a community.

const mods = await discuit.getCommunityMods('12346');

addCommunityMod(communityId: string, username: string): Promise

Adds a moderator to a community.

await discuit.addCommunityMod('12346', 'username');

deleteCommunityMod(communityId: string, username: string): Promise

Removes a moderator from a community.

await discuit.deleteCommunityMod('12346', 'username');

getCommunityRules(communityId: string): Promise<CommunityRule[]>

Returns the rules of a community.

const rules = await discuit.getCommunityRules('12346');

createCommunityRule(communityId: string, rule: string, description: string): Promise

Creates a rule for a community.

await discuit.createCommunityRule('12346', 'Rule 1', 'This is rule 1.');

updateCommunityRule(communityId: string, ruleId: number, rule: Partial): Promise

Updates a rule for a community.

await discuit.updateCommunityRule('12346', 1, {
  rule: 'Rule 1',
  description: 'This is rule 1.',
    zIndex: 4,
});

deleteCommunityRule(communityId: string, ruleId: number): Promise

Deletes a rule from a community.

await discuit.deleteCommunityRule('12346', 1);
0.5.3

9 months ago

0.5.2

9 months ago

0.5.1

9 months ago

0.5.0

9 months ago

0.4.3

9 months ago

0.4.2

9 months ago

0.4.1

9 months ago

0.4.0

9 months ago

0.3.2

9 months ago

0.3.1

9 months ago

0.3.0

9 months ago

0.2.6

9 months ago

0.2.5

9 months ago

0.2.4

9 months ago

0.2.3

9 months ago

0.2.2

9 months ago

0.2.1

9 months ago

0.2.0

9 months ago

0.1.1

9 months ago

0.1.0

9 months ago

0.0.5

9 months ago

0.0.4

9 months ago

0.0.3

9 months ago

0.0.2

9 months ago

0.0.1

9 months ago