-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d384b5b
commit fd74b2e
Showing
35 changed files
with
6,723 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["@babel/preset-env"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
set -o v |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fixes #**ISSUE_ID** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
yarn test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
build | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
nodejs: 20.11.0 | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
localhost ansible_connection=local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
test("telegram bot running", () => { | ||
|
||
}); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.