mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-24 22:46:41 +07:00
made safe getUserById for handlers
This commit is contained in:
parent
b28c34878b
commit
740cf4f5e7
3 changed files with 18 additions and 12 deletions
|
@ -9,7 +9,7 @@ import { ExtraReplyMessage } from "telegraf/typings/telegram-types";
|
||||||
interface Fields {}
|
interface Fields {}
|
||||||
|
|
||||||
interface Values {
|
interface Values {
|
||||||
user: UsersUserFull;
|
user?: UsersUserFull;
|
||||||
group: ConfigGroup;
|
group: ConfigGroup;
|
||||||
isJoined: boolean;
|
isJoined: boolean;
|
||||||
isLeave: boolean;
|
isLeave: boolean;
|
||||||
|
@ -24,9 +24,10 @@ export class JoinLeaveHandler extends VkEventHandler<Fields, Values> {
|
||||||
const user = await this.getUserByID(String(context.userId));
|
const user = await this.getUserByID(String(context.userId));
|
||||||
const dir = context.isJoin ? "joined" : "left";
|
const dir = context.isJoin ? "joined" : "left";
|
||||||
const count = await this.getMembersCount();
|
const count = await this.getMembersCount();
|
||||||
|
const { first_name = "[unknown]", last_name = "[unknown]" } = user || {};
|
||||||
|
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`vk, group ${this.group.name}: ${user.first_name} ${user.last_name} ${dir} the group`
|
`vk, group ${this.group.name}: ${first_name} ${last_name} ${dir} the group`
|
||||||
);
|
);
|
||||||
|
|
||||||
const parsed = this.template.theme(
|
const parsed = this.template.theme(
|
||||||
|
|
|
@ -13,7 +13,7 @@ interface Fields {
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Values {
|
interface Values {
|
||||||
user: UsersUserFull;
|
user?: UsersUserFull;
|
||||||
group: ConfigGroup;
|
group: ConfigGroup;
|
||||||
text: string;
|
text: string;
|
||||||
}
|
}
|
||||||
|
@ -29,9 +29,10 @@ export class MessageNewHandler extends VkEventHandler<Fields, Values> {
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = await this.getUserByID(String(context.senderId));
|
const user = await this.getUserByID(String(context.senderId));
|
||||||
|
const { first_name = "[unknown]", last_name = "[unknown]" } = user || {};
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
`vk, group ${this.group.name} received message from ${user.first_name} ${user.last_name}: "${context.text}"`
|
`vk, group ${this.group.name} received message from ${first_name} ${last_name}: "${context.text}"`
|
||||||
);
|
);
|
||||||
|
|
||||||
const parsed = this.template.theme(
|
const parsed = this.template.theme(
|
||||||
|
@ -47,7 +48,7 @@ export class MessageNewHandler extends VkEventHandler<Fields, Values> {
|
||||||
disable_web_page_preview: true,
|
disable_web_page_preview: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
this.appendButtons(extras, user.id);
|
this.appendButtons(extras, user?.id);
|
||||||
|
|
||||||
await this.telegram.sendMessageToChan(
|
await this.telegram.sendMessageToChan(
|
||||||
this.channel.id,
|
this.channel.id,
|
||||||
|
@ -62,8 +63,8 @@ export class MessageNewHandler extends VkEventHandler<Fields, Values> {
|
||||||
/**
|
/**
|
||||||
* Appending buttons (if needed) by mutating original extras
|
* Appending buttons (if needed) by mutating original extras
|
||||||
*/
|
*/
|
||||||
private appendButtons = (extras: ExtraReplyMessage, userId: number) => {
|
private appendButtons = (extras: ExtraReplyMessage, userId?: number) => {
|
||||||
if (!this.template?.fields?.buttons?.includes("link")) {
|
if (!userId || !this.template?.fields?.buttons?.includes("link")) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -36,12 +36,16 @@ export class VkEventHandler<
|
||||||
* @param id
|
* @param id
|
||||||
*/
|
*/
|
||||||
protected getUserByID = async (id: string) => {
|
protected getUserByID = async (id: string) => {
|
||||||
const users = await this.instance.api.users.get({
|
try {
|
||||||
user_ids: [id],
|
const users = await this.instance.api.users.get({
|
||||||
fields: ["sex"],
|
user_ids: [id],
|
||||||
});
|
fields: ["sex"],
|
||||||
|
});
|
||||||
|
|
||||||
return users[0];
|
return users[0];
|
||||||
|
} catch (e) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue