mirror of
https://github.com/tribufu/tribufu-js
synced 2026-06-01 09:42:36 +00:00
Parse bot token to get id and add examples
This commit is contained in:
parent
5047dada7c
commit
de1f90c622
14 changed files with 298 additions and 146 deletions
26
src/bot.ts
26
src/bot.ts
|
|
@ -11,8 +11,26 @@ import { TribufuApi } from "./api";
|
|||
* - A bot give you read and write access to the Tribufu API.
|
||||
*/
|
||||
export class TribufuBot extends TribufuApi {
|
||||
private readonly botId: string;
|
||||
|
||||
constructor(token: string) {
|
||||
const payload = TribufuApi.parseToken(token);
|
||||
|
||||
if (!payload) {
|
||||
throw new Error("Invalid token");
|
||||
}
|
||||
|
||||
if (payload.type !== "bot") {
|
||||
throw new Error("Invalid token type");
|
||||
}
|
||||
|
||||
if (!payload.bot_id) {
|
||||
throw new Error("Invalid token payload");
|
||||
}
|
||||
|
||||
super({ accessToken: token });
|
||||
|
||||
this.botId = payload.bot_id;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -37,4 +55,12 @@ export class TribufuBot extends TribufuApi {
|
|||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the bot id.
|
||||
* @returns string
|
||||
*/
|
||||
public getBotId(): string {
|
||||
return this.botId;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue