-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b3ea118
Showing
20 changed files
with
591 additions
and
0 deletions.
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,3 @@ | ||
node_modules/** | ||
build/** | ||
src/examples/** |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 TheBusyBiscuit | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,28 @@ | ||
{ | ||
"name": "bstats-discord-integration", | ||
"author": "TheBusyBiscuit", | ||
"version": "1.0.0", | ||
|
||
"description": "... TODO ...", | ||
"license": "MIT", | ||
"main": "src/index.ts", | ||
|
||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/baked-libs/bstats-discord-integration.git" | ||
}, | ||
|
||
"scripts": { | ||
"webhook": "npx tsc && node build/index.js" | ||
}, | ||
|
||
"dependencies": { | ||
"discord.js": "^12.5.3", | ||
"node-fetch": "^2.6.1" | ||
}, | ||
|
||
"devDependencies": { | ||
"@types/node": "^16.0.1", | ||
"typescript": "^4.3.5" | ||
} | ||
} |
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 * as fetch from 'node-fetch' | ||
import { Project } from './types' | ||
import { ChartType, Chart, convert } from './charts' | ||
|
||
const apiUrl: string = 'https://bstats.org/api/v1' | ||
const maxElements: number = 2 * 24 * 30 * 3 | ||
|
||
export async function getCharts(project: Project): Promise<any> { | ||
return getProjectInfo(project).then(async info => { | ||
console.log(`Found ${Object.keys(info.charts).length} charts!`) | ||
console.log(`Querying ${project.charts.length} charts...`) | ||
|
||
let data = await Promise.all(getAllCharts(project.id, project.charts, info.charts)); | ||
let charts = {}; | ||
|
||
for (let i in data) { | ||
let chart = data[i]; | ||
charts[chart.id] = chart; | ||
} | ||
|
||
return charts; | ||
}) | ||
} | ||
|
||
function getAllCharts(projectId: number, requestedCharts: string[], charts: any): Promise<Chart>[] { | ||
let promises = [] | ||
|
||
for (let i in requestedCharts) { | ||
let chartId = requestedCharts[i] | ||
|
||
// Verify the chart does actually exist | ||
if (chartId in charts) { | ||
let chartType = charts[chartId].type as ChartType | ||
promises.push(getChartData(projectId, chartId, chartType)) | ||
} else { | ||
console.log(`[!] Unknown chartId: '${chartId}', chart does not seem to exist.`) | ||
} | ||
} | ||
|
||
return promises | ||
} | ||
|
||
/** | ||
* Helper method to fetch a JSON object from an url. | ||
* | ||
* @param url The url to fetch data from | ||
* @return A Promise with the resulting JSON object | ||
*/ | ||
async function fetchJSON(url: string): Promise<any> { | ||
return fetch(url, { | ||
method: "GET", | ||
follow: 0, | ||
timeout: 60000, | ||
compress: true, | ||
headers: { | ||
'User-Agent': 'bstats-discord-webhook v1.0.0 (https://github.com/baked-libs/bstats-discord-integration)', | ||
'Accept': 'application/json' | ||
} | ||
}).then(response => response.json()) | ||
} | ||
|
||
async function getProjectInfo(project: Project): Promise<any> { | ||
console.log(`Querying project '${project.name}' with id '${project.id}'`) | ||
return fetchJSON(`${apiUrl}/plugins/${project.id}`) | ||
} | ||
|
||
async function getChartData(projectId: number, chartId: string, chartType: ChartType): Promise<Chart> { | ||
console.log(`-> Querying chart '${chartId}'...`) | ||
|
||
return fetchJSON(`${apiUrl}/plugins/${projectId}/charts/${chartId}/data?maxElements=${maxElements}`).then(chartData => { | ||
console.log(`--> Processing chart '${chartId}'...`) | ||
return convert(chartId, chartType, chartData) | ||
}) | ||
} |
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,5 @@ | ||
export * from './charts/ChartType' | ||
export * from './charts/Chart' | ||
export * from './charts/LineChart' | ||
export * from './charts/PieChart' | ||
export * from './charts/converter' |
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,9 @@ | ||
export class Chart { | ||
|
||
id: string | ||
|
||
constructor(id: string) { | ||
this.id = id | ||
} | ||
|
||
} |
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,7 @@ | ||
export const enum ChartType { | ||
|
||
SINGLE_LINE = 'single_linechart', | ||
SIMPLE_PIE = 'simple_pie', | ||
DRILLDOWN_PIE = 'drilldown_pie' | ||
|
||
} |
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 { Chart } from "./Chart"; | ||
|
||
export class LineChart extends Chart { | ||
|
||
current: number | ||
peak: number | ||
|
||
constructor(chartId: string, current: number, peak: number) { | ||
super(chartId) | ||
|
||
this.current = current | ||
this.peak = peak | ||
} | ||
|
||
} |
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,92 @@ | ||
import { Chart } from "./Chart"; | ||
|
||
export class PieChart extends Chart { | ||
|
||
id: string | ||
values: any | ||
|
||
constructor(id: string, values: any) { | ||
super(id) | ||
|
||
this.values = values | ||
} | ||
|
||
for(key: string) { | ||
let slice = this.values[key] | ||
return slice ? slice : new PieChartSlice(0, 1, key) | ||
} | ||
|
||
forAny(regex: RegExp) { | ||
let total = 0; | ||
let sum = 0; | ||
|
||
for (let key in this.values) { | ||
total += this.values[key].value | ||
|
||
if (key.match(regex)) { | ||
sum += this.values[key].value | ||
} | ||
} | ||
|
||
return new PieChartSlice(sum, total) | ||
} | ||
|
||
forAll(keys: string[]) { | ||
let total = 0 | ||
let sum = 0 | ||
|
||
for (let key in this.values) { | ||
total += this.values[key].value | ||
|
||
if (keys.indexOf(key) > -1) { | ||
sum += this.values[key].value | ||
} | ||
} | ||
|
||
return new PieChartSlice(sum, total) | ||
} | ||
|
||
forAllExcept(keys: string[]) { | ||
let total = 0 | ||
let remaining = 0 | ||
|
||
for (let key in this.values) { | ||
total += this.values[key].value | ||
|
||
if (keys.indexOf(key) === -1) { | ||
remaining += this.values[key].value | ||
} | ||
} | ||
|
||
return new PieChartSlice(remaining, total) | ||
} | ||
|
||
top() { | ||
let topSlice: PieChartSlice | ||
|
||
for (let key in this.values) { | ||
let slice = this.values[key] | ||
|
||
if (!topSlice || slice.value > topSlice.value) { | ||
topSlice = slice | ||
} | ||
} | ||
|
||
return topSlice | ||
} | ||
|
||
} | ||
|
||
class PieChartSlice { | ||
|
||
label?: string | ||
value: number | ||
percentage: string | ||
|
||
constructor(value: number, total: number, label?: string) { | ||
this.value = value | ||
this.percentage = Number((value / total) * 100.0).toFixed(2) + '%' | ||
this.label = label | ||
} | ||
|
||
} |
Oops, something went wrong.