Skip to content

Commit

Permalink
Merge pull request #6 from aXises/v2
Browse files Browse the repository at this point in the history
Merge V2 documentation and formatting
  • Loading branch information
fatton139 authored Jul 28, 2018
2 parents 0e46504 + c2dd3e4 commit c257fb2
Show file tree
Hide file tree
Showing 41 changed files with 907 additions and 52 deletions.
112 changes: 105 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,114 @@
# Fortnite Bot
A bot which pings selected targets whenever fortnite is mentioned. Use at your own risk.

![CodeFactor](https://www.codefactor.io/repository/github/axises/fortnitebot/badge) [![codecov](https://codecov.io/gh/aXises/fortniteBot/branch/master/graph/badge.svg)](https://codecov.io/gh/aXises/fortniteBot)
[![Maintainability](https://api.codeclimate.com/v1/badges/51cbd263ff1f0afff332/maintainability)](https://codeclimate.com/github/aXises/fortniteBot/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/51cbd263ff1f0afff332/test_coverage)](https://codeclimate.com/github/aXises/fortniteBot/test_coverage)

A bot which pings selected targets whenever fortnite is mentioned. Use at your own risk. Despite its name, the main focus of the bot is to provide a flexible command system so that fun commands can be easily added to provide entertaining interactions between users and the bot.

## Requirements
- Node 6 or higher
- TypeScript 2
- tslint is recommended for style consistency

## Build
Clone the repository `git clone https://github.com/aXises/fortniteBot`

Install required packages `npm install`

Install TypeScript `npm install -g typescript`

Nodemon is recommended `npm install -g nodemon`

Compile TypeScript `tsc`

Configure API keys in fortniteBot.ts or with .env file

Running the application `node fortniteBot.js` or `nodemon fortniteBot.js`

## Creating commands
It is recommended to place these in a seperate file.
```
// MyCustomCommands.ts
// Examples to demonstrate adding different types of commands.
import * as Discord from "discord.js";
import { FortniteBotAction } from "../action/FortniteBotAction";
import { ExecutableCommand } from "../command/ExecutableCommand";
import { AutoTriggerCommand } from "../command/AutoTriggerCommand";
import { FortniteBotTrigger } from "../action/FortniteBotTrigger";
// Example Executable Command.
// Example action. Takes 1 argument and sends it back to the channel.
const testCommandAction = new FortniteBotAction(1, (state, args) => {
const m: Discord.Message = state.getHandle();
if (args.length !== 1) {
return false; // Command failed.
}
m.channel.send(args[0]);
return true; // Command was successfully executed.
});
// To execute the command, call '!f testcommand'. Anyone with access level 0 can execute this command.
const testCommand = new ExecutableCommand("testcommand", 0, testCommandAction);
// Example Auto Trigger Command
// Example Triggers.
const trigger1 = new FortniteBotTrigger((state) => {
return Math.random() > 0.5; // Action will be executed if true. (50%)
});
const trigger2 = new FortniteBotTrigger((state) => {
const m: Discord.Message = state.getHandle();
return m.content === "egg"; // Action will be executed whenever the message is "egg".
});
// Example Action for auto trigger.
const testAutoAction = new FortniteBotAction(1, (state) => {
const m: Discord.Message = state.getHandle();
m.channel.send("This action was triggered");
return true;
});
// Set up the command with different triggers.
const autoCommand1 = new AutoTriggerCommand(0, testAutoAction, trigger1);
const autoCommand2 = new AutoTriggerCommand(0, testAutoAction, trigger2);
// Export all the commands.
export const myCustomCommands = [testCommand, autoCommand1, autoCommand2];
// Import in to the command manager with CommandManager.addBulkCommand() or CommandManager.addCommand().
// See FortniteBotEventCore.ts for example.
```


## Commands
```
!fortnite - Asks your targets to play fortnite.
!fortnite tts - Asks your targets to play fortnite but more nicely.
!fortnite target {@target1} {@target2} ... {@targetn} - Set new targets/friends.
!fortnite auto {amount} {delay(ms)} - Ask your friends many times
!fortnite delete - Deletes all messages sent by the bot.
Standard Commands
!f - Asks your targets to play fortnite.
!f ping - Check for response.
!f help - Displays help message.
!f target @target - Adds target to the targetlist.
!f targetlist - Shows the current targets.
!f removeself - Removes yourself from the targetlist.
!f auto amount delay(s) - Automatically ping the targets a set amount of times with an delay.
User Commands
!f register - Registers yourself.
!f profile - Check your profile.
!f daily - Grab daily rewards.
Shop Commands
!f shoplist - Displays all available shops.
!f viewshop shopname - View a shop.
!f buy index/itemname from shopname - Buy an item from a shop.
...More coming soon.
```

## Contributing
~~Don't.~~
All contribtions are welcome. Adhering to tslint style is recommeded.

## Disclaimer
I do not play nor have any affiliation with Fortnite. This application is a simple tool to ~~spam~~ ask your friends/server members.
4 changes: 2 additions & 2 deletions fortniteBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ dotenvConfig();
const keys = {
discordToken: process.env.discordToken,
chatBotUserId: process.env.chatBotUserId,
chatBotAPIKey: process.env.chatBotAPIKey
chatBotApiKey: process.env.chatBotApiKey
};

const initConfig = new FortniteBotInitConfig(keys.discordToken,
keys.chatBotUserId, keys.chatBotAPIKey);
keys.chatBotUserId, keys.chatBotApiKey);

export const fortniteBotCore = new FortniteBotCore(initConfig);

Expand Down
20 changes: 20 additions & 0 deletions src/action/FortniteBotAction.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
import { FortniteBotState } from "../state/FortniteBotState";

export class FortniteBotAction {
/**
* Number of arguments this action requires.
*/
public argLength: number;

/**
* An action to invoke.
*/
public action: (stateHandle: FortniteBotState, args: string[]) => boolean;

/**
* @classdesc Base class for a standard action executed by the bot.
* @param argLength - Number of arguments the action requires.
* @param action - An action to invoke.
*/
public constructor(argLength: number,
action: (stateHandle: FortniteBotState,
args: string[]) => boolean) {
this.argLength = argLength;
this.action = action;
}

/**
* Executes the action with a set of arguments.
* @param state - A Handle for the action to bind to.
* @param args - Arguments to execute the action with.
* @returns true if the command was successfully executed.
*/
public execute(state: FortniteBotState, args: string[]): boolean {
return this.action(state, args);
}
Expand Down
10 changes: 10 additions & 0 deletions src/action/FortniteBotTrigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@ import { FortniteBotAction } from "./FortniteBotAction";
import { FortniteBotState } from "../state/FortniteBotState";

export class FortniteBotTrigger extends FortniteBotAction {
/**
* @classdesc Base trigger class, invokes an FortniteBotAction if conditions are met.
* @param trigger - A trigger to invoke, returns true if conditions are met.
*/
public constructor(trigger: (state: FortniteBotState) => boolean) {
super(0, trigger);
}

/**
* Attempt to execute the trigger.
* @param state A Handle for the trigger to bind to.
* @returns true if the command was successfully executed.
*/
public execute(state: FortniteBotState): boolean {
return super.execute(state, null);
}
Expand Down
5 changes: 5 additions & 0 deletions src/action/RequestResponseAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { FortniteBotState } from "../state/FortniteBotState";
import { FortniteBotAction } from "./FortniteBotAction";

export class RequestResponseAction extends FortniteBotAction {
/**
* @classdesc An action which requires a user response.
* @param argLength - Number of arguments the action requires.
* @param action - An action to invoke.
*/
public constructor(argLength: number,
action: (stateHandle: FortniteBotState,
args: string[]) => boolean) {
Expand Down
19 changes: 15 additions & 4 deletions src/command/AutoTriggerCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,28 @@ import { FortniteBotAction } from "action/FortniteBotAction";
import { FortniteBotTrigger } from "action/FortniteBotTrigger";
import { fortniteBotCore as activeCore } from "../../fortniteBot";

/**
* Commands which are auto triggered.
*/

export class AutoTriggerCommand extends Command implements ICommand {
/**
* The condition required to execute the action.
*/
public trigger: FortniteBotTrigger;

/**
* @classdesc Commands which are triggered without user directly calling it.
* @param accessLevel - The required access level to execute this command.
* @param action - The action to execute.
* @param trigger - The condition required to execute the action.
*/
public constructor(accessLevel: number,
action: FortniteBotAction, trigger: FortniteBotTrigger) {
super(null, accessLevel, action);
this.trigger = trigger;
}

/**
* Attempt to execute the trigger.
* @returns true if conditions of the trigger has been met.
*/
public tryTrigger(): boolean {
return this.trigger.execute(activeCore.getCoreState());
}
Expand Down
33 changes: 32 additions & 1 deletion src/command/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,52 @@ import { fortniteBotCore } from "../../fortniteBot";
import { fortniteBotCore as activeCore } from "../../fortniteBot";

export class Command implements ICommand {
/**
* The string required to execute this command.
*/
public commandString?: string;

/**
* The required access level to execute this command.
*/
public readonly accessLevel: number;

/**
* An action to execute.
*/
public action: FortniteBotAction;

/**
* Arguments to execute the action with.
*/
public args: string[];

/**
* @classdesc Base command class for the bot.
* @param commandString - The string required to execute this command.
* @param accessLevel - The required access level to execute this command.
* @param action - The action to execute.
*/
public constructor(commandString: string, accessLevel: number,
action: FortniteBotAction) {
this.action = action;
this.accessLevel = accessLevel;
this.commandString = commandString;
}

/**
* Changes the arguments of the command.
* @param args - New arguments for the command.
*/
public setArgs(args: string[]): void {
this.args = args;
}
public executeAction(user: User): void {

/**
* Execute the action provided by this command.
* @param user - The user attempting to execute this command.
*/
public executeAction(user: User): void {
if (user.accessLevel < this.accessLevel) {
const m = activeCore.getCoreState().getHandle() as Discord.Message;
m.reply(
Expand Down
Loading

0 comments on commit c257fb2

Please sign in to comment.