mirror of
https://gitlab.com/qwerinope/qweribot.git
synced 2026-02-04 11:06:59 +01:00
add user init validation try/catch, move @ symbol parsing
This commit is contained in:
@@ -2,13 +2,13 @@ import { commandPrefix } from "..";
|
|||||||
|
|
||||||
/** Helper function to extract arguments from commands */
|
/** Helper function to extract arguments from commands */
|
||||||
export default function parseCommandArgs(input: string) {
|
export default function parseCommandArgs(input: string) {
|
||||||
const nice = input.toLowerCase().slice(commandPrefix.length).trim().replace(/[@]/g, '');
|
const nice = input.toLowerCase().slice(commandPrefix.length).trim();
|
||||||
const sliceLength = nice.startsWith('use') ? 2 : 1;
|
const sliceLength = nice.startsWith('use') ? 2 : 1;
|
||||||
return nice.split(' ').slice(sliceLength);
|
return nice.split(' ').slice(sliceLength);
|
||||||
};
|
};
|
||||||
|
|
||||||
export function parseCheerArgs(input: string) {
|
export function parseCheerArgs(input: string) {
|
||||||
const nice = input.toLowerCase().trim().replace(/[@]/g, '');
|
const nice = input.toLowerCase().trim();
|
||||||
|
|
||||||
// This is for the test command. Remove the command prefix, the command, the whitespace after and the amount of fake bits
|
// This is for the test command. Remove the command prefix, the command, the whitespace after and the amount of fake bits
|
||||||
if (nice.startsWith(commandPrefix + 'testcheer')) return nice.slice(commandPrefix.length + 'testcheer'.length + 1).split(' ').slice(1);
|
if (nice.startsWith(commandPrefix + 'testcheer')) return nice.slice(commandPrefix.length + 'testcheer'.length + 1).split(' ').slice(1);
|
||||||
|
|||||||
13
src/user.ts
13
src/user.ts
@@ -1,6 +1,7 @@
|
|||||||
import { redis } from "bun";
|
import { redis } from "bun";
|
||||||
import { chatterApi } from ".";
|
import { chatterApi } from ".";
|
||||||
import { HelixUser } from "@twurple/api"
|
import { HelixUser } from "@twurple/api"
|
||||||
|
import logger from "./lib/logger";
|
||||||
|
|
||||||
const EXPIRETIME = 60 * 60 // 60 minutes
|
const EXPIRETIME = 60 * 60 // 60 minutes
|
||||||
|
|
||||||
@@ -20,8 +21,9 @@ export class User {
|
|||||||
public displayName!: string;
|
public displayName!: string;
|
||||||
|
|
||||||
static async initUsername(username: string): Promise<User | null> {
|
static async initUsername(username: string): Promise<User | null> {
|
||||||
|
try {
|
||||||
const userObj = new User();
|
const userObj = new User();
|
||||||
userObj.username = username;
|
userObj.username = username.replaceAll(/[@]/g, '');
|
||||||
const userid = await redis.get(`userlookup:${username}`);
|
const userid = await redis.get(`userlookup:${username}`);
|
||||||
if (!userid) {
|
if (!userid) {
|
||||||
const userdata = await chatterApi.users.getUserByName(username);
|
const userdata = await chatterApi.users.getUserByName(username);
|
||||||
@@ -36,9 +38,14 @@ export class User {
|
|||||||
userObj.displayName = displayname!;
|
userObj.displayName = displayname!;
|
||||||
};
|
};
|
||||||
return userObj;
|
return userObj;
|
||||||
|
} catch {
|
||||||
|
logger.err(`Failed to initialize user with name: ${username}`);
|
||||||
|
return null;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
static async initUserId(userId: string): Promise<User | null> {
|
static async initUserId(userId: string): Promise<User | null> {
|
||||||
|
try {
|
||||||
const userObj = new User();
|
const userObj = new User();
|
||||||
userObj.id = userId;
|
userObj.id = userId;
|
||||||
if (!await redis.exists(`user:${userId}:displayName`)) {
|
if (!await redis.exists(`user:${userId}:displayName`)) {
|
||||||
@@ -57,6 +64,10 @@ export class User {
|
|||||||
userObj.displayName = displayName!;
|
userObj.displayName = displayName!;
|
||||||
};
|
};
|
||||||
return userObj;
|
return userObj;
|
||||||
|
} catch {
|
||||||
|
logger.err(`Failed to initializer user with id: ${userId}`);
|
||||||
|
return null;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
private async _setCache(userdata: HelixUser) {
|
private async _setCache(userdata: HelixUser) {
|
||||||
|
|||||||
Reference in New Issue
Block a user