Skip to content

Commit

Permalink
init project
Browse files Browse the repository at this point in the history
  • Loading branch information
ReDBrother committed Feb 11, 2024
1 parent d384b5b commit fd74b2e
Show file tree
Hide file tree
Showing 35 changed files with 6,723 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-env"]
}
1 change: 1 addition & 0 deletions .bashrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
set -o v
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root = true

[*]
quote_type = single
indent_style = space
indent_size = 2
end_of_line = if
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{md, markdown}]
trim_trailing_whitespace = false

[Makefile]
indent_style = tab
1 change: 1 addition & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes #**ISSUE_ID**
68 changes: 68 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Build main

on:
push:
branches:
- main
jobs:
build:
if: github.repository == 'hexlet-codebattle/codebattle-bot'
runs-on: ubuntu-latest

services:
db:
image: postgres:12-alpine
ports: ['5432:5432']
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}

- name: Get yarn cache
id: yarn-cache
run: echo "::set-output name=dir::${yarn cache dir}"

- uses: actions/cache@v3
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install yarn dependencies
run: yarn install --froze-lockfile

- name: Eslint
run: yarn lint

- name: Prettify code
uses: creyD/[email protected]
with:
prettier_options: --write **/*.{js,md}

- name: Run jest
run: yarn test

# - name: Setup db

- name: Login to Docker Hub
run: echo "${{ services.DOCKER_PASSWORD }}" | docker login -u ${{ secrets.DOCKER_USERNAME }} --pasword-stdin

- name: Build docker image for codebattle bot
run: make docker-build-chat-bot
- name: Push docker image for codebattle bot
run: make docker-push-chat-bot

# - name:
# run:
# uses:
# with:
53 changes: 53 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Build PR

on:
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

services:
db:
image: 'postgres:12-alpine'
ports: ['5432:5432']
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}

- name: Get yarn cache
id: yarn-cache
run: echo "::set-output name=dir::${yarn cache dir}"

- uses: actions/cache@v3
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install yarn dependencies
run: yarn install --froze-lockfile

- name: Eslint
run: yarn lint

- name: Prettify code
uses: creyD/[email protected]
with:
prettier_options: --write **/*.{js,md}

- name: Run jest
run: yarn test
28 changes: 28 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
deps
.idea
.vscode
tmp
/tmp/
*.ez
*.retry
*.log
*.swp
*.swo
**/__pycache__

node_modules

tags
.env
*.DS_Store
.deliver/config

*.db

.vagrant
.terraform
.cache-loader
secrets.auto.tfvars
*.secret.yml
stats.json
google.key.json
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarn test
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
coverage
2 changes: 2 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nodejs: 20.11.0

Empty file added CHANGELOG.md
Empty file.
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM node:20.11.0 AS bot-image

ENV NODE_ENV=production

WORKDIR /opt/app
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
define GetFromPkg
$(shell node -p "require('./package.json').$(1)")
endef

BOT_VERSION := $(call GetFromPkg,version)

setup: setup-env compose-setup

setup-env:
docker run --rm -v $(CURDIR):/app -w /app williamyeh/ansible:alpine3 ansible-playbook ansible/development.yml -i ansible/development -vv

setup-env-local:
ansible-playbook ansible/development.yml -i ansible/development -vv

docker-build-bot:
docker pull codebattle/chat:latest || true
docker build --target bot-image \
--cache-from=codebattle/chat:$(BOT_VERSION) \
--cache-from=codebattle/chat:latest \
--file Dockerfile \
--tag codebattle/chat:latest app \
--tag codebattle/chat:$(BOT_VERSION)

docker-push-bot:
docker push codebattle/chat:$(BOT_VERSION)
docker push codebattle/chat:latest
11 changes: 11 additions & 0 deletions ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[defaults]
roles_path = tmp/ansible-roles
gathering = smart
fact_caching = jsonfile
fact_caching_connection = tmp/ansible-cached-facts
fact_caching_timeout = 86400
host_key_checking = False

[ssh_connection]
ssh_args = -o ForwardAgent=yes

7 changes: 7 additions & 0 deletions ansible/development.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- hosts: all
gather_facts: no
tasks:
- template:
src: environment.j2
dest: '../.env'
12 changes: 12 additions & 0 deletions ansible/development/group_vars/all/vars.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
node_env: 'delelopment'
bot_port: 5000
bot_codebattle_socket_token: 'chat_bot'
bot_codebattle_socket_url: 'ws://localhost:4000/chat_bot'
codebattle_db_name: 'codebattle_dev'
codebattle_db_hostname: 'db'
codebattle_db_username: 'postgres'
codebattle_db_password: 'postgres'
codebattle_db_port: 5432
telegram_bot_token: null
discord_bot_token: null
discord_bot_client_id: null
1 change: 1 addition & 0 deletions ansible/development/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
localhost ansible_connection=local
12 changes: 12 additions & 0 deletions ansible/templates/environment.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
NODE_ENV:{{ node_env }}
BOT_PORT:{{ bot_port }}
BOT_CODEBATTLE_SOCKET_TOKEN:{{ bot_codebattle_socket_token }}
BOT_CODEBATTLE_SOCKET_URL:{{ bot_codebattle_socket_url }}
CODEBATTLE_DB_NAME:{{ codebattle_db_name }}
CODEBATTLE_DB_HOSTNAME:{{ codebattle_db_hostname }}
CODEBATTLE_DB_USERNAME:{{ codebattle_db_username }}
CODEBATTLE_DB_PASSWORD:{{ codebattle_db_password }}
CODEBATTLE_DB_PORT:{{ codebattle_db_port }}
TELEGRAM_BOT_TOKEN:{{ telegram_bot_token }}
DISCORD_BOT_TOKEN:{{ discord_bot_token }}
DISCORD_BOT_CLIENT_ID:{{ discord_bot_client_id }}
3 changes: 3 additions & 0 deletions app/__tests__/telegram.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test("telegram bot running", () => {

});
Empty file added app/discord/channel/index.js
Empty file.
15 changes: 15 additions & 0 deletions app/discord/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const names = {
ping: 'ping',
};

const commands = {
[names.ping]: {
name: names.ping,
description: 'Replies with Pong!',
callback: async (interaction) => {
await interaction.reply('Pong!');
},
},
};

export default commands;
62 changes: 62 additions & 0 deletions app/discord/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Client, GatewayIntentBits } from 'discord.js';
import { REST, Routes } from 'discord.js';

import logger from '../logger';
import socket from '../socket';

import commands from './commands';
import { intersection } from 'lodash';

// https://github.com/discordjs/discord.js/tree/main/packages/discord.js#readme

const noop = () => {};

const setupSlashCommands = async ({ token, clientId }) => {
const rest = new REST({ version: '10' }).setToken(token);

try {
logger.info('Started refreshing application (/) commands.');

await rest.put(Routes.applicationCommands(clientId), {
body: Object.values(commands),
});

logger.info('Successfully reloaded application (/) commands.');
} catch (error) {
logger.error(JSON.stringify(error, null, 2));
}
};

const setup = (params) => {
const { token, env } = params;

if (!token && env !== 'development') {
throw new Error("Token for discord bot doesn't exists");
}

if (!token) {
logger.error("Token for discord bot doesn't exists");
return;
}

setupSlashCommands(params);

const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.on('ready', () => {
logger.info(`Logged in as ${client.user.tag}!`);
});

client.on('interactionCreate', async (interaction) => {
if (!interaction.isChatInputCommand()) return;

const callback = commands[interaction.commandName]?.callback || noop;
callback(intersection);
});

client.login(token);

return client;
};

export default setup;
Loading

0 comments on commit fd74b2e

Please sign in to comment.