Skip to content

Commit

Permalink
rename module to plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoins committed Jul 8, 2018
1 parent 9409a4e commit f614785
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 91 deletions.
3 changes: 1 addition & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"singleQuote": true,
"useTabs": false,
"arrowParens": "always",
"bracketSpacing": true,
"parser": "typescript",
"trailingComma": "none",
"tabWidth": 2,
"semi": true,
"semi": false,
"printWidth": 80
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Levels
> Leveling system module.
> Leveling system plugin.
## Features
* Gives a random amount of XP per message after an amount of time.
Expand All @@ -10,4 +10,4 @@
N/A

## Installation
All modules should be installed in the `modules/` directory of the bot. Modules are automatically loaded and registered when the bot starts.
All plugins should be installed in the `plugins/` directory of the bot. Plugins are automatically loaded and registered when the bot starts.
4 changes: 2 additions & 2 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { CommandoClient } from 'discord.js-commando';
import { Config } from '@nightwatch/util';
export declare class Module {
export declare class Plugin {
static config: Config;
static client: CommandoClient;
static id: string;
static description: string;
/**
* Initializes module
* Initializes plugin
* @param client
* @param config
*/
Expand Down
16 changes: 8 additions & 8 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

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

6 changes: 3 additions & 3 deletions dist/lib/Events.js

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

2 changes: 1 addition & 1 deletion dist/lib/Events.js.map

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

2 changes: 1 addition & 1 deletion dist/lib/index.js

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

2 changes: 1 addition & 1 deletion dist/lib/index.js.map

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

6 changes: 3 additions & 3 deletions package.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/NightwatchBot/module-levels.git"
"url": "git+https://github.com/NightwatchBot/plugin-levels.git"
},
"author": "Tanner Goins",
"license": "GPL-3.0",
"bugs": {
"url": "https://github.com/NightwatchBot/module-levels/issues"
"url": "https://github.com/NightwatchBot/plugin-levels/issues"
},
"homepage": "https://github.com/NightwatchBot/module-levels#readme",
"homepage": "https://github.com/NightwatchBot/plugin-levels#readme",
"devDependencies": {
"@types/node": "^9.6.1",
"@types/winston": "^2.3.9",
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import { CommandoClient } from 'discord.js-commando'
import { Config } from '@nightwatch/util'
import { onMessage } from './lib/Events'

export class Module {
export class Plugin {
static config: Config
static client: CommandoClient
static id = 'Levels'
static description = 'A leveling system that awards XP when users send messages. Also rewards credits when a user levels up.'

/**
* Initializes module
* Initializes plugin
* @param client
* @param config
*/
public async init (client: CommandoClient, config: Config) {
Module.client = client
Module.config = config
Plugin.client = client
Plugin.config = config
await this.registerListeners(client, config)
}

Expand All @@ -25,6 +25,6 @@ export class Module {
* @param client
*/
private async registerListeners (client: CommandoClient, config: Config): Promise<void> {
client.on('message', (message) => onMessage(message, config))
client.on('message', message => onMessage(message, config))
}
}
40 changes: 20 additions & 20 deletions src/lib/Events.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import { Message, TextChannel } from 'discord.js';
import axios from 'axios';
import { Module } from '../';
import { Logger, MessageUtility, Config } from '@nightwatch/util';
import { User as NightwatchUser } from '@nightwatch/db';
import { giveXp } from './';
import { Message, TextChannel } from 'discord.js'
import axios from 'axios'
import { Plugin } from '../'
import { Logger, MessageUtility, Config } from '@nightwatch/util'
import { User as NightwatchUser } from '@nightwatch/db'
import { giveXp } from './'

export const onMessage = async (message: Message, config: Config) => {
if (message.author.bot || !message.content || !message.content.trim() || message.channel.type !== 'text') {
return;
return
}

const { api } = Module.config;
const baseRoute = `${api.address}/users`;
const { api } = Plugin.config
const baseRoute = `${api.address}/users`

// prevent the user from earning xp for bot commands.
// handles *most* bots.
const firstTwoMatch = message.content.trim().substring(0, 2).match(/[a-z]/gi);
const firstTwoMatch = message.content.trim().substring(0, 2).match(/[a-z]/gi)
if (!firstTwoMatch || firstTwoMatch.length !== 2) {
return;
return
}

const route = `${baseRoute}/${message.author.id}?token=${api.token}`;
const route = `${baseRoute}/${message.author.id}?token=${api.token}`

axios
.get(route)
.then((res) => {
.then(res => {
if (!res.data) {
MessageUtility.createUser(message.author, Module.config).catch(Logger.error);
return;
MessageUtility.createUser(message.author, Plugin.config).catch(Logger.error)
return
}

const user = res.data as NightwatchUser;
const user = res.data as NightwatchUser

giveXp(user, message).catch(Logger.error);
giveXp(user, message).catch(Logger.error)
})
.catch((err: any) => {
Logger.error(err);
});
};
Logger.error(err)
})
}
82 changes: 41 additions & 41 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
import { Message, User } from 'discord.js';
import axios from 'axios';
import { Module } from '../';
import { Logger } from '@nightwatch/util';
import { User as NightwatchUser } from '@nightwatch/db';
import { Message, User } from 'discord.js'
import axios from 'axios'
import { Plugin } from '../'
import { Logger } from '@nightwatch/util'
import { User as NightwatchUser } from '@nightwatch/db'

const timeForExp = 60 * 1000;
const minExpPerMessage = 15;
const maxExpPerMessage = 25;
const timeForExp = 60 * 1000
const minExpPerMessage = 15
const maxExpPerMessage = 25

const getRandomNumber = (min: number, max: number) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
return Math.floor(Math.random() * (max - min + 1)) + min
}

const getXpForLevel = (level: number) => {
return 5 * level * level + 50 * level + 100;
};
return 5 * level * level + 50 * level + 100
}

export const giveXp = async (user: NightwatchUser, message: Message) => {
if (!user.settings.levelsEnabled) {
return;
return
}

const { api } = Module.config;
const baseRoute = `${api.address}/users`;
const { api } = Plugin.config
const baseRoute = `${api.address}/users`

const timeDiff: number = Date.now() - new Date(user.level.timestamp).getTime();
const timeDiff: number = Date.now() - new Date(user.level.timestamp).getTime()

if (timeDiff < timeForExp) {
return;
return
}

const entry: { xp: number; level: number } = user.level;
const entry: { xp: number; level: number } = user.level

let experience: number = entry.xp;
let level: number = entry.level;
let experienceNext: number = getXpForLevel(level);
let leveledup: boolean = false;
const expGain: number = getRandomNumber(maxExpPerMessage, minExpPerMessage);
let experience: number = entry.xp
let level: number = entry.level
let experienceNext: number = getXpForLevel(level)
let leveledup: boolean = false
const expGain: number = getRandomNumber(maxExpPerMessage, minExpPerMessage)

experience += expGain;
experience += expGain

while (experience >= experienceNext) {
experience -= experienceNext;
experienceNext = getXpForLevel(level);
level++;
leveledup = true;
experience -= experienceNext
experienceNext = getXpForLevel(level)
level++
leveledup = true
}

const route = `${baseRoute}/${message.author.id}/level?token=${api.token}`;
const route = `${baseRoute}/${message.author.id}/level?token=${api.token}`

if (leveledup) {
const popcornEmoji = '🍿';
const dollarEmoji = '💵';
const rewardAmount = getRandomNumber(45, 50) + Math.floor(level * 0.5);
const popcornEmoji = '🍿'
const dollarEmoji = '💵'
const rewardAmount = getRandomNumber(45, 50) + Math.floor(level * 0.5)
message.channel.send(
`**${popcornEmoji} | ${message.member
.displayName} just advanced to level ${level} and earned ${dollarEmoji} ${rewardAmount} credits!**`
);
)

user.balance.balance += rewardAmount;
user.balance.netWorth += rewardAmount;
user.balance.balance += rewardAmount
user.balance.netWorth += rewardAmount

const postData = {
level: {
Expand All @@ -71,18 +71,18 @@ export const giveXp = async (user: NightwatchUser, message: Message) => {
netWorth: user.balance.netWorth,
dateLastClaimedDailies: user.balance.dateLastClaimedDailies
}
};
}

axios.put(route, postData).catch(Logger.error);
return;
axios.put(route, postData).catch(Logger.error)
return
}

const postData = {
level: {
xp: experience,
level
}
};
}

axios.put(route, postData).catch(Logger.error);
};
axios.put(route, postData).catch(Logger.error)
}
1 change: 0 additions & 1 deletion tslint.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"no-unused-expression": true,
"no-unused-variable": true,
"no-unnecessary-qualifier": true,
"semicolon": true,
"space-before-function-paren": false
},
"rulesDirectory": [
Expand Down

0 comments on commit f614785

Please sign in to comment.