Skip to content

Commit

Permalink
Merge pull request #29 from aNickzz/rc/0.7.0
Browse files Browse the repository at this point in the history
rc/0.7.0
  • Loading branch information
OldStarchy authored Aug 2, 2020
2 parents 74aac17 + e37e979 commit 0ecd737
Show file tree
Hide file tree
Showing 56 changed files with 3,675 additions and 145 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
"Glowstone",
"Greenlock",
"Minecraft's",
"Mineflayer",
"Pufferfish",
"Redstone",
"composter",
"hmsp",
"keybind",
"luxon",
Expand All @@ -31,5 +33,6 @@
"npm.packageManager": "yarn",
"eslint.packageManager": "yarn",
"prettier.packageManager": "yarn",
"typescript.disableAutomaticTypeAcquisition": false
"typescript.disableAutomaticTypeAcquisition": false,
"editor.formatOnSave": true
}
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dashbot",
"version": "0.6.0",
"version": "0.7.0",
"main": "index.js",
"author": "Nicholas Sorokin <[email protected]>",
"license": "Apache-2.0",
Expand All @@ -26,8 +26,11 @@
"greenlock-express": "^4.0.3",
"haiku-random": "^1.0.0",
"luxon": "^1.24.1",
"mineflayer": "^2.23.0",
"node-fetch": "^2.6.0",
"tail": "^2.0.3",
"throttle-debounce": "^2.2.1",
"typed-emitter": "^1.2.0",
"typescript": "^3.7.5",
"winston": "^3.2.1"
},
Expand All @@ -41,6 +44,7 @@
"@types/mocha": "^7.0.2",
"@types/node-fetch": "^2.5.4",
"@types/tail": "^2.0.0",
"@types/throttle-debounce": "^2.1.0",
"@types/ws": "^7.2.3",
"@typescript-eslint/eslint-plugin": "^2.18.0",
"@typescript-eslint/parser": "^2.18.0",
Expand All @@ -55,5 +59,8 @@
"prettier": "^1.19.1",
"source-map-support": "^0.5.16",
"ts-node": "^8.6.2"
},
"resolutions": {
"vec3": "0.1.6"
}
}
1 change: 1 addition & 0 deletions src/ChatServer/ChatServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default interface ChatServer<
> extends EventEmitter<ChatServerEvents> {
readonly id: string;
readonly me: Readonly<TIdentity>;
readonly isConnected: boolean;
connect(): Promise<void>;
disconnect(): Promise<void>;
getAudioChannels(): Promise<AudioChannel[]>;
Expand Down
37 changes: 31 additions & 6 deletions src/Command.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
import Message from './ChatServer/Message';

export default interface Command {
run(
message: Message | null,
name: string,
...args: string[]
): Promise<void>;
export default abstract class Command {
abstract readonly name: string;
readonly alias: string[] | null = null;
abstract readonly description: string;

getUsage(): string {
return `${this.name}${
this.alias !== null ? ` (${this.alias.join(', ')})` : ``
}:\n${this.description}`;
}
abstract run(message: Message, ...args: string[]): Promise<void>;
}

export class CommandSet {
readonly commands: Command[] = [];

add(command: Command) {
this.commands.push(command);
}

async run(message: Message, commandName: string, args: string[]) {
for (const command of this.commands) {
if (
command.name === commandName ||
command.alias?.includes(commandName)
) {
await command.run(message, ...args);
return;
}
}
}
}
18 changes: 10 additions & 8 deletions src/Commands/EchoCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import Message from '../ChatServer/Message';
import Command from '../Command';
import Permissions from '../Permissions';

export default class EchoCommand implements Command {
constructor(private readonly permissions: Permissions) {}
async run(message: Message | null, command: string) {
export default class EchoCommand extends Command {
readonly name = 'echo';
readonly description = 'Says what you say';

constructor(private readonly permissions: Permissions) {
super();
}
async run(message: Message) {
if (message === null) {
return;
}
Expand All @@ -18,11 +23,8 @@ export default class EchoCommand implements Command {
return;
}

const content =
command === 'echoraw'
? message.rawContent.substr('!echoraw'.length).trimLeft()
: message.textContent.substr('!echo'.length).trimLeft();
const content = message.textContent.substr('!echo'.length).trimLeft();

await message.channel.sendText('`' + content + '`');
await message.channel.sendText(content);
}
}
5 changes: 4 additions & 1 deletion src/Commands/HaikuCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { random as getHaiku } from 'haiku-random';
import Message from '../ChatServer/Message';
import Command from '../Command';

export default class HaikuCommand implements Command {
export default class HaikuCommand extends Command {
readonly name = 'haiku';
readonly description = 'Gets you a random haiku';

async run(message: Message | null) {
if (message === null) {
return;
Expand Down
6 changes: 5 additions & 1 deletion src/Commands/HelpCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ interface HelpCommandSession {
/**
* Placeholder help action supposed to give people hints as to what DashBot can do, however due to the "conversational"-like invocation phrases I feel like it doesn't really make sense to just list all the "commands" so not much work has been put in this.
*/
export default class HelpCommand implements Command, Interaction {
export default class HelpCommand extends Command implements Interaction {
readonly name = 'help';
readonly description = 'Shows help for available commands';

private static readonly defaultSession: Readonly<HelpCommandSession> = {
pendingAnswer: false,
sentMessageTime: 0,
Expand All @@ -46,6 +49,7 @@ export default class HelpCommand implements Command, Interaction {
private readonly _session: SessionStore<HelpCommandSession>;

constructor(private storage: StorageRegister) {
super();
this._session = new SessionStore(storage);
}

Expand Down
9 changes: 7 additions & 2 deletions src/Commands/IdCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import Command from '../Command';
import Permissions from '../Permissions';
import formatTable from '../util/formatTable';

export default class IdCommand implements Command {
constructor(private readonly permissions: Permissions) {}
export default class IdCommand extends Command {
readonly name = 'id';
readonly description = 'Shows you your identity information';

constructor(private readonly permissions: Permissions) {
super();
}
async run(message: Message | null) {
if (message === null) {
return;
Expand Down
9 changes: 7 additions & 2 deletions src/Commands/JokeCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import Command from '../Command';
/**
* Performs an Imgur search and posts a random result
*/
export default class JokeCommand implements Command {
constructor(private readonly _jokes: ICanHazDadJokeClient) {}
export default class JokeCommand extends Command {
readonly name = 'joke';
readonly description = 'Gets you an amazingly funny joke (sometimes).';

constructor(private readonly _jokes: ICanHazDadJokeClient) {
super();
}

async run(message: Message | null) {
if (message === null) {
Expand Down
52 changes: 52 additions & 0 deletions src/Commands/LoginCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import winston from 'winston';
import ChatServer from '../ChatServer/ChatServer';
import Message from '../ChatServer/Message';
import Command from '../Command';
import DashBot from '../DashBot';

export default class LoginCommand extends Command {
readonly name = 'login';
readonly description = 'Logs dashbot in to a server';

constructor(private bot: DashBot) {
super();
}

async run(message: Message, serverId?: string) {
const servers = this.bot.servers.filter(server => !server.isConnected);
if (servers.length === 0) {
await message.channel.sendText('No servers to login to.');
return;
}

let server: ChatServer | null = null;
if (!serverId) {
if (servers.length > 1) {
await message.channel.sendText('Which server?');
return;
}

server = servers[0];
} else {
server =
servers.filter(server => server.id === serverId).shift() ??
null;

if (!server) {
await message.channel.sendText("Couldn't find that server");
return;
}
}
message.channel.sendText('Connecting to server...');
try {
//TODO: this is weird, shouldn't need both of these calls
await server.connect();
await server.awaitConnected();

await message.channel.sendText('Connected!');
} catch (e) {
winston.error("Couldn't log in to server on request");
await message.channel.sendText("Couldn't connect! :(");
}
}
}
55 changes: 55 additions & 0 deletions src/Commands/LogoutCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import winston from 'winston';
import ChatServer from '../ChatServer/ChatServer';
import Message from '../ChatServer/Message';
import Command from '../Command';
import DashBot from '../DashBot';

export default class LogoutCommand extends Command {
readonly name = 'logout';
readonly description =
'Logs out of the given server (or the current one if no server was specified)';

constructor(private bot: DashBot) {
super();
}

async run(message: Message, serverId?: string) {
const servers = this.bot.servers.filter(server => server.isConnected);

if (servers.length === 1) {
await message.channel.sendText(
"Not logging out, this is the only server I'm connected to"
);
return;
}

let server: ChatServer | null = null;
if (!serverId) {
server = message.channel.server;
} else {
server =
servers.filter(server => server.id === serverId).shift() ??
null;

if (!server) {
await message.channel.sendText("Couldn't find that server");
return;
}
}

message.channel.sendText('Disconnecting');
try {
await server.disconnect();

if (message.channel.server.isConnected) {
await message.channel.sendText('Connected!');
}
} catch (e) {
winston.error("Couldn't log in to server on request");

if (message.channel.server.isConnected) {
await message.channel.sendText("Couldn't disconnect! :(");
}
}
}
}
9 changes: 7 additions & 2 deletions src/Commands/PermissionCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ enum PermissionsPermissions {
MANAGE_ADMINS = 'permissions.manage-admins',
}

export default class PermissionCommand implements Command {
constructor(private readonly permissions: Permissions) {}
export default class PermissionCommand extends Command {
readonly name = 'permission';
readonly description = '(WIP) Handles assigning permissions to people';

constructor(private readonly permissions: Permissions) {
super();
}

private parseIdentity(identity: Identity, str: string) {
if (str === 'me') {
Expand Down
6 changes: 5 additions & 1 deletion src/Commands/PetCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ interface PetActionStorage {
petsPerPerson: Record<string, number>;
}

export default class PetCommand implements Command, StatisticProvider {
export default class PetCommand extends Command implements StatisticProvider {
readonly name = 'pet';
readonly description = 'Shows dashbot you care';

private _timesPet = 0;
private _timesPetToday = 0;
private _timesPetTodayDate = PetCommand.getDateKey();
Expand All @@ -22,6 +25,7 @@ export default class PetCommand implements Command, StatisticProvider {
private _store: PersistentData<PetActionStorage>;

constructor(storage: StorageRegister) {
super();
this._store = storage.createStore('PetAction');
this._store.on('dataLoaded', this.onReadData.bind(this));
}
Expand Down
5 changes: 4 additions & 1 deletion src/Commands/PickCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ const grammar = {
*
* `!poll "this is my question" yes no maybe "i don't know".`
*/
export default class PickCommand implements Command {
export default class PickCommand extends Command {
readonly name = 'pick';
readonly description = 'Chooses a random answer for you';

async run(message: Message, _: string, ...args: string[]) {
if (message === null) {
return;
Expand Down
13 changes: 6 additions & 7 deletions src/Commands/PollCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ const grammar = {
};

/**
* Allows you to create polls (much like other existing poll bots).
*
* `!poll "this is my question" yes no maybe "i don't know".`
*/
export default class PollCommand implements Command {
export default class PollCommand extends Command {
readonly name = 'poll';
readonly description =
'Allows you to create polls (much like other existing poll bots).\n' +
'`!poll "this is my question" yes no maybe "i don\'t know".`';

static readonly answersEmoji = [
Emoji.ZERO,
Emoji.ONE,
Expand All @@ -42,9 +44,6 @@ export default class PollCommand implements Command {
];

async run(message: Message, _: string, ...args: string[]) {
if (message === null) {
return;
}
const channel = message.channel;
if (!channel.supportsReactions) {
await channel.sendText("This chat does't support polls");
Expand Down
Loading

0 comments on commit 0ecd737

Please sign in to comment.