diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..76b33ab --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,2 @@ +patreon: Lebyy +github: Lebyy diff --git a/.github/workflows/jsdoc-deploy.yml b/.github/workflows/jsdoc-deploy.yml new file mode 100644 index 0000000..5ef12e7 --- /dev/null +++ b/.github/workflows/jsdoc-deploy.yml @@ -0,0 +1,28 @@ +name: GitHub pages + +on: + push: + branches: + - master + +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Build + uses: andstor/jsdoc-action@v1 + with: + output_dir: ./docs + config_file: .jsdoc.json + template: Androz2091/jsdoc-skyceil + front_page: README.md + + - name: Deploy + uses: peaceiris/actions-gh-pages@v3 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + publish_dir: ./docs + cname: discord-messages.js.org \ No newline at end of file diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml new file mode 100644 index 0000000..65b8f4c --- /dev/null +++ b/.github/workflows/npm-publish.yml @@ -0,0 +1,36 @@ +# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created +# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages + +name: Node.js Package + +on: + release: + types: [created] + +jobs: + + publish-npm: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: 12 + registry-url: https://registry.npmjs.org/ + - run: npm publish + env: + NODE_AUTH_TOKEN: ${{secrets.npm_token}} + + publish-gpr: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: 12 + registry-url: 'https://npm.pkg.github.com/' + scope: '@lebyy' + - run: npm run gpr + - run: npm publish + env: + NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 71b87bc..27101f6 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ package-lock.json test.js docs/ .npmrc -.replit \ No newline at end of file +.replit +scripts/ diff --git a/README.md b/README.md index 82ca4f6..1e91ae8 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,17 @@ Promise It removes a specified amount of messages to the current amount of messages for that user, in that guild. ```js -Messages.subtractXp(, , ); +Messages.subtractMessages(, , ); +``` +- Output: +``` +Promise +``` +**resetGuild** + +It deletes the entire guild's data. +```js +Messages.resetGuild(); ``` - Output: ``` diff --git a/index.d.ts b/index.d.ts index 842354d..5315e31 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,12 +1,13 @@ export class DiscordMessages{ -public setURL(dbUrl:string); -public createUser(userId:string, guildId:string); -public deleteUser(userId:string, guildId:string); -public appendMessage(userId:string, guildId:string, messages:number); -public setMessages(userId:string, guildId:string, messages:number); -public fetch(userId:string, guildId:string, fetchPosition:boolean); -public subtractMessages(userId:string, guildId:string, messages:number); -public fetchLeaderboard(guildId:string, limit:number); -public computeLeaderboard(client:any, leaderboard:any, fetchUsers:boolean); -} \ No newline at end of file +public setURL(dbUrl:string): object; +public createUser(userId:string, guildId:string): object; +public deleteUser(userId:string, guildId:string): object; +public appendMessage(userId:string, guildId:string, messages:number): object; +public setMessages(userId:string, guildId:string, messages:number): object; +public fetch(userId:string, guildId:string, fetchPosition:boolean): object; +public subtractMessages(userId:string, guildId:string, messages:number): object; +public resetGuild(guildId:string): object; +public fetchLeaderboard(guildId:string, limit:number): object; +public computeLeaderboard(client:any, leaderboard:any, fetchUsers:boolean): object; +} diff --git a/index.js b/index.js index 02d9360..99ee8c9 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ const mongoose = require("mongoose"); const Messages = require("./models/messages.js"); -var mongoUrl; +let mongoUrl; /** * @@ -13,12 +13,13 @@ class DiscordMessages { * * * @static - * @param {string} [dbUrl] - A valid mongo database URI. + * @param {string} dbUrl - A valid mongo database URI. * @return {Promise} - The mongoose connection promise. * @memberof DiscordMessages */ static async setURL(dbUrl) { if (!dbUrl) throw new TypeError("A database url was not provided."); + if(mongoUrl) throw new TypeError("A database url was already configured."); mongoUrl = dbUrl; return mongoose.connect(dbUrl, { useNewUrlParser: true, @@ -31,9 +32,9 @@ class DiscordMessages { * * * @static - * @param {string} [userId] - Discord user id. - * @param {string} [guildId] - Discord guild id. - * @return {objcet} - The user data object. + * @param {string} userId - Discord user id. + * @param {string} guildId - Discord guild id. + * @return {object} - The user data object. * @memberof DiscordMessages */ static async createUser(userId, guildId) { @@ -57,9 +58,9 @@ class DiscordMessages { * * * @static - * @param {string} [userId] - Discord user id. - * @param {string} [guildId] - Discord guild id. - * @return {objcet} - The user data object. + * @param {string} userId - Discord user id. + * @param {string} guildId - Discord guild id. + * @return {object} - The user data object. * @memberof DiscordMessages */ static async deleteUser(userId, guildId) { @@ -78,10 +79,10 @@ class DiscordMessages { * * * @static - * @param {string} [userId] - Discord user id. - * @param {string} [guildId] - Discord guild id. - * @param {number} [messages] - Amount of messages to append. - * @return {objcet} - The user data object. + * @param {string} userId - Discord user id. + * @param {string} guildId - Discord guild id. + * @param {number} messages - Amount of messages to append. + * @return {object} - The user data object. * @memberof DiscordMessages */ static async appendMessage(userId, guildId, messages) { @@ -113,10 +114,10 @@ class DiscordMessages { * * * @static - * @param {string} [userId] - Discord user id. - * @param {string} [guildId] - Discord guild id. - * @param {number} [messages] - Amount of messages to set. - * @return {objcet} - The user data object. + * @param {string} userId - Discord user id. + * @param {string} guildId - Discord guild id. + * @param {number} messages - Amount of messages to set. + * @return {object} - The user data object. * @memberof DiscordMessages */ static async setMessages(userId, guildId, messages) { @@ -138,10 +139,10 @@ class DiscordMessages { * * * @static - * @param {string} [userId] - Discord user id. - * @param {string} [guildId] - Discord guild id. + * @param {string} userId - Discord user id. + * @param {string} guildId - Discord guild id. * @param {boolean} [fetchPosition=false] - Wheter to fetch the users position. - * @return {objcet} - The user data object. + * @return {object} - The user data object. * @memberof DiscordMessages */ static async fetch(userId, guildId, fetchPosition = false) { @@ -167,10 +168,10 @@ class DiscordMessages { * * * @static - * @param {string} [userId] - Discord user id. - * @param {string} [guildId] - Discord guild id. - * @param {number} [messages] - Amount of messages to subtract. - * @return {objcet} - The user data object. + * @param {string} userId - Discord user id. + * @param {string} guildId - Discord guild id. + * @param {number} messages - Amount of messages to subtract. + * @return {object} - The user data object. * @memberof DiscordMessages */ static async subtractMessages(userId, guildId, messages) { @@ -188,12 +189,33 @@ class DiscordMessages { return user; } + /** + * + * + * @static + * @param {string} guildId - Discord guild id. + * @return {boolean} - Return's true if success. + * @memberof DiscordMessages + */ + static async resetGuild(guildId) { + if (!guildId) throw new TypeError("A guild id was not provided."); + + const user = await Messages.findOne({ guildID: guildId }); + if (!user) return false; + + await Messages.findOneAndDelete({ + guildID: guildId + }).catch(e => console.log(`Failed to reset guild: ${e}`)); + + return true; + } + /** * * * @static - * @param {string} [guildId] - Discord guild id. - * @param {number} [limit] - Amount of maximum enteries to return. + * @param {string} guildId - Discord guild id. + * @param {number} limit - Amount of maximum enteries to return. * @return {Array} - The leaderboard array. * @memberof DiscordMessages */ @@ -210,8 +232,8 @@ class DiscordMessages { * * * @static - * @param {string} [client] - Your Discord.CLient. - * @param {array} [leaderboard] - The output from 'fetchLeaderboard' function. + * @param {string} client - Your Discord.CLient. + * @param {array} leaderboard - The output from 'fetchLeaderboard' function. * @param {boolean} [fetchUsers=false] - Wheter to fetch each users position. * @return {*} * @memberof DiscordMessages diff --git a/package.json b/package.json index b11dbf3..54623fb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,9 @@ { "name": "discord-messages", - "version": "1.0.1", + "scripts": { + "gpr": "node scripts/gpr.js" + }, + "version": "1.0.2", "description": "A lightweight and easy to use message tracking framework for discord bots, uses MongoDB.", "main": "index.js", "dependencies": { @@ -39,4 +42,4 @@ "url": "https://github.com/Lebyy/discord-messages/issues" }, "homepage": "https://discord-messages.js.org" -} \ No newline at end of file +} diff --git a/scripts/gpr.js b/scripts/gpr.js new file mode 100644 index 0000000..29fa39f --- /dev/null +++ b/scripts/gpr.js @@ -0,0 +1,8 @@ +const fs = require('fs'); +const { join } = require('path'); + +const pkg = require('../package.json'); + +pkg.name = `@lebyy/${pkg.name}`; + +fs.writeFileSync(join(__dirname, '../package.json'), JSON.stringify(pkg));