Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sudojunior committed Dec 25, 2021
0 parents commit 78f402b
Show file tree
Hide file tree
Showing 12 changed files with 414 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
45 changes: 45 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module.exports = {
env: {
commonjs: true,
es6: true,
node: true
},
extends: ['eslint:recommended', 'prettier/@typescript-eslint', 'plugin:prettier/recommended'],
globals: {
NodeJS: true,
BigInt: true
},
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 6,
sourceType: 'module'
},
plugins: ['@typescript-eslint'],
rules: {
'prettier/prettier': 'warn',
'no-cond-assign': [2, 'except-parens'],
'no-unused-vars': 0,
'@typescript-eslint/no-unused-vars': 1,
'no-empty': [
'error',
{
allowEmptyCatch: true
}
],
'prefer-const': [
'warn',
{
destructuring: 'all'
}
],
'spaced-comment': 'warn'
},
overrides: [
{
files: ['slash-up.config.js'],
env: {
node: true
}
}
]
};
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Packages
node_modules/
yarn.lock
package_lock.json

# Log files
logs/
*.log

# Miscellaneous
.tmp/
.vscode/**/*
!.vscode/extensions.json
.env
dist/
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"useTabs": false,
"trailingComma": "none",
"printWidth": 120
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Junior

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.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# interaction-prototypes
> Generated from `slash-create-template` using [`slash-up`](https://github.com/Snazzah/slash-up).
A collection of interaction prototypes made purely for testing, with the intention of using them for services in the future.

## [Game lobby handler](./src/commands/game.ts)

- Lobby names use the NATO phonetic alphabet.
- `#initialRoster` will clone itself before being used for rendering the list of players. Since it uses 3 inline fields to render the list, it will determine where each player goes - for mobile compatibility, it will render the numbers vertically.
> Originally it was intended to use `index % 3` but it now uses `index / 3` to ensure numbers are grouped together.
- It will not allow players to join the lobby if it is full, and it will not allow the host to leave the lobby.
- Components will enable themselves once their condition has been met.
35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "interaction-prototypes",
"version": "1.0.0",
"license": "MIT",
"description": "A collection of interaction prototypes made purely for testing.",
"main": "dist/index.js",
"scripts": {
"sync": "slash-up sync",
"sync:dev": "slash-up sync -e development",
"start": "cd dist && node index.js",
"build": "npx tsc",
"lint": "npx eslint --ext .ts ./src",
"lint:fix": "npx eslint --ext .ts ./src --fix"
},
"dependencies": {
"cat-loggr": "^1.1.0",
"dotenv": "^8.2.0",
"eris": "^0.16.1",
"fastify": "^3.9.2",
"slash-create": "^4.0.1"
},
"devDependencies": {
"@types/express": "^4.17.11",
"@types/node": "^14.14.37",
"@typescript-eslint/eslint-plugin": "^4.19.0",
"@typescript-eslint/parser": "^4.19.0",
"eslint": "^7.15.0",
"eslint-config-prettier": "^7.0.0",
"eslint-plugin-prettier": "^3.3.0",
"prettier": "^2.2.1",
"slash-up": "^1.0.3",
"ts-node": "^9.1.1",
"typescript": "^4.2.3"
}
}
9 changes: 9 additions & 0 deletions pm2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"apps": [
{
"name": "slash-commands",
"script": "node",
"args": "dist/index.js"
}
]
}
19 changes: 19 additions & 0 deletions slash-up.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// This is the slash-up config file.
// Make sure to fill in "token" and "applicationId" before using.
// You can also use environment variables from the ".env" file if any.

module.exports = {
// The Token of the Discord bot
token: process.env.DISCORD_BOT_TOKEN,
// The Application ID of the Discord bot
applicationId: process.env.DISCORD_APP_ID,
// This is where the path to command files are, .ts files are supported!
commandPath: './src/commands',
// You can use different environments with --env (-e)
env: {
development: {
// The "globalToGuild" option makes global commands sync to the specified guild instead.
globalToGuild: process.env.DEVELOPMENT_GUILD_ID
}
}
};
175 changes: 175 additions & 0 deletions src/commands/game.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import {
SlashCommand,
SlashCreator,
CommandContext,
ComponentType,
ButtonStyle,
MessageOptions,
ApplicationCommandType,
Member
} from 'slash-create';

export default class HelloCommand extends SlashCommand {
names = [
'Alpha',
'Bravo',
'Charlie',
'Delta',
'Echo',
'Foxtrot',
'Golf',
'Hotel',
'India',
'Juliet',
'Kilo',
'Lima',
'Mike',
'November',
'Oscar',
'Papa',
'Quebec',
'Romeo',
'Sierra',
'Tango',
'Uniform',
'Victor',
'Whiskey',
'X-ray',
'Yankee',
'Zulu'
];

get initialRoster() {
return Array.from({ length: 3 }, () => ({
name: '\u200b',
value: '\u200b',
inline: true
})).slice();
}

constructor(creator: SlashCreator) {
super(creator, {
name: 'new-game',
description: 'Start a new game',
type: ApplicationCommandType.CHAT_INPUT
});
}

randomName() {
return this.names[Math.floor(Math.random() * this.names.length)];
}

buildPost(players: Member[], title: string): MessageOptions {
// index 0 of players is assumed to be the host
const host = players[0];

return {
content: ':satellite: **| A new game is starting!**',
embeds: [
{
title,
author: {
name: host.nick || host.user.username,
icon_url: host.avatarURL
},
fields: players.reduce((fields, player, index) => {
if (index === 0) {
fields[0].name = `Roster (${players.length} / 15)`;
}

const fieldIndex = Math.floor(index / 3);

fields[fieldIndex].value += `[\`${(index + 1).toString().padStart(2, '0')}\`] ${player.mention}\n`;

return fields;
}, this.initialRoster)
}
],
components: [
{
type: ComponentType.ACTION_ROW,
components: [
{
type: ComponentType.BUTTON,
label: 'Join',
custom_id: 'join',
disabled: players.length >= 15,
style: ButtonStyle.PRIMARY,
emoji: {
name: '📥'
}
},
{
type: ComponentType.BUTTON,
label: 'Leave',
custom_id: 'leave',
disabled: players.length <= 1,
style: ButtonStyle.DESTRUCTIVE,
emoji: {
name: '📤'
}
}
]
}
]
};
}

async run(ctx: CommandContext) {
if (!ctx.guildID) {
return 'This command can only be used in a server.';
}

const players = [ctx.member];

const title = `${this.randomName()} ${this.randomName()}`;

await ctx.send(this.buildPost(players, title));

await ctx.fetch();

ctx.registerComponentFrom(ctx.messageID, 'join', async (ctx) => {
// check if the user is already in the game
if (players.some((p) => p.id === ctx.member.id)) {
return ctx.send('You are already in the game.', { ephemeral: true });
}

// check if the game is full
if (players.length >= 15) {
return ctx.send('The game is full.', { ephemeral: true });
}

// add the user to the game
players.push(ctx.member);

await ctx.send('You have joined the game.', {
ephemeral: true
});

// update the game
await ctx.edit(ctx.message.id, this.buildPost(players, title));
});

ctx.registerComponentFrom(ctx.messageID, 'leave', async (ctx) => {
// check player is not the host
if (players[0].id === ctx.member.id) {
return ctx.send('You cannot leave the game as host.', { ephemeral: true });
}

// check if the user is already in the game
if (!players.some((p) => p.id === ctx.member.id)) {
return ctx.send('You are not in the game.', { ephemeral: true });
}

// remove the user from the game
players.splice(players.indexOf(ctx.member), 1);

await ctx.send('You have left the game.', {
ephemeral: true
});

// update the game
await ctx.edit(ctx.message.id, this.buildPost(players, title));
});
}
}
Loading

0 comments on commit 78f402b

Please sign in to comment.