Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Feature/54 command cal #79

Merged
merged 17 commits into from
Mar 28, 2024
Merged
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,6 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# Font file
*.ttf
4 changes: 3 additions & 1 deletion config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@
"rconpass1": "yourpassword1234",
"syslogChannel": "1151139585791901746",
"notificationChannel": "1151139585791901746",
"language": "default"
"language": "default",
"fontFile": "font.ttf",
"fontFamily": "sans-serif"
}
43 changes: 43 additions & 0 deletions language/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,49 @@
}
},
"commands": {
"cal": {
"name": "cal",
"description": "カレンダーを表示します",
"options": {
"month": {
"name": "month",
"description": "月"
},
"year": {
"name": "year",
"description": "年"
},
"weekStart": {
"name": "week_start",
"description": "週の始まりの曜日"
}
},
"dayNames": [
"日曜日",
"月曜日",
"火曜日",
"水曜日",
"木曜日",
"金曜日",
"土曜日"
],
"dayLabels": ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"],
"monthNames": [
"1月",
"2月",
"3月",
"4月",
"5月",
"6月",
"7月",
"8月",
"9月",
"10月",
"11月",
"12月"
],
"monthYear": "${year}年${month}"
},
"check": {
"name": "check",
"description": "Ping Checker",
Expand Down
43 changes: 43 additions & 0 deletions language/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,49 @@
}
},
"commands": {
"cal": {
"name": "cal",
"description": "Shows the calendar",
"options": {
"month": {
"name": "month",
"description": "Month"
},
"year": {
"name": "year",
"description": "Year"
},
"weekStart": {
"name": "week_start",
"description": "The days to start weeks"
}
},
"dayNames": [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
],
"dayLabels": ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"],
"monthNames": [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
],
"monthYear": "${month} ${year}"
},
"check": {
"name": "check",
"description": "Ping Checker",
Expand Down
43 changes: 43 additions & 0 deletions language/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,49 @@
}
},
"commands": {
"cal": {
"name": "cal",
"description": "カレンダーを表示します",
"options": {
"month": {
"name": "month",
"description": "月"
},
"year": {
"name": "year",
"description": "年"
},
"weekStart": {
"name": "week_start",
"description": "週の始まりの曜日"
}
},
"dayNames": [
"日曜日",
"月曜日",
"火曜日",
"水曜日",
"木曜日",
"金曜日",
"土曜日"
],
"dayLabels": ["日", "月", "火", "水", "木", "金", "土"],
"monthNames": [
"1月",
"2月",
"3月",
"4月",
"5月",
"6月",
"7月",
"8月",
"9月",
"10月",
"11月",
"12月"
],
"monthYear": "${year}年${month}"
},
"check": {
"name": "check",
"description": "Ping を確認",
Expand Down
120 changes: 120 additions & 0 deletions packages/misc/commands/cal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { createCanvas } from 'canvas';
import { SimpleSlashCommandBuilder } from '../../../common/SimpleCommand';
import { LANG, strFormat } from '../../../util/languages';
import { DayOfWeek, MonthCalendar } from '../util/calendar';
import {
BoundingBox,
CanvasTable,
CanvasTextBox,
FONT_FAMILY,
InlineText,
} from '../util/canvasUtils';

const WORKDAY_COLOR = 'black';
const HOLIDAY_COLOR = 'red';
const SUNDAY_COLOR = 'red';
const SATURDAY_COLOR = 'blue';
const EXCLUDED_COLOR = 'gray';

function dayColor(day: DayOfWeek) {
switch (day) {
case DayOfWeek.Sunday:
return SUNDAY_COLOR;
case DayOfWeek.Saturday:
return SATURDAY_COLOR;
default:
return WORKDAY_COLOR;
}
}

export default SimpleSlashCommandBuilder.create(
LANG.commands.cal.name,
LANG.commands.cal.description,
)
.addIntegerOption({
name: LANG.commands.cal.options.month.name,
description: LANG.commands.cal.options.month.description,
choices: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map((value) => ({
name: LANG.commands.cal.monthNames[value],
value,
})),
required: false,
})
.addIntegerOption({
name: LANG.commands.cal.options.year.name,
description: LANG.commands.cal.options.year.description,
required: false,
})
.addIntegerOption({
name: LANG.commands.cal.options.weekStart.name,
description: LANG.commands.cal.options.weekStart.description,
choices: Object.values(DayOfWeek).map((value) => ({
name: LANG.commands.cal.dayNames[value],
value,
})),
required: false,
})
.build(async (interaction, month, year, weekStart = DayOfWeek.Sunday) => {
const today = new Date();
const calendar = new MonthCalendar(
month ?? today.getMonth(),
year ?? today.getFullYear(),
);
const days = [];
for (let i = 0; i < 7; i++) {
days.push((weekStart + i) % 7);
}
const table = [
days.map((i) => {
const text = new InlineText(LANG.commands.cal.dayLabels[i]);
text.color = dayColor(i as DayOfWeek);
return text;
}),
...Array.from(calendar.weeks(weekStart)).map((week) =>
week.map((day) => {
const text = new InlineText(day.date.toString());
text.color = dayColor(day.day);
if (day.isHoliday()) {
text.color = HOLIDAY_COLOR;
}
if (!calendar.includes(day)) {
text.color = EXCLUDED_COLOR;
}
if (day.is(today)) {
text.font = `bold 24px ${FONT_FAMILY}`;
}
return text;
}),
),
];
const canvas = createCanvas(800, 400);
const ctx = canvas.getContext('2d');
new BoundingBox(0, 0, 800, 400).fill(ctx, 'white');
const title = strFormat(LANG.commands.cal.monthYear, {
month: LANG.commands.cal.monthNames[calendar.month],
year: calendar.year,
});
const titleStyle = new InlineText(title);
titleStyle.color = 'black';
titleStyle.font = `48px ${FONT_FAMILY}`;
new CanvasTextBox(titleStyle, new BoundingBox(50, 0, 700, 100)).renderTo(
ctx,
);
new CanvasTable(table, new BoundingBox(50, 100, 700, 300)).renderTo(ctx);
await interaction.reply({
files: [
{
attachment: canvas.toBuffer(),
name: 'calendar.png',
},
],
embeds: [
{
title: strFormat(title),
image: {
url: 'attachment://calendar.png',
},
},
],
});
});
7 changes: 6 additions & 1 deletion packages/misc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ const fs = require('fs');
const path = require('path');

const { CommandManager } = require('../../internal/commands');
const { registerConfiguredFont } = require('./util/canvasUtils');

class MiscFeature {
onLoad() {
registerConfiguredFont();
fs.readdirSync(path.join(__dirname, 'commands'), {
withFileTypes: true,
}).forEach((file) => {
const ext = path.extname(file.name);
if (!file.isFile() || (ext != '.js' && ext != '.ts')) return;
const cmds = require(path.join(__dirname, 'commands', file.name));
let cmds = require(path.join(__dirname, 'commands', file.name));
if ('default' in cmds) {
cmds = cmds.default;
}
CommandManager.default.addCommands(cmds);
});
}
Expand Down
Loading