Skip to content

Commit

Permalink
Merge pull request #6 from JuanM4T/main
Browse files Browse the repository at this point in the history
- Change the way to config (new is config/channel.json)
- Add output customization  (config/language.json)
- Improve code quality (now I can read it easily :v )
- Change docs language to English
  • Loading branch information
thnhmai06 authored Feb 23, 2024
2 parents aba7cbe + 7812744 commit 86ee5b8
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 131 deletions.
63 changes: 27 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,59 +1,50 @@
<h1 align="center">
osu-chat-discord
</h1>
</h1><p align="center">Fetch messages from osu!Bancho Chat and send it to Discord channel</p>

<p align="center">
<a href="https://lgtm.com/projects/g/thanhgaming5550/osu-chat-discord/alerts/"><img alt="Total alerts" src="https://img.shields.io/lgtm/alerts/g/thanhgaming5550/osu-chat-discord.svg?logo=lgtm&logoWidth=18"/></a>
<a href="https://lgtm.com/projects/g/thanhgaming5550/osu-chat-discord/context:javascript"><img alt="Language grade: JavaScript" src="https://img.shields.io/lgtm/grade/javascript/g/thanhgaming5550/osu-chat-discord.svg?logo=lgtm&logoWidth=18"/></a>
<img alt="GitHub repo size" src="https://img.shields.io/github/repo-size/thanhgaming5550/osu-chat-discord">
<img alt="GitHub" src="https://img.shields.io/github/license/thanhgaming5550/osu-chat-discord">
<img alt="GitHub release (latest by date)" src="https://img.shields.io/github/v/release/thanhgaming5550/osu-chat-discord">
</p>

## Yêu cầu cài đặt:
* [Node.js](https://nodejs.org/) >=16.6.0
* npm (ngầm định trong Node.js)
## Requirements:
* [Node.js](https://nodejs.org/) >=16.6.0 (*with npm*)

## Usage:

## Cách dùng:
1. Run `git clone https://github.com/thanhgaming5550/osu-chat-discord.git` or manually download and extract the repository.

1. Chạy `git clone https://github.com/thanhgaming5550/osu-chat-discord.git` hoặc tải thủ công rồi giải nén. Vào thư mục đó.
2. Run `npm install`

2. Chạy `npm install`

3. Tạo 1 file `.env` và ghi các nội dung sau:
3. Create a `.env` file and write the following contents:
```env
TOKEN= #Token Discord Bot của bạn
NAME= #Name osu username (IRC)
TOKEN= #Your Discord Bot Token
NAME= #osu username (IRC)
IRC= #IRC Password
```
*Để lấy IRC, truy cập [trang web này](https://osu.ppy.sh/p/irc)*

*In order to obtain IRC keys, please visit [this website](https://osu.ppy.sh/p/irc)*

4. Trong `config.json`, config theo mẫu:
4. In `config/channel.json`, configure as follows:
```cfg
{
"channel" : [ #Theo dõi kênh osu chat
{ #Xóa phần ngoặc nhọn {} nếu không dùng
"channel":"ID_Kênh_Muốn_Gửi",
"osucnn":"Name_của_osu_channel (có chứa #)"
}#,....
],
"pm": [ #Theo dõi PM chat (Chat PM của người cho IRC)
{ #Xóa phần ngoặc nhọn {} nếu không dùng
"channel": "ID_Kênh_Muốn_Gửi"
}#,....
]
"channel": {
"enabled": true, #Look for messages on channels
"channels": { #Send to what Discord Channel
"#name_of_Osu_channel": ["DiscordChannelID1", "DiscordChannelID2"],
...
}
},
"pm": {
"enabled": true, #Look for PM (of those who connect via IRC)
"channels": ["DiscordChannelID3", "DiscordChannelID4",...], #Send to what Discord Channel
...
}
}
```
5. Chạy `node index.js`

## Mở rộng:
- Bạn có thể thay đổi cách hiển thị đầu ra tin nhắn trên Discord theo ý bạn bằng cách tùy chỉnh nó (đối với channel chat) ở dòng 60 (cho tin nhắn dạng /np, /me,...) https://github.com/thanhgaming5550/osu-chat-discord/blob/5de753be2677b09b8b21af2f220b8b7ceb55ef8a/index.js#L60 và dòng 61 (dạng tin nhắn thường) https://github.com/thanhgaming5550/osu-chat-discord/blob/5de753be2677b09b8b21af2f220b8b7ceb55ef8a/index.js#L61
```

5. Run `node index.js`

**Trong đó các biến:** ```message.user.ircUsername``` là tên người gửi, ```message.message.replace(`�ACTION`, ``).slice(0, -1)``` là nội dung tin nhắn sau khi đã được xử lý **ACTION** (chỉ cho tin nhắn dạng /np, /me,...), ```message.message``` là nội dung tin nhắn nguyên gốc, ```channel``` là tên kênh mà tin nhắn đó được gửi trên osu!Bancho


*Tương tự với PM Chat, dòng 31 (cho tin nhắn dạng /np, /me,...) https://github.com/thanhgaming5550/osu-chat-discord/blob/5de753be2677b09b8b21af2f220b8b7ceb55ef8a/index.js#L31 và dòng 32 (dạng tin nhắn thường) https://github.com/thanhgaming5550/osu-chat-discord/blob/5de753be2677b09b8b21af2f220b8b7ceb55ef8a/index.js#L32
## Customization:
You can customizate the output in `config/language.json`
20 changes: 0 additions & 20 deletions config.json

This file was deleted.

13 changes: 13 additions & 0 deletions config/channel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"channel": {
"enabled": true,
"channels": {
"#vietnamese": ["1212345678912345678", "1212345678912345699"],
"#osu": ["1212345678912345600"]
}
},
"pm": {
"enabled": true,
"channels": ["1212345678912345611"]
}
}
36 changes: 36 additions & 0 deletions config/language.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"onReady" : "{{name}} is ready!",
"onLogged": "Logged as {{name}}",
"line":"=========================================================================================================",

"onConnected": "Connected to osu!Bancho (As {{name}})",
"unableConnect": "Can't connect to osu!Bancho ({{err}})",
"pmDisabled": "PM Disabled!",
"channelDisabled": "Channel Disabled!",

"pmEnabled": "Connected to PM",
"osuChannelConnected": "Connected to {{channel}}",
"osuChannelUnable":"Can't connect to osu channel: {{channel}}",
"discordUnableMessage": "Can't send message to discord channel: {{channel}} ({{err}})",

"log":{
"pm": {
"action":"[PM] *{{user}}: {{content}}",
"normal":"[PM] {{user}}: {{content}}"
},
"channel": {
"action":"[{{channel}}] *{{user}}: {{content}}",
"normal":"[{{channel}}] {{user}}: {{content}}"
}
},
"message":{
"pm": {
"action":"***{{user}}**: {{content}}",
"normal":"**{{user}}**: {{content}}"
},
"channel": {
"action":"***{{user}}**: {{content}}",
"normal":"**{{user}}**: {{content}}"
}
}
}
153 changes: 84 additions & 69 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,87 +1,102 @@
const Discord = require("discord.js");
const client = new Discord.Client({
intents: ["GUILDS", "GUILD_MESSAGES"]
});
//Setup Client
require("dotenv").config();
const config = require("./config.json")
const channelsync = config.channel;
const pm = config.pm;
const Discord = require("discord.js");
const Mustache = require('mustache');
const client = new Discord.Client({intents: ["GUILDS", "GUILD_MESSAGES"]});
const Banchojs = require("bancho.js");
const osu = new Banchojs.BanchoClient({
username: process.env.NAME,
password: process.env.IRC,
});
Mustache.escape = (value) => {return value}; //For Escape HTML Char Code

client.on("error", console.error);

client.on('ready', () => {
client.user.setActivity(`osu!`, {
type: "PLAYING"
})
console.log(`${client.user.tag} đã sẵn sàng`);
//Save Configuration
const config = require("./config/channel.json")
const channelConfiguration = config.channel;
const pmConfiguration = config.pm;
const language = require("./config/language.json");

osu.connect().then(() => {
console.log("Đã kết nối tới osu!Bancho (" + process.env.NAME + ")");
console.log('=========================================================================================================');
if (pm.length > 0) {
osu.on("PM", (message) => {
console.log(`[PM] ${message.user.ircUsername}: ${message.message}`);
let msg;
if (message.message.indexOf(`ACTION`) === 0) {
msg = `***${message.user.ircUsername}**` + message.message.replace(`ACTION`, ``).slice(0, -1);
} else msg = `**${message.user.ircUsername}**: ${message.message}`
for (let i = 0; i < pm.length; i++) {
client.channels.cache.get(pm[i].channel).send(msg);
}
})
console.log("Đã kết nối tới PM chat")
} else console.log("No PM chat Sync detected")
//? Sub-Function
function rawProcessing(message, osuChannel) {
if (!osuChannel) osuChannel="PM";
let isAction=false;
if (message.message.indexOf(`ACTION`) === 0) isAction=true;
return {
isAction: isAction,
data: {
user: message.user.ircUsername,
content: (isAction ? message.message.replace(`ACTION`, ``).slice(0, -1) : message.message),
channel: osuChannel
}
};
}

if (channelsync.length > 0) {
let channels = new Array;
//? Main-Function
async function pm() {
osu.on("PM", (message) => {
let msg = rawProcessing(message);
console.log(Mustache.render(msg.isAction ? language.log.pm.action : language.log.pm.normal, msg.data));
for (const discordChannel of pmConfiguration.channels)
client.channels.cache.get(discordChannel).send(
Mustache.render(msg.isAction ? language.message.pm.action : language.message.pm.normal, msg.data)
).catch((err) => {
console.error(Mustache.render(language.discordUnableMessage, {channel: discordChannel, err: err}));
});;
})
console.log(language.pmEnabled)
}
async function channel() {
for (const osuChannel in channelConfiguration.channels) {
let channelInstance = osu.getChannel(osuChannel);
try {
await channelInstance.join().then(() => {
console.log(Mustache.render(language.osuChannelConnected, {channel: osuChannel}))
});
}
catch {
console.log(Mustache.render(language.osuChannelUnable, {channel: osuChannel}));
continue;
}

// Filter out duplicate channel from config file (why tho??)
for (let channel of channelsync) {
if (channels.indexOf(channel.osucnn) === -1) {
channels.push(channel.osucnn)
}
channelInstance.on("message", (message) => {
for (const discordChannel of channelConfiguration.channels[osuChannel])
{
let msg = rawProcessing(message, osuChannel)
console.log(Mustache.render(msg.isAction ? language.log.channel.action : language.log.channel.normal, msg.data));
client.channels.cache.get(discordChannel).send(
Mustache.render(msg.isAction ? language.message.channel.action : language.message.channel.normal, msg.data)
).catch((err) => {
console.error(Mustache.render(language.discordUnableMessage, {channel: discordChannel, err: err}));
});
}
})
}
}

for (let channel of channels) {
let channelInstance = osu.getChannel(channel);

channelInstance.join().then(() => {
channelInstance.on("message", (message) => {
for (let i = 0; i < channelsync.length; i++) {
if (channelsync[i].osucnn === channel) {
console.log(`[${channel}] ${message.user.ircUsername}: ${message.message}`)
let msg;
if (message.message.indexOf(`ACTION`) === 0) {
msg = `***${message.user.ircUsername}**` + message.message.replace(`ACTION`, ``).slice(0, -1);
} else msg = `**${message.user.ircUsername}**: ${message.message}`
client.channels.cache.get(channelsync[i].channel).send(msg);

break;
}
}
})
//Main Process
client.on("error", console.error);
client.on('ready', async () => {
try {
await osu.connect().then(() => {
console.log(Mustache.render(language.onConnected, {name: process.env.NAME}));
})
} catch {
console.error(Mustache.render(language.osuChannelUnable, {err: err}))
}

console.log(language.line);
if (pmConfiguration.enabled) await pm(); else console.log(language.pmDisabled)
if (channelConfiguration.enabled) await channel(); else console.log(language.channelDisabled);
console.log(language.line);

console.log("Đã kết nối tới " + channel)
})
}
} else console.log("No Channel chat Sync detected");
console.log('=========================================================================================================');
})
client.user.setActivity(`osu!`, {type: "PLAYING"})
console.log(Mustache.render(language.onReady, {name: client.user.tag}));
})
console.log('=========================================================================================================');

client.login(process.env.TOKEN).then((token) => {
client.user.setPresence({
status: 'online',
});
console.log(`Đã đăng nhập vào ${client.user.tag}`)
client.login(process.env.TOKEN).then(() => {
client.user.setPresence({status: 'online',});
console.log(Mustache.render(language.onLogged, {name: client.user.tag}));
});

process.on('uncaughtException', function (err) {
console.error(err);
});
process.on('uncaughtException', (err) => console.error(err));
Loading

0 comments on commit 86ee5b8

Please sign in to comment.