Skip to content

Commit

Permalink
New features and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbberry committed May 26, 2021
1 parent 6623197 commit 171f349
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 78 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ Easy AutoPosting via the [top.gg sdk](https://npmjs.com/package/@top-gg/sdk)
# How to
It's really really simple! All you gotta do is:
```js
const AutoPoster = require('topgg-autoposter')
const { AutoPoster } = require('topgg-autoposter')

const ap = AutoPoster('topggtoken', client) // your discord.js or eris client
const poster = AutoPoster('topggtoken', client) // your discord.js or eris client

// optional
ap.on('posted', () => { // ran when succesfully posted
console.log('Posted stats to top.gg')
poster.on('posted', (stats) => { // ran when succesfully posted
console.log(`Posted stats to Top.gg | ${stats.serverCount} servers`)
})
```
*You can also do poster.on('error', (err) => { ... }) and this will stop errors from being logged and left for you to handle*

And that's it!

It will begin to post your server count, and shard count every 30 minutes.

This even work on individual Discord.JS shards that are process separated.

If you would like to do specific clients, `AutoPoster.DJSPoster` & `AutoPoster.ErisPoster` & `AutoPoster.DJSSharderPoster` with the same parameters!
If you would like to do specific clients, `DJSPoster` & `ErisPoster` & `DJSSharderPoster` & `RosePoster` are all exported classes!

## Traditional Discord.JS Sharding:

Expand Down
1 change: 0 additions & 1 deletion index.js

This file was deleted.

52 changes: 6 additions & 46 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "topgg-autoposter",
"version": "1.2.0",
"version": "2.0.0",
"description": "Auto-Poster for Top.gg",
"main": "index.js",
"main": "dist/index.js",
"scripts": {
"test": "node ./tests/test.js",
"build": "tsc",
Expand All @@ -24,6 +24,7 @@
"eris": "latest"
},
"dependencies": {
"@jpbberry/typed-emitter": "^1.0.1",
"@top-gg/sdk": "^3.0.9",
"typescript": "^4.2.3"
},
Expand Down
20 changes: 10 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import DJSPoster from './structs/DJSPoster'
import ErisPoster from './structs/ErisPoster'
import DJSSharderPoster from './structs/DJSSharderPoster'
import RosePoster from './structs/RosePoster'
import { DJSPoster } from './structs/DJSPoster'
import { ErisPoster } from './structs/ErisPoster'
import { DJSSharderPoster } from './structs/DJSSharderPoster'
import { RosePoster } from './structs/RosePoster'

import { BasePoster } from './structs/BasePoster'

Expand All @@ -17,7 +17,7 @@ import { PosterOptions } from './typings'
*
* AutoPoster('topggtoken', client) // that's it!
*/
function AutoPoster (token: string, client: any, options?: PosterOptions): BasePoster {
export function AutoPoster (token: string, client: any, options?: PosterOptions): BasePoster {
if (!token) throw new Error('Top.gg token is missing')
if (!client) throw new Error('Client is missing')
let DiscordJS
Expand All @@ -43,9 +43,9 @@ function AutoPoster (token: string, client: any, options?: PosterOptions): BaseP
throw new Error('Unsupported client')
}

AutoPoster.DJSPoster = DJSPoster
AutoPoster.ErisPoster = ErisPoster
AutoPoster.DJSSharderPost = DJSSharderPoster
AutoPoster.RosePoster = RosePoster
export { DJSPoster } from './structs/DJSPoster'
export { ErisPoster } from './structs/ErisPoster'
export { DJSSharderPoster } from './structs/DJSSharderPoster'
export { RosePoster } from './structs/RosePoster'

export default AutoPoster
export default AutoPoster
17 changes: 9 additions & 8 deletions src/structs/BasePoster.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventEmitter } from 'events'
import { Api } from '@top-gg/sdk'
import { EventEmitter } from '@jpbberry/typed-emitter'

import { BotStats } from '@top-gg/sdk/dist/typings'

Expand All @@ -11,12 +11,10 @@ export interface BasePosterInterface {
waitForReady: (fn: () => void) => void
}

export interface BasePoster {
on(event: 'posted', listener: (stats) => void)
on(event: 'error', listener: (Error) => void)
}

export class BasePoster extends EventEmitter {
export class BasePoster extends EventEmitter<{
posted: BotStats,
error: Error
}> {
private options: PosterOptions
private binds: BasePosterInterface
private api: Api
Expand Down Expand Up @@ -89,6 +87,9 @@ export class BasePoster extends EventEmitter {
public async post () {
this.api.postStats(await this.binds.getStats())
.then((data) => this.emit('posted', data))
.catch((err) => "error" in this._events ? this.emit("error", err) : console.error(err))
.catch((err) => this.eventNames().includes('error')
? this.emit('error', err)
: console.error(err)
)
}
}
2 changes: 1 addition & 1 deletion src/structs/DJSPoster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Client } from 'discord.js'
/**
* Auto-Poster For Discord.JS
*/
export default class DJSPoster extends BasePoster implements BasePosterInterface {
export class DJSPoster extends BasePoster implements BasePosterInterface {
private client: Client

/**
Expand Down
2 changes: 1 addition & 1 deletion src/structs/DJSSharderPoster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { PosterOptions } from '../typings'
/**
* Auto-Poster For Discord.JS ShardingManager
*/
export default class DJSSharderPoster extends BasePoster implements BasePosterInterface {
export class DJSSharderPoster extends BasePoster implements BasePosterInterface {
private client: ShardingManager

/**
Expand Down
2 changes: 1 addition & 1 deletion src/structs/ErisPoster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { PosterOptions } from '../typings'
/**
* Auto-Poster For Eris
*/
export default class ErisPoster extends BasePoster implements BasePosterInterface {
export class ErisPoster extends BasePoster implements BasePosterInterface {
private client: Client

/**
Expand Down
2 changes: 1 addition & 1 deletion src/structs/RosePoster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { PosterOptions } from '../typings'
/**
* Auto-Poster For Discord-Rose
*/
export default class RosePoster extends BasePoster implements BasePosterInterface {
export class RosePoster extends BasePoster implements BasePosterInterface {
private client: Master

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const topggToken = 'abc' // unnecessary to debug

const path = require('path')

const AutoPoster = require('..')
const { AutoPoster } = require('..')

function debug (msg) {
console.debug(`[TEST] ${currentRunning}: ${msg}`)
Expand Down
2 changes: 1 addition & 1 deletion tests/traditional.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ api._request = (method, path, body) => {
console.log(method, path, body)
}

const AutoPoster = require('..')
const { AutoPoster } = require('..')
AutoPoster('abc', client, {
sdk: api
})
Expand Down

0 comments on commit 171f349

Please sign in to comment.