Skip to content

Commit

Permalink
gaming more efficient routing, fixed the issue with non / commands pa…
Browse files Browse the repository at this point in the history
…rsed
  • Loading branch information
Nickolay Kiselyov committed Nov 12, 2023
1 parent b5ca311 commit f0efcde
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion bot/core/HackerEmbassyBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ ${chunks[index]}
if (Math.abs(Date.now() / 1000 - message.date) > IGNORE_UPDATE_TIMEOUT) return;

const text = message.text;
if (!text) return;
if (!text || text[0] !== "/") return;

const fullCommand = text.split(" ")[0];
const command = fullCommand.split("@")[0].slice(1);
Expand Down
25 changes: 24 additions & 1 deletion bot/handlers/embassy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ const embassyApiConfig = config.get<EmbassyApiConfig>("embassy-api");
const botConfig = config.get<BotConfig>("bot");
export const embassyBase = `${embassyApiConfig.host}:${embassyApiConfig.port}`;

enum DeviceOperation {
Help = "help",
Status = "status",
Up = "up",
Down = "down",
}

export default class EmbassyHandlers implements BotHandlers {
static async unlockHandler(bot: HackerEmbassyBot, msg: Message) {
if (!(await hasDeviceInside(msg.from?.username))) {
Expand Down Expand Up @@ -315,12 +322,28 @@ export default class EmbassyHandlers implements BotHandlers {
}
}

static async deviceHelpHandler(bot: HackerEmbassyBot, msg: Message, deviceName: string) {
static async deviceHandler(
bot: HackerEmbassyBot,
msg: Message,
deviceName: string,
operation: DeviceOperation = DeviceOperation.Help
) {
try {
const device = embassyApiConfig.devices[deviceName];

if (!device) throw Error();

switch (operation) {
case "up":
return EmbassyHandlers.wakeHandler(bot, msg, deviceName);
case "down":
return EmbassyHandlers.shutdownHandler(bot, msg, deviceName);
case "status":
return EmbassyHandlers.pingHandler(bot, msg, deviceName, false);
default:
break;
}

await bot.sendMessageExt(msg.chat.id, t("embassy.device.help", { deviceName }), msg);
} catch (error) {
logger.error(error);
Expand Down
29 changes: 4 additions & 25 deletions bot/init/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,31 +88,10 @@ export function addRoutes(bot: HackerEmbassyBot): void {

// Devices
bot.addRoute(
["gayming", "gaming", "gayminghelp", "gaminghelp", "gaming help", "gayming help"],
EmbassyHandlers.deviceHelpHandler,
null,
() => ["gaming"],
["member"]
);
bot.addRoute(
["gaymingup", "gamingup", "wolgaming", "gaming up", "gayming up"],
EmbassyHandlers.wakeHandler,
null,
() => ["gaming"],
["member"]
);
bot.addRoute(
["gaymingdown", "gamingdown", "gaming shutdown", "gayming shutdown", "gaming down", "gayming down"],
EmbassyHandlers.shutdownHandler,
null,
() => ["gaming"],
["member"]
);
bot.addRoute(
["gaymingstatus", "gamingstatus", "gaming status", "gayming status"],
EmbassyHandlers.pingHandler,
null,
() => ["gaming"],
["gayming", "gaming"],
EmbassyHandlers.deviceHandler,
OptionalParam(/(status|help|up|down)/),
match => ["gaming", match[1]],
["member"]
);

Expand Down
2 changes: 1 addition & 1 deletion resources/locales/ru/translation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ embassy:
#`/{{deviceName}} down#` - Выключить
#`/{{deviceName}} status#` - Проверить доступность
#`/{{deviceName}} help#` - Вывести это сообщение
notfound: ⚠️ Девайс не известен системе
notfound: ⚠️ Девайс не известен системе или что-то пошло очень не так
wake:
success: ⏰ Попытались разбудить девайс, следи за его статусом
fail: ⚠️ Не получилось разбудить девайс
Expand Down

0 comments on commit f0efcde

Please sign in to comment.