Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tr lang #39

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,33 @@ $ yarn start
```

<br />
See available langs in `src/language.ts`. Configure your language in `main.ts` file:
See available langs in `src/language.ts`. Configure your language in `config.ts` file:

```ts
const text = langs.br; // brazilian portuguese
export const LANGUAGE = 'br'; // brazilian portuguese

// or

const text = langs.en; // english

export const LANGUAGE = 'en'; // english
```

After script run, you need to scan the QrCode in your terminal with your whatsapp! (Same proccess to join whatsapp web)

## :mag_right: Functionalities

See the current features!

- Search a song directly from youtube;
- Songs once searched are downloaded for optimized next searches;
- Send songs in private or in groups;
- Your friends can send the command too;
- <s>You can enable the "Download progress";</s>
- Prevents downloading videos more than MAX DURATION minutes long.

## :stars: Upcoming features
- New command's system
- Help command

## :stars: Upcoming features

- New error handling
- Redesigned music cache

Expand All @@ -117,6 +120,7 @@ Open a pull request with your branch. After pull request merge, you should delet
We need some translations too! Look for `language.ts` file! **It's so easy!**

## :memo: License

This project is under MIT license. See [LICENSE](https://github.com/mlg404/whatsapp-music-bot/blob/master/LICENSE) for more information.

---
Expand All @@ -126,4 +130,3 @@ Made with 💙 by Victor Eyer :wave: [Get in touch!](https://www.linkedin.com/in
[nodejs]: https://nodejs.org/
[yarn]: https://classic.yarnpkg.com/lang/en/
[vc]: https://code.visualstudio.com/
```
Empty file.
39 changes: 22 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
{
"name": "whatsapp_music_bot",
"version": "2.0.0",
"main": "main.js",
"description": "Turn your number into a DJ!",
"version": "3.1.0",
"main": "main.ts",
"license": "MIT",
"author": {
"email": "[email protected]",
"name": "Victor Eyer",
"url": "https://github.com/mlg404"
},
"scripts": {
"start": "ts-node-dev --transpile-only --ignore-watch node_modules src/main.ts"
},
"devDependencies": {
"@types/ffmpeg": "^1.0.4",
"@types/yt-search": "^2.3.0",
"@typescript-eslint/eslint-plugin": "^4.9.0",
"@typescript-eslint/parser": "^4.9.0",
"eslint": "^7.15.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-config-prettier": "^6.15.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-prettier": "^3.2.0",
"prettier": "^2.2.1",
"ts-node-dev": "^1.0.0",
"typescript": "^4.1.2"
"@typescript-eslint/eslint-plugin": "^5.28.0",
"@typescript-eslint/parser": "^5.28.0",
"eslint": "^8.17.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.0.0",
"prettier": "^2.7.0",
"ts-node-dev": "^2.0.0",
"typescript": "^4.7.3"
},
"dependencies": {
"@types/ffmpeg": "^1.0.4",
"ffmpeg": "^0.0.4",
"qrcode-terminal": "^0.12.0",
"whatsapp-web.js": "github:pedroslopez/whatsapp-web.js#multidevice",
"yt-dl-playlist": "^2.1.0",
"yt-search": "^2.10.2",
"ytdl-core": "^4.9.1"
"whatsapp-web.js": "^1.16.7",
"yt-search": "^2.10.3",
"ytdl-core": "^4.11.0"
}
}
1 change: 0 additions & 1 deletion src/@types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
declare module 'qrcode-terminal';
declare module 'yt-dl-playlist';
19 changes: 19 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Client, LocalAuth } from 'whatsapp-web.js';
import qrcode from 'qrcode-terminal';

import text from './language';
import { LANGUAGE } from './config';

const client = new Client({
puppeteer: { headless: true, args: ['--no-sandbox'] },
authStrategy: new LocalAuth(),
});
client.on('qr', qr => {
qrcode.generate(qr, { small: true });
});

client.on('ready', async () => {
console.log(text[LANGUAGE].CONNECTED);
});

export default client;
22 changes: 22 additions & 0 deletions src/commands/help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Message } from 'whatsapp-web.js';
import text from '../language';
import { LANGUAGE, PREFIX } from '../config';
import commands from '.';

export default {
run: async (message: Message, keyword: string): Promise<Message> => {
if (!keyword)
return message.reply(
`${text[LANGUAGE].AVAILABLE_COMMANDS}: ${Object.keys(commands).join(
', ',
)}`,
);
try {
const helpText = commands[`${PREFIX}${keyword}`].help;
return message.reply(helpText);
} catch (error) {
return message.reply(`${text[LANGUAGE].ERROR}`);
}
},
help: text[LANGUAGE].HELP_HELP,
};
8 changes: 8 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { PREFIX } from '../config';
import play from './play';
import help from './help';

export default {
[`${PREFIX}play`]: play,
[`${PREFIX}help`]: help,
};
27 changes: 25 additions & 2 deletions src/commands/play.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
import { MessageMedia, Message } from 'whatsapp-web.js';
import Downloader from '../services/download';
import Searcher from '../services/search';
import text from '../language';
import { LANGUAGE } from '../config';

export default {
run: async () => { },
help: {},
run: async (message: Message, keyword: string): Promise<Message> => {
const downloader = new Downloader();
const searcher = new Searcher();
try {
const { title, videoId } = await searcher.handle(keyword);
message.reply(`${text[LANGUAGE].FOUNDED} "${title}"`);

message.reply(text[LANGUAGE].DOWNLOAD_STARTED);

const music = await downloader.handle(videoId);

const media = MessageMedia.fromFilePath(music);
return message.reply(media);
} catch (error) {
console.log(error);
return message.reply(text[LANGUAGE].ERROR);
}
},
help: text[LANGUAGE].HELP_PLAY,
};
3 changes: 2 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const MAX_DURATION = 900;
export const TESTE = 900;
export const DOWNLOAD_PATH = 'downloads';
export const PREFIX = '!';
export const LANGUAGE = 'en';
62 changes: 31 additions & 31 deletions src/language.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
const br = {
MESSAGE_CONNECTED: 'Conectado com sucesso!',
MESSAGE_NOT_FOUND: 'Não encontrado, tente novamente.',
MESSAGE_TOO_LARGE: 'Essa música é muito grande!',
MESSAGE_WAIT_QUEUE:
'Alguma música está sendo baixada no momento. Aguarde alguns instantes e envie o comando novamente',
MESSAGE_DOWNLOAD_STARTED:
'*INICIANDO DOWNLOAD:* _Esta ação pode demorar um pouco!_',
MESSAGE_DOWNLOAD_ERROR: 'Erro, tente novamente.',
MESSAGE_NO_DOWNLOADED_MUSICS: 'Não temos músicas salvas',
MESSAGE_ERROR: 'Erro ao concluir a operação.',
COMMAND_PLAY: '!play',
COMMAND_INFO: '!info',
COMMAND_MUSICS: '!musics',
MESSAGE_SEARCHING_FOR: '*Procurando por*',
MESSAGE_FOUNDED: '*Encontrado*',
CONNECTED: 'Conectado com sucesso!',
DOWNLOAD_STARTED: '*INICIANDO DOWNLOAD:* _Esta ação pode demorar um pouco!_',
ERROR: 'Erro ao concluir a operação.',
SEARCHING_FOR: '*Procurando por*',
FOUNDED: '*Encontrado*',
AVAILABLE_COMMANDS: '*Comandos disponíveis:*',
HELP_PLAY:
'*Comando:* play\n*Descrição:* Reproduz o áudio de algum vídeo do youtube',
HELP_HELP: '*Comando:* help\n*Descrição:* Exibe esta mensagem',
};

const en = {
MESSAGE_CONNECTED: 'Successfully connected!',
MESSAGE_NOT_FOUND: 'Not found. Try again',
MESSAGE_TOO_LARGE: 'Music duration is too large!',
MESSAGE_WAIT_QUEUE:
'Download proccess got taken by another user. Await then finish to continue',
MESSAGE_DOWNLOAD_STARTED:
'*DOWNLOAD STARTED:* _This action can take some minutes_',
MESSAGE_DOWNLOAD_ERROR: 'Error! Try again',
MESSAGE_NO_DOWNLOADED_MUSICS: 'No stored musics',
MESSAGE_ERROR: 'Error to complete operation.',
COMMAND_PLAY: '!play',
COMMAND_INFO: '!info',
COMMAND_MUSICS: '!musics',
MESSAGE_SEARCHING_FOR: '*Searching for*',
MESSAGE_FOUNDED: '*Founded*',
CONNECTED: 'Successfully connected!',
DOWNLOAD_STARTED: '*DOWNLOAD STARTED:* _This action can take some minutes_',
ERROR: 'Error to complete operation.',
SEARCHING_FOR: '*Searching for*',
FOUNDED: '*Founded*',
AVAILABLE_COMMANDS: '*Available commands:*',
HELP_PLAY:
'*Command:* play\n*Description:* Play the audio of a youtube video',
HELP_HELP: '*Command:* help\n*Description:* Show this message',
};

export default { br, en };
const tr = {
CONNECTED: 'Başarıyla bağlandı!',
DOWNLOAD_STARTED: '*İNDİRME BAŞLADI:* _Bu işlem birkaç dakika sürebilir_',
ERROR: 'Error to complete operation.',
SEARCHING_FOR: '*Aranıyor*',
FOUNDED: '*Bulundu*',
AVAILABLE_COMMANDS: '*Kullanılabilir komutlar:*',
HELP_PLAY:
'*Komut:* play\n*Açıklama:* Bir youtube videosunun sesini oynatın',
HELP_HELP: '*Komut:* help\n*Açıklama:* Bu mesajı göster',
};

export default { br, en, tr };
51 changes: 11 additions & 40 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,18 @@
import qrcode from 'qrcode-terminal';
import { Client, MessageMedia } from 'whatsapp-web.js';
import langs from './language';
import Downloader from './services/download';
import Searcher from './services/search';
import client from './client';
import commands from './commands';
import { PREFIX } from './config';

const text = langs.en;
const allCommands = ["play", "help"];

const client = new Client({
puppeteer: { headless: true, args: ['--no-sandbox'] },
clientId: 'example',
});

const downloader = new Downloader();
const searcher = new Searcher();

client.on('qr', qr => {
qrcode.generate(qr, { small: true });
});

client.on('ready', async () => {
console.log(text.MESSAGE_CONNECTED);
});
client.initialize();

client.on('message_create', async message => {
console.log(message.body);

if (message.body.startsWith(text.COMMAND_PLAY)) {
try {
const keyword = message.body.split('!play ')[1];
const { title, videoId } = await searcher.handle(keyword);
message.reply(`${text.MESSAGE_FOUNDED} "${title}"`);

message.reply(text.MESSAGE_DOWNLOAD_STARTED);
if (!message.body.startsWith(PREFIX)) return;

const music = await downloader.handle(videoId);
const [command, ...rest] = message.body.split(' ');
const content = rest.join(' ');

if (!allCommands.includes(command.substring(1))) return;

const media = MessageMedia.fromFilePath(music);
return message.reply(media);
} catch (error) {
console.log(error);
return message.reply(text.MESSAGE_ERROR);
}
}
commands[command].run(message, content);
});

client.initialize();
2 changes: 1 addition & 1 deletion src/services/search/base.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default abstract class BaseSearch {
abstract handle(videoUrl: string): Promise<SearchResponse>;
abstract handle(keyword: string): Promise<SearchResponse>;
}
Loading