-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Discord] Add more badges (members/online/boosts) #9098
Closed
Closed
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2cc4811
Add more Discord badges
RedSparr0w d600cf0
tidy up, add online-count
RedSparr0w c3b6723
tidy up doc
RedSparr0w fdc7f99
prettier?
RedSparr0w a14f994
Fixup duplicate class names
RedSparr0w 34244e7
Fixup test
RedSparr0w b02753f
remove auth from request to invite URI
RedSparr0w 5d863c9
Revert "remove auth from request to invite URI"
RedSparr0w e02d21f
rename badge with-total
RedSparr0w 48057e8
Update discord-online-with-total.service.js
RedSparr0w 3598d70
update cache time
RedSparr0w 7907340
Merge branch 'master' into sparr0w/discord
RedSparr0w File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import Joi from 'joi' | ||
import { nonNegativeInteger } from '../validators.js' | ||
import { BaseJsonService } from '../index.js' | ||
|
||
const schema = Joi.object({ | ||
guild: { | ||
premium_subscription_count: nonNegativeInteger, | ||
}, | ||
}).required() | ||
|
||
const documentation = ` | ||
<p> | ||
The Discord badge requires the <code>SERVER INVITE CODE</code> in order access the Discord JSON API, this invite code should be set to never expire. | ||
</p> | ||
<p> | ||
The <code>SERVER INVITE CODE</code> can be located at the end of the invite url. | ||
</p> | ||
` | ||
|
||
export default class DiscordBoosts extends BaseJsonService { | ||
static category = 'chat' | ||
|
||
static route = { | ||
base: 'discord/boosts', | ||
pattern: ':inviteId', | ||
} | ||
|
||
static auth = { | ||
passKey: 'discord_bot_token', | ||
authorizedOrigins: ['https://discord.com'], | ||
isRequired: false, | ||
} | ||
|
||
static examples = [ | ||
{ | ||
title: 'Discord', | ||
namedParams: { inviteId: 'HjJCwm5' }, | ||
staticPreview: this.render({ boosts: 5 }), | ||
documentation, | ||
}, | ||
] | ||
|
||
static _cacheLength = 300 | ||
|
||
static defaultBadgeData = { label: 'chat' } | ||
|
||
static render({ boosts }) { | ||
return { | ||
message: `${boosts} boosts`, | ||
color: 'brightgreen', | ||
} | ||
} | ||
|
||
async fetch({ inviteId }) { | ||
const url = `https://discord.com/api/v9/invites/${inviteId}?with_counts=true` | ||
return this._requestJson( | ||
this.authHelper.withBearerAuthHeader( | ||
{ | ||
url, | ||
schema, | ||
errorMessages: { | ||
404: 'invalid server invite', | ||
}, | ||
}, | ||
'Bot' | ||
) | ||
) | ||
} | ||
|
||
async handle({ inviteId }) { | ||
const data = await this.fetch({ inviteId }) | ||
return this.constructor.render({ | ||
boosts: data.guild.premium_subscription_count, | ||
}) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Joi from 'joi' | ||
import { createServiceTester } from '../tester.js' | ||
export const t = await createServiceTester() | ||
|
||
t.create('gets boosts for Reactiflux') | ||
.get('/reactiflux.json') | ||
.expectBadge({ | ||
label: 'chat', | ||
message: Joi.string().regex(/^[0-9]+ boosts$/), | ||
color: 'brightgreen', | ||
}) | ||
|
||
t.create('invalid server invite ID') | ||
.get('/discord.json') | ||
.expectBadge({ label: 'chat', message: 'invalid server invite' }) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import Joi from 'joi' | ||
import { nonNegativeInteger } from '../validators.js' | ||
import { BaseJsonService } from '../index.js' | ||
|
||
const schema = Joi.object({ | ||
approximate_member_count: nonNegativeInteger, | ||
}).required() | ||
|
||
const documentation = ` | ||
<p> | ||
The Discord badge requires the <code>SERVER INVITE CODE</code> in order access the Discord JSON API, this invite code should be set to never expire. | ||
</p> | ||
<p> | ||
The <code>SERVER INVITE CODE</code> can be located at the end of the invite url. | ||
</p> | ||
` | ||
|
||
export default class DiscordMembers extends BaseJsonService { | ||
static category = 'chat' | ||
|
||
static route = { | ||
base: 'discord/members', | ||
pattern: ':inviteId', | ||
} | ||
|
||
static auth = { | ||
passKey: 'discord_bot_token', | ||
authorizedOrigins: ['https://discord.com'], | ||
isRequired: false, | ||
} | ||
|
||
static examples = [ | ||
{ | ||
title: 'Discord', | ||
namedParams: { inviteId: 'HjJCwm5' }, | ||
staticPreview: this.render({ members: 237 }), | ||
documentation, | ||
}, | ||
] | ||
|
||
static _cacheLength = 300 | ||
|
||
static defaultBadgeData = { label: 'chat' } | ||
|
||
static render({ members }) { | ||
return { | ||
message: `${members} members`, | ||
color: 'brightgreen', | ||
} | ||
} | ||
|
||
async fetch({ inviteId }) { | ||
const url = `https://discord.com/api/v9/invites/${inviteId}?with_counts=true` | ||
return this._requestJson( | ||
this.authHelper.withBearerAuthHeader( | ||
{ | ||
url, | ||
schema, | ||
errorMessages: { | ||
404: 'invalid server invite', | ||
}, | ||
}, | ||
'Bot' | ||
) | ||
) | ||
} | ||
|
||
async handle({ inviteId }) { | ||
const data = await this.fetch({ inviteId }) | ||
return this.constructor.render({ | ||
members: data.approximate_member_count, | ||
}) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Joi from 'joi' | ||
import { createServiceTester } from '../tester.js' | ||
export const t = await createServiceTester() | ||
|
||
t.create('gets members for Reactiflux') | ||
.get('/reactiflux.json') | ||
.expectBadge({ | ||
label: 'chat', | ||
message: Joi.string().regex(/^[0-9]+ members$/), | ||
color: 'brightgreen', | ||
}) | ||
|
||
t.create('invalid server invite ID') | ||
.get('/discord.json') | ||
.expectBadge({ label: 'chat', message: 'invalid server invite' }) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import Joi from 'joi' | ||
import { nonNegativeInteger } from '../validators.js' | ||
import { BaseJsonService } from '../index.js' | ||
|
||
const schema = Joi.object({ | ||
approximate_presence_count: nonNegativeInteger, | ||
approximate_member_count: nonNegativeInteger, | ||
}).required() | ||
|
||
const documentation = ` | ||
<p> | ||
The Discord badge requires the <code>SERVER INVITE CODE</code> in order access the Discord JSON API, this invite code should be set to never expire. | ||
</p> | ||
<p> | ||
The <code>SERVER INVITE CODE</code> can be located at the end of the invite url. | ||
</p> | ||
` | ||
|
||
export default class DiscordOnlineWithTotalt extends BaseJsonService { | ||
static category = 'chat' | ||
|
||
static route = { | ||
base: 'discord/online-with-total', | ||
pattern: ':inviteId', | ||
} | ||
|
||
static auth = { | ||
passKey: 'discord_bot_token', | ||
authorizedOrigins: ['https://discord.com'], | ||
isRequired: false, | ||
} | ||
|
||
static examples = [ | ||
{ | ||
title: 'Discord', | ||
namedParams: { inviteId: 'HjJCwm5' }, | ||
staticPreview: this.render({ online: 97, members: 237 }), | ||
documentation, | ||
}, | ||
] | ||
|
||
static _cacheLength = 300 | ||
|
||
static defaultBadgeData = { label: 'chat' } | ||
|
||
static render({ online, members }) { | ||
return { | ||
message: `${online}/${members} online`, | ||
color: 'brightgreen', | ||
} | ||
} | ||
|
||
async fetch({ inviteId }) { | ||
const url = `https://discord.com/api/v9/invites/${inviteId}?with_counts=true` | ||
return this._requestJson( | ||
this.authHelper.withBearerAuthHeader( | ||
{ | ||
url, | ||
schema, | ||
errorMessages: { | ||
404: 'invalid server invite', | ||
}, | ||
}, | ||
'Bot' | ||
) | ||
) | ||
} | ||
|
||
async handle({ inviteId }) { | ||
const data = await this.fetch({ inviteId }) | ||
return this.constructor.render({ | ||
online: data.approximate_presence_count, | ||
members: data.approximate_member_count, | ||
}) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Joi from 'joi' | ||
import { createServiceTester } from '../tester.js' | ||
export const t = await createServiceTester() | ||
|
||
t.create('gets online members for Reactiflux') | ||
.get('/reactiflux.json') | ||
.expectBadge({ | ||
label: 'chat', | ||
message: Joi.string().regex(/^[0-9]+\/[0-9]+ online$/), | ||
color: 'brightgreen', | ||
}) | ||
|
||
t.create('invalid server invite ID') | ||
.get('/discord.json') | ||
.expectBadge({ label: 'chat', message: 'invalid server invite' }) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import Joi from 'joi' | ||
import { nonNegativeInteger } from '../validators.js' | ||
import { BaseJsonService } from '../index.js' | ||
|
||
const schema = Joi.object({ | ||
approximate_presence_count: nonNegativeInteger, | ||
}).required() | ||
|
||
const documentation = ` | ||
<p> | ||
The Discord badge requires the <code>SERVER INVITE CODE</code> in order access the Discord JSON API, this invite code should be set to never expire. | ||
</p> | ||
<p> | ||
The <code>SERVER INVITE CODE</code> can be located at the end of the invite url. | ||
</p> | ||
` | ||
|
||
export default class DiscordOnline extends BaseJsonService { | ||
static category = 'chat' | ||
|
||
static route = { | ||
base: 'discord/online', | ||
pattern: ':inviteId', | ||
} | ||
|
||
static auth = { | ||
passKey: 'discord_bot_token', | ||
authorizedOrigins: ['https://discord.com'], | ||
isRequired: false, | ||
} | ||
|
||
static examples = [ | ||
{ | ||
title: 'Discord', | ||
namedParams: { inviteId: 'HjJCwm5' }, | ||
staticPreview: this.render({ online: 97 }), | ||
documentation, | ||
}, | ||
] | ||
|
||
static _cacheLength = 300 | ||
|
||
static defaultBadgeData = { label: 'chat' } | ||
|
||
static render({ online }) { | ||
return { | ||
message: `${online} online`, | ||
color: 'brightgreen', | ||
} | ||
} | ||
|
||
async fetch({ inviteId }) { | ||
const url = `https://discord.com/api/v9/invites/${inviteId}?with_counts=true` | ||
return this._requestJson( | ||
this.authHelper.withBearerAuthHeader( | ||
{ | ||
url, | ||
schema, | ||
errorMessages: { | ||
404: 'invalid server invite', | ||
}, | ||
}, | ||
'Bot' | ||
) | ||
) | ||
} | ||
|
||
async handle({ inviteId }) { | ||
const data = await this.fetch({ inviteId }) | ||
return this.constructor.render({ | ||
online: data.approximate_presence_count, | ||
}) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Joi from 'joi' | ||
import { createServiceTester } from '../tester.js' | ||
export const t = await createServiceTester() | ||
|
||
t.create('gets online members for Reactiflux') | ||
.get('/reactiflux.json') | ||
.expectBadge({ | ||
label: 'chat', | ||
message: Joi.string().regex(/^[0-9]+ online$/), | ||
color: 'brightgreen', | ||
}) | ||
|
||
t.create('invalid server invite ID') | ||
.get('/discord.json') | ||
.expectBadge({ label: 'chat', message: 'invalid server invite' }) |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like there's some commonality/duplication across the new classes especially in these
fetch
functions. Probably worth hoisting that out into a Discord base class or a common helper function IMO