From e1b79056548edfacb3696b019917d55fa1a04cf4 Mon Sep 17 00:00:00 2001 From: soge__ Date: Mon, 4 Jan 2021 20:51:12 +0100 Subject: [PATCH] feat(customvariables): add displayname to user function Fixes https://discord.com/channels/317348946144002050/689472699910717522/795723169239531540 --- src/bot/customvariables.ts | 32 +++++++++++++++++-- src/bot/microservices/getUserFromTwitch.ts | 18 ++++++++--- src/bot/users.ts | 8 ++--- .../custom-variables-code.txt | 2 +- 4 files changed, 48 insertions(+), 12 deletions(-) diff --git a/src/bot/customvariables.ts b/src/bot/customvariables.ts index 45b07d9f333..094777ac0f9 100644 --- a/src/bot/customvariables.ts +++ b/src/bot/customvariables.ts @@ -24,6 +24,7 @@ import { permission } from './helpers/permissions'; import { addToViewersCache, getFromViewersCache } from './helpers/permissions'; import { adminEndpoint } from './helpers/socket'; import Message from './message'; +import { getUserFromTwitch } from './microservices/getUserFromTwitch'; import permissions from './permissions'; import users from './users'; import custom_variables from './widgets/customvariables'; @@ -316,7 +317,8 @@ class CustomVariables extends Core { if (_user) { const userObj = { username, - id: await users.getIdByName(username), + id: String(_user.userId), + displayname: _user.displayname, is: { online: _user.isOnline ?? false, follower: get(_user, 'is.follower', false), @@ -327,7 +329,33 @@ class CustomVariables extends Core { }; return userObj; } else { - return null; + try { + // we don't have data of user, we will try to get them + const userFromTwitch = await getUserFromTwitch(username); + const createdUser = await getRepository(User).save({ + username, + userId: Number(userFromTwitch.id), + displayname: userFromTwitch.display_name, + profileImageUrl: userFromTwitch.profile_image_url, + }); + + const userObj = { + username, + id: String(createdUser.userId), + displayname: createdUser.displayname, + is: { + online: createdUser.isOnline ?? false, + follower: get(createdUser, 'is.follower', false), + vip: get(createdUser, 'is.vip', false), + subscriber: get(createdUser, 'is.subscriber', false), + mod: isModerator(createdUser), + }, + }; + return userObj; + } catch (e) { + error(e.stack); + return null; + } } }, ...customVariables, diff --git a/src/bot/microservices/getUserFromTwitch.ts b/src/bot/microservices/getUserFromTwitch.ts index c34bc2f7810..f4a115def2b 100644 --- a/src/bot/microservices/getUserFromTwitch.ts +++ b/src/bot/microservices/getUserFromTwitch.ts @@ -3,11 +3,21 @@ import { getRepository } from 'typeorm'; import { Settings } from '../database/entity/settings'; -export const getUserFromTwitch = async (username: string) => { +type Await = T extends { + then(onfulfilled?: (value: infer U) => unknown): unknown; +} ? U : T; + +export const getUserFromTwitch = async (username: string): Promise>[number]> => { + const user = (await getUsersFromTwitch([username]))[0]; + if (typeof user === 'undefined') { + throw new Error(`User ${username} not found on twitch`); + } return (await getUsersFromTwitch([username]))[0]; }; -export const getUsersFromTwitch = async (usernames: string[]) => { +export const getUsersFromTwitch = async (usernames: string[]): Promise<{ + id: string; login: string; display_name: string; profile_image_url: string; +}[]> => { const url = `https://api.twitch.tv/helix/users?login=${usernames.join('&login=')}`; /* { @@ -55,7 +65,5 @@ export const getUsersFromTwitch = async (usernames: string[]) => { }, }); - return request.data.data as { - id: string; login: string; display_name: string; profile_image_url: string; - }[]; + return request.data.data; }; \ No newline at end of file diff --git a/src/bot/users.ts b/src/bot/users.ts index f138a544620..3dcf8f0b9cb 100644 --- a/src/bot/users.ts +++ b/src/bot/users.ts @@ -1,7 +1,7 @@ import { setTimeout } from 'timers'; import axios from 'axios'; -import { Brackets, getConnection, getRepository, IsNull } from 'typeorm'; +import { Brackets, FindOneOptions, getConnection, getRepository, IsNull } from 'typeorm'; import Core from './_interface'; import api from './api'; @@ -157,7 +157,7 @@ class Users extends Core { if (username.startsWith('@')) { username = username.substring(1); } - const user = await getRepository(User).findOne({ username }); + const user = await getRepository(User).findOne({ where: { username }, select: ['userId'] }); if (!user) { const savedUser = await getRepository(User).save({ userId: Number(await api.getIdFromTwitch(username)), @@ -168,8 +168,8 @@ class Users extends Core { return user.userId; } - async getUserByUsername(username: string) { - const userByUsername = await getRepository(User).findOne({ where: { username }}); + async getUserByUsername(username: string, select?: FindOneOptions>>['select']) { + const userByUsername = await getRepository(User).findOne({ where: { username }, select}); if (userByUsername) { return userByUsername; diff --git a/src/panel/views/registries/custom-variables/custom-variables-code.txt b/src/panel/views/registries/custom-variables/custom-variables-code.txt index 40e20461f47..ae179b97b3f 100644 --- a/src/panel/views/registries/custom-variables/custom-variables-code.txt +++ b/src/panel/views/registries/custom-variables/custom-variables-code.txt @@ -1,6 +1,6 @@ /* Available functions info(text: string), warning(text: string) - info and warning logging - user(username?: string): Promise<{ username: string, id: string, is: { follower: boolean, mod: boolean, online: boolean, subscriber: boolean, vip: boolean }}> - returns user object (if null -> sender) + user(username?: string): Promise<{ username: string, displayname: string, id: string, is: { follower: boolean, mod: boolean, online: boolean, subscriber: boolean, vip: boolean }}> - returns user object (if null -> sender) url(url: string, opts?: { method: 'POST' | 'GET', headers: object, data: object}): Promise<{ data: object, status: number, statusText: string}> * * Available variables