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 */
|
/** 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);
|
||||||
|
|||||||
75
src/user.ts
75
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,43 +21,53 @@ 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> {
|
||||||
const userObj = new User();
|
try {
|
||||||
userObj.username = username;
|
const userObj = new User();
|
||||||
const userid = await redis.get(`userlookup:${username}`);
|
userObj.username = username.replaceAll(/[@]/g, '');
|
||||||
if (!userid) {
|
const userid = await redis.get(`userlookup:${username}`);
|
||||||
const userdata = await chatterApi.users.getUserByName(username);
|
if (!userid) {
|
||||||
if (!userdata) return null;
|
const userdata = await chatterApi.users.getUserByName(username);
|
||||||
userObj._setCache(userdata);
|
if (!userdata) return null;
|
||||||
userObj.id = userdata.id;
|
userObj._setCache(userdata);
|
||||||
userObj.displayName = userdata.displayName;
|
userObj.id = userdata.id;
|
||||||
} else {
|
userObj.displayName = userdata.displayName;
|
||||||
const displayname = await redis.get(`user:${userid}:displayName`);
|
} else {
|
||||||
userObj._setExpire(userid, username);
|
const displayname = await redis.get(`user:${userid}:displayName`);
|
||||||
userObj.id = userid;
|
userObj._setExpire(userid, username);
|
||||||
userObj.displayName = displayname!;
|
userObj.id = userid;
|
||||||
|
userObj.displayName = displayname!;
|
||||||
|
};
|
||||||
|
return userObj;
|
||||||
|
} catch {
|
||||||
|
logger.err(`Failed to initialize user with name: ${username}`);
|
||||||
|
return null;
|
||||||
};
|
};
|
||||||
return userObj;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static async initUserId(userId: string): Promise<User | null> {
|
static async initUserId(userId: string): Promise<User | null> {
|
||||||
const userObj = new User();
|
try {
|
||||||
userObj.id = userId;
|
const userObj = new User();
|
||||||
if (!await redis.exists(`user:${userId}:displayName`)) {
|
userObj.id = userId;
|
||||||
const userdata = await chatterApi.users.getUserById(userId);
|
if (!await redis.exists(`user:${userId}:displayName`)) {
|
||||||
if (!userdata) return null;
|
const userdata = await chatterApi.users.getUserById(userId);
|
||||||
userObj._setCache(userdata);
|
if (!userdata) return null;
|
||||||
userObj.username = userdata.name;
|
userObj._setCache(userdata);
|
||||||
userObj.displayName = userdata.displayName;
|
userObj.username = userdata.name;
|
||||||
} else {
|
userObj.displayName = userdata.displayName;
|
||||||
const [displayName, username] = await Promise.all([
|
} else {
|
||||||
redis.get(`user:${userId}:displayName`),
|
const [displayName, username] = await Promise.all([
|
||||||
redis.get(`user:${userId}:username`)
|
redis.get(`user:${userId}:displayName`),
|
||||||
]);
|
redis.get(`user:${userId}:username`)
|
||||||
userObj._setExpire(userId, username!);
|
]);
|
||||||
userObj.username = username!;
|
userObj._setExpire(userId, username!);
|
||||||
userObj.displayName = displayName!;
|
userObj.username = username!;
|
||||||
|
userObj.displayName = displayName!;
|
||||||
|
};
|
||||||
|
return userObj;
|
||||||
|
} catch {
|
||||||
|
logger.err(`Failed to initializer user with id: ${userId}`);
|
||||||
|
return null;
|
||||||
};
|
};
|
||||||
return userObj;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
private async _setCache(userdata: HelixUser) {
|
private async _setCache(userdata: HelixUser) {
|
||||||
|
|||||||
Reference in New Issue
Block a user