Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(customvariables): add displayname to user function #4507

Merged
merged 1 commit into from Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/bot/customvariables.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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),
Expand All @@ -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,
Expand Down
18 changes: 13 additions & 5 deletions src/bot/microservices/getUserFromTwitch.ts
Expand Up @@ -3,11 +3,21 @@ import { getRepository } from 'typeorm';

import { Settings } from '../database/entity/settings';

export const getUserFromTwitch = async (username: string) => {
type Await<T> = T extends {
then(onfulfilled?: (value: infer U) => unknown): unknown;
} ? U : T;

export const getUserFromTwitch = async (username: string): Promise<Await<ReturnType<typeof getUsersFromTwitch>>[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=')}`;
/*
{
Expand Down Expand Up @@ -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;
};
8 changes: 4 additions & 4 deletions 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';
Expand Down Expand Up @@ -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)),
Expand All @@ -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<Readonly<Required<UserInterface>>>['select']) {
const userByUsername = await getRepository(User).findOne({ where: { username }, select});

if (userByUsername) {
return userByUsername;
Expand Down
@@ -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
Expand Down