Files
Yumi/src/discord/UserInfo.ts
T

219 lines
8.5 KiB
TypeScript
Raw Normal View History

2022-06-29 15:27:42 +07:00
import { Message, CommandInteraction, Presence, PresenceStatus, ActivityType } from 'discord.js';
2022-06-07 03:08:27 +07:00
import { getColorFromURL, Palette } from 'color-thief-node';
import { I18n } from 'i18n';
2022-03-03 20:56:49 +07:00
2022-06-06 14:59:53 +07:00
import DiscordModule, { HybridInteractionMessage } from '../utils/DiscordModule';
2022-06-07 03:08:27 +07:00
import { sendHybridInteractionMessageResponse, makeErrorEmbed, makeInfoEmbed } from '../utils/DiscordMessage';
import Locale from '../services/Locale';
interface UserInfoData {
displayName: string;
tag: string;
status: PresenceStatus | null;
joinedAt: Date | null;
displayAvatarURL: string;
isGuildOwner: boolean;
presence: Presence | null;
colorThiefColor: Palette | null;
}
2021-09-20 12:37:38 -07:00
const EMBEDS = {
2022-06-07 03:08:27 +07:00
INFO: (data: HybridInteractionMessage, locale: I18n) => {
2022-06-06 14:59:53 +07:00
return makeInfoEmbed({
2022-06-07 03:08:27 +07:00
title: locale.__('userinfo.title'),
description: locale.__('userinfo.info'),
2021-09-20 12:37:38 -07:00
fields: [
{
2022-06-07 03:08:27 +07:00
name: locale.__('common.available_args'),
value: locale.__('userinfo.valid_args')
2021-09-20 12:37:38 -07:00
}
],
2022-06-07 03:08:27 +07:00
user: data.getUser()
2021-09-20 12:37:38 -07:00
});
},
2022-06-07 03:08:27 +07:00
USER_NOT_FOUND: (data: HybridInteractionMessage, locale: I18n) => {
2022-06-06 14:59:53 +07:00
return makeErrorEmbed({
2022-06-07 03:08:27 +07:00
title: locale.__('userinfo.user_not_found'),
user: data.getUser()
2021-09-20 12:37:38 -07:00
});
2022-06-07 03:08:27 +07:00
},
USER_INFO: (data: HybridInteractionMessage, locale: I18n, user: UserInfoData) => {
let readableStatus: string;
switch (user.status) {
case 'online':
readableStatus = `🟢 ${locale.__('userinfo.online')}`;
break;
case 'idle':
readableStatus = `🌙 ${locale.__('userinfo.idle')}`;
break;
case 'dnd':
readableStatus = `⛔ ${locale.__('userinfo.dnd')}`;
break;
case 'offline':
readableStatus = `⚫ ${locale.__('userinfo.offline')}`;
break;
default:
readableStatus = `⚫ ${locale.__('userinfo.unknown')}`;
break;
}
let embed = makeInfoEmbed({
icon: null,
title: user.tag,
fields: [
{
name: `${readableStatus}`,
value: `\u200b`
}
],
color: user.colorThiefColor || undefined,
user: data.getUser()
});
if (user.presence?.activities) {
for (let activity of user.presence?.activities) {
2022-06-29 15:27:42 +07:00
if (activity.type === ActivityType.Custom)
embed.addFields({
name: `✨ ${locale.__('userinfo.custom_status')}`,
value: `${
2022-06-07 03:08:27 +07:00
!activity.emoji
? ''
: `${
activity.emoji?.identifier.startsWith('<') &&
activity.emoji?.identifier.endsWith('>') &&
activity.emoji?.identifier.split(':').length == 2
? activity.emoji?.identifier.split(':')[0]
: activity.emoji?.name
}`
} ${activity.state === null ? '' : activity.state}
\u200b`,
2022-06-29 15:27:42 +07:00
inline: false
});
2022-06-07 03:08:27 +07:00
else {
let title = '';
switch (activity.type) {
2022-06-29 15:27:42 +07:00
case ActivityType.Playing:
title = `'🕹 ${locale.__('userinfo.playing_x', { NAME: activity.name })}`;
2022-06-07 03:08:27 +07:00
break;
2022-06-29 15:27:42 +07:00
case ActivityType.Streaming:
title = `'🔴 ${locale.__('userinfo.streaming_x', { NAME: activity.name })}`;
2022-06-07 03:08:27 +07:00
break;
2022-06-29 15:27:42 +07:00
case ActivityType.Listening:
title = `🎵 ${locale.__('userinfo.listening_x', { NAME: activity.name })}`;
2022-06-07 03:08:27 +07:00
break;
2022-06-29 15:27:42 +07:00
case ActivityType.Watching:
title = `📺 ${locale.__('userinfo.watching_x', { NAME: activity.name })}`;
2022-06-07 03:08:27 +07:00
break;
2022-06-29 15:27:42 +07:00
case ActivityType.Competing:
2022-06-29 21:00:58 +07:00
title = `🌠 ${locale.__('userinfo.competing_x', { NAME: activity.name })}`;
2022-06-07 03:08:27 +07:00
break;
}
2022-06-29 21:00:58 +07:00
const startTime = activity.timestamps?.start;
const endTime = activity.timestamps?.end;
2022-06-29 15:27:42 +07:00
embed.addFields({
name: `${title}`,
value: `${activity.details === null ? '' : activity.details}
2022-06-29 21:00:58 +07:00
${activity.state === null ? '' : activity.state}
${
startTime
? `Since <t:${Math.round(startTime.getTime() / 1000)}:R>`
: ''
}${
endTime
? `\nEnd <t:${Math.round(endTime.getTime() / 1000)}:R>`
: ''
}
\u200b`,
2022-06-29 15:27:42 +07:00
inline: false
});
2022-06-07 03:08:27 +07:00
}
}
}
2022-06-29 15:27:42 +07:00
embed.addFields({
name: `📰 ${locale.__('userinfo.user_guild_info')}`,
value: `${
2022-06-07 03:08:27 +07:00
user.joinedAt === null
? locale.__('userinfo.join_date_unknown')
2022-06-29 15:27:42 +07:00
: locale.__('userinfo.join_date', {
TIME: `<t:${Math.round(user.joinedAt.getTime() / 1000).toString()}:R>`
})
2022-06-07 03:08:27 +07:00
}
${user.isGuildOwner ? `${locale.__('userinfo.guild_owner')}` : ''}
`
2022-06-29 15:27:42 +07:00
});
2022-06-07 03:08:27 +07:00
embed.setThumbnail(user.displayAvatarURL + '?size=4096');
embed.setAuthor({
name: user.displayName,
iconURL: user.displayAvatarURL + '?size=4096'
});
return embed;
2021-09-20 12:37:38 -07:00
}
2022-06-06 14:59:53 +07:00
};
2021-09-20 12:37:38 -07:00
2022-03-03 20:56:49 +07:00
export default class UserInfo extends DiscordModule {
2022-06-06 14:59:53 +07:00
public id = 'Discord_UserInfo';
public commands = ['userinfo'];
public commandInteractionName = 'userinfo';
2022-03-03 20:56:49 +07:00
async GuildOnModuleCommand(args: any, message: Message) {
await this.run(new HybridInteractionMessage(message), args);
2021-09-20 12:37:38 -07:00
}
2022-06-06 14:59:53 +07:00
async GuildModuleCommandInteractionCreate(interaction: CommandInteraction) {
2022-03-03 20:56:49 +07:00
await this.run(new HybridInteractionMessage(interaction), interaction.options);
2021-09-20 12:37:38 -07:00
}
2022-03-03 20:56:49 +07:00
async run(data: HybridInteractionMessage, args: any) {
2022-06-07 03:08:27 +07:00
const guild = data.getGuild();
if (!guild) return;
const locale = await Locale.getGuildLocale(guild.id);
2021-09-20 12:37:38 -07:00
let query;
2022-06-06 14:59:53 +07:00
if (data.isMessage()) {
if (args.length === 0)
return await sendHybridInteractionMessageResponse(data, {
2022-06-07 03:08:27 +07:00
embeds: [EMBEDS.INFO(data, locale)]
2022-06-06 14:59:53 +07:00
});
2021-09-20 12:37:38 -07:00
2022-06-06 14:59:53 +07:00
if (typeof data.getMessage().mentions.users.first() !== 'undefined')
2022-03-03 20:56:49 +07:00
query = data.getMessage().mentions.users.first()?.id;
2022-06-06 14:59:53 +07:00
else query = args[0];
2022-06-29 15:27:42 +07:00
} else if (data.isApplicationCommand()) query = data.getSlashCommand().options.getUser('user')?.id;
2021-09-20 12:37:38 -07:00
2022-03-03 20:56:49 +07:00
// Find the user want to look up
2022-06-07 03:08:27 +07:00
let TargetMember = (await guild.members.fetch()).get(query);
2022-06-06 14:59:53 +07:00
if (!TargetMember)
return await sendHybridInteractionMessageResponse(data, {
2022-06-07 03:08:27 +07:00
embeds: [EMBEDS.USER_NOT_FOUND(data, locale)]
2022-06-06 14:59:53 +07:00
});
2021-09-20 12:37:38 -07:00
2022-06-29 15:27:42 +07:00
if (data.isApplicationCommand()) await data.getMessageComponentInteraction().deferReply();
2021-09-20 12:37:38 -07:00
2022-06-07 03:08:27 +07:00
let colorthief = null;
2022-06-06 14:59:53 +07:00
try {
2022-06-07 03:08:27 +07:00
colorthief = await getColorFromURL(TargetMember.user.displayAvatarURL().replace('.webp', '.jpg'));
2022-06-06 14:59:53 +07:00
} catch (err) {}
2021-09-20 12:37:38 -07:00
2022-06-07 03:08:27 +07:00
const userInfoData: UserInfoData = {
tag: TargetMember.user.tag,
status: TargetMember.presence?.status ? TargetMember.presence?.status : null,
displayName: TargetMember.displayName,
joinedAt: TargetMember.joinedAt,
displayAvatarURL: TargetMember.displayAvatarURL(),
presence: TargetMember.presence || null,
isGuildOwner: TargetMember.id === guild.ownerId,
colorThiefColor: colorthief
};
2021-09-20 12:37:38 -07:00
2022-06-07 03:08:27 +07:00
return await sendHybridInteractionMessageResponse(data, {
embeds: [EMBEDS.USER_INFO(data, locale, userInfoData)]
});
2021-09-20 12:37:38 -07:00
}
2022-06-06 14:59:53 +07:00
}