mirror of
https://github.com/qwerinope/qweribot.git
synced 2025-12-19 08:41:39 +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 */
|
||||
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;
|
||||
return nice.split(' ').slice(sliceLength);
|
||||
};
|
||||
|
||||
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
|
||||
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 { chatterApi } from ".";
|
||||
import { HelixUser } from "@twurple/api"
|
||||
import logger from "./lib/logger";
|
||||
|
||||
const EXPIRETIME = 60 * 60 // 60 minutes
|
||||
|
||||
@@ -20,8 +21,9 @@ export class User {
|
||||
public displayName!: string;
|
||||
|
||||
static async initUsername(username: string): Promise<User | null> {
|
||||
try {
|
||||
const userObj = new User();
|
||||
userObj.username = username;
|
||||
userObj.username = username.replaceAll(/[@]/g, '');
|
||||
const userid = await redis.get(`userlookup:${username}`);
|
||||
if (!userid) {
|
||||
const userdata = await chatterApi.users.getUserByName(username);
|
||||
@@ -36,9 +38,14 @@ export class User {
|
||||
userObj.displayName = displayname!;
|
||||
};
|
||||
return userObj;
|
||||
} catch {
|
||||
logger.err(`Failed to initialize user with name: ${username}`);
|
||||
return null;
|
||||
};
|
||||
};
|
||||
|
||||
static async initUserId(userId: string): Promise<User | null> {
|
||||
try {
|
||||
const userObj = new User();
|
||||
userObj.id = userId;
|
||||
if (!await redis.exists(`user:${userId}:displayName`)) {
|
||||
@@ -57,6 +64,10 @@ export class User {
|
||||
userObj.displayName = displayName!;
|
||||
};
|
||||
return userObj;
|
||||
} catch {
|
||||
logger.err(`Failed to initializer user with id: ${userId}`);
|
||||
return null;
|
||||
};
|
||||
};
|
||||
|
||||
private async _setCache(userdata: HelixUser) {
|
||||
|
||||
Reference in New Issue
Block a user