Skip to content

Commit

Permalink
Merge pull request #28 from tutinoko2048/dev
Browse files Browse the repository at this point in the history
v3.0.2
  • Loading branch information
tutinoko2048 authored May 22, 2023
2 parents 1824171 + 85c864f commit 0ad68f5
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 32 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"arrow-spacing": "warn",
"space-before-blocks": "warn",
"keyword-spacing": "warn",
"no-irregular-whitespace": "off"
"no-irregular-whitespace": "off",
"no-useless-escape": "off"
},
"parserOptions": {
"ecmaVersion": 13
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ node_modules/
package-lock.json
yarn.lock
yarn-error.log
data/
config.jsonc
data/
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ https://youtu.be/BEv4oozeQKU

## 使い方
### Nodejsをインストール
Nodejs v16.6以上をインストールしてください
Nodejs v18以上をインストールしてください
https://nodejs.org/ja/

### botを作る
Expand Down Expand Up @@ -90,6 +90,7 @@ tellでメッセージをプレイヤーに送信します。周りからは見
文字の配列で指定します (EX: `[ "ロールID1", "ロールID2",... ]`)
- `ready_message`: サーバー起動時に通知メッセージを送信
- `delete_color_prefix`: discord送信時に§とその後の文字を削除するか
- `panel_update_interval`: ステータスパネルの更新間隔(ミリ秒単位)
- `scripts_entry`: 実行するスクリプトのエントリポイント
- `command_version`: マイクラに送るコマンドのバージョン
- `debug`: デバッグログを有効化
Expand Down
3 changes: 2 additions & 1 deletion README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ https://youtu.be/BEv4oozeQKU

## Installation
### Install Nodejs
Requires version more than 16.6
Requires v18 or more
https://nodejs.org/en/

### Make a bot account
Expand Down Expand Up @@ -88,6 +88,7 @@ or you can run `loopback.cmd` (same commands here)
Array of string. (EX: `[ "RoleID1", "RoleID2",... ]`)
- `ready_message`: Sends a message when the server(bot) starts
- `delete_color_prefix`: Remove § and the following character
- `panel_update_interval`: Update interval of status panel(ms)
- `scripts_entry`: The entrypoint of scripts
- `command_version`: The version of command to send
- `debug`: Enables debug log
Expand Down
3 changes: 2 additions & 1 deletion config.jsonc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// discord-mcbe config 各項目の説明ははREADMEにも載せています
// version: 1
// version: 2

{
// required
Expand All @@ -14,6 +14,7 @@
"command_role_id": [ "" ], // マイクラへのコマンドの送信を許可するロールのID
"ready_message": true, // サーバー起動時に通知メッセージを送信
"delete_color_prefix": true, // discord送信時に§とその後の文字を削除するか
"panel_update_interval": "10000", // パネルの更新間隔(ミリ秒単位)
"scripts_entry": "scripts/index.js", // 実行するカスタムスクリプトのエントリポイント
"command_version": "1.19.80", // マイクラに送るコマンドのバージョン
"debug": false,
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "discord-mcbe",
"version": "3.0.0",
"version": "3.0.2",
"engines": {
"node": ">=16.6.0"
"node": ">=18.0.0"
},
"main": "src/index.js",
"typings": "src/types/index.d.ts",
Expand All @@ -17,11 +17,11 @@
"dotlang": "^1.1.1",
"jsonc": "^2.0.0",
"moment-timezone": "^0.5.43",
"socket-be": "^1.1.1"
"socket-be": "^1.2.0"
},
"devDependencies": {
"eslint": "^8.37.0",
"typescript": "^5.0.3"
"eslint": "^8.41.0",
"typescript": "^5.0.4"
},
"overrides": {
"discord-api-types": "0.37.35"
Expand Down
5 changes: 2 additions & 3 deletions scripts/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { server, client } = require('discord-mcbe');
const { version: VERSION } = require('../package.json');
const { server, client, version } = require('discord-mcbe');

console.log('[Script] loaded!');

Expand All @@ -13,7 +12,7 @@ server.events.on('playerChat', async ev => {
if (command === 'help') {
await world.sendMessage([
`§b[discord-mcbe]§r`,
`§7-§f version: §e${VERSION}§r`,
`§7-§f version: §e${version}§r`,
`§7-§f client: §6${world.localPlayer}§r`,
'§7Made by RetoRuto9900K / tutinoko2048§r'
].join('\n'));
Expand Down
15 changes: 13 additions & 2 deletions src/handlers/PanelHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const moment = require('moment-timezone');
const panelEmbed = new EmbedBuilder()
.setAuthor({ name: 'Status Panel' })
.setColor(colors.leave)
.setDescription('Awaiting update...');
.setDescription('Awaiting update...')
.setFooter({ text: 'discord-mcbe' });

class PanelHandler {
/** @param {import('../index')} main */
Expand All @@ -24,6 +25,16 @@ class PanelHandler {
this.message;
}

async startInterval() {
const panel = await this.fetch()
.catch((e) => this.main.logger.error(`[PanelHandler] failed to fetch panel | code: ${e.code}`));
if (panel) {
this.main.logger.info('[PanelHandler] successfully fetched the panel');
this.update();
}
setInterval(() => this.message && this.update(), this.main.config.panel_update_interval);
}

/**
* @returns {Promise<import('discord.js').Message|void>}
*/
Expand Down Expand Up @@ -88,7 +99,7 @@ class PanelHandler {
`** | **Ping: ${w.ping} ms`,
`** | **Connected: ${connectAt}`,
'** | **Players:',
`** | **${list.players.join(', ')}`
`** | **${list.players.sort().join(', ')}`
].join('\n');
}));
const messages = [
Expand Down
11 changes: 4 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Main {
console.log(logo);
console.log(`discord-mcbe v${VERSION}`);

this.version = VERSION;

this.config = getConfig();

this.logger = new Logger('Discord', {
Expand All @@ -40,6 +42,7 @@ class Main {
],
allowedMentions: { repliedUser: false }
});
if ('DISCORD_TOKEN' in process.env) this.config.discord_token ||= process.env.DISCORD_TOKEN;
validateConfig(this.config, this.client);

this.interactions = new DiscordInteractions(this.client);
Expand All @@ -66,13 +69,7 @@ class Main {
this.updateActivity();
setInterval(() => this.updateActivity(), 20*1000);

const panel = await this.panels.fetch()
.catch((e) => this.logger.error(`[PanelHandler] failed to fetch panel | code: ${e.code}`));
if (panel) {
this.logger.info('[PanelHandler] successfully fetched the panel');
this.panels.update();
}
setInterval(() => this.panels.message && this.panels.update(), 10*1000);
this.panels.startInterval();
});

this.client.on('messageCreate', async message => {
Expand Down
5 changes: 2 additions & 3 deletions src/interactions/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ const { ChatInput } = require('@akki256/discord-interaction');
const { EmbedBuilder } = require('discord.js');
const localization = require('./_localizations.json');
const { TN_ICON_URL, GITHUB_URL } = require('../util/constants');
const { lang, interactions } = require('../index.js');
const { version: VERSION } = require('../../package.json');
const { lang, interactions, version } = require('../index.js');

const helpCommand = new ChatInput({
name: 'help',
Expand All @@ -14,7 +13,7 @@ const helpCommand = new ChatInput({
const commands = [...interactions.chatInputs.values()];

const embed = new EmbedBuilder();
embed.setAuthor({ name: `discord-mcbe v${VERSION} Help` });
embed.setAuthor({ name: `discord-mcbe v${version} Help` });
embed.setDescription([
lang.run('command.help.commands'),
...commands.map(c => `</${c.data.name}:${c.id}> - ${getDescription(c, interaction.locale)}`),
Expand Down
2 changes: 1 addition & 1 deletion src/interactions/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const listCommand = new ChatInput({
return [
`**${w.name} - ${list.current}/${list.max}**`,
'** | **Players:',
`** | **${list.players.join(', ')}`
`** | **${list.players.sort().join(', ')}`
].join('\n');
}));

Expand Down
8 changes: 4 additions & 4 deletions src/interactions/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ const pingCommand = new ChatInput({
await interaction.deferReply();
const deltaTime = Date.now() - sentTime;

const startAt = moment(server.startTime).tz(config.timezone);
const startAt = moment(server.startTime).tz(config.timezone).format('MM/DD HH:mm:ss');

const embed = new EmbedBuilder();
embed.setDescription([
'Pong!',
`- **Time**: ${deltaTime}ms`,
`- **Discord**: ${interaction.client.ws.ping}ms`,
...worlds.map(w => `- **${w.name}**: ${w.ping}ms`)
`\- **Time**: ${deltaTime}ms`,
`\- **Discord**: ${interaction.client.ws.ping}ms`,
...worlds.map(w => `\- **${w.name}**: ${w.ping}ms`)
].join('\n'));
embed.setColor('Random');
embed.setFooter({ text: lang.run('command.ping.startAt', [ startAt ]) });
Expand Down
1 change: 1 addition & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface IConfig {
command_role_id: string[];
ready_message: boolean;
delete_color_prefix: boolean;
panel_update_interval: number;
styles_tnac: boolean;
scripts_entry: string;
command_version: import('socket-be').VersionResolvable;
Expand Down
2 changes: 1 addition & 1 deletion src/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ function getConfig() {
* @param {import('discord.js').Client} client
*/
function validateConfig(config, client) {
if ('DISCORD_TOKEN' in process.env) config.discord_token ||= process.env.DISCORD_TOKEN;
if (!config.discord_token) throw Error('Set discord_token in config');
if (!config.guild_id) throw Error('Set guild_id in config');
if (!config.channel_id) throw Error('Set channel_id in config');
if (!config.language) throw Error('Set valid language name in config');
if (config.panel_update_interval === undefined) throw Error('Set panel_update_interval in config');

if (typeof config.guild_id !== 'string') throw TypeError('The type of config.guild_id is not a string');
if (typeof config.channel_id !== 'string') throw TypeError('The type of config.channel_id is not a string');
Expand Down

0 comments on commit 0ad68f5

Please sign in to comment.