|
1 |
| -import { CommandInteraction, GuildMember } from "discord.js"; |
2 |
| -import { SlashCommandBuilder } from "@discordjs/builders"; |
3 |
| -import { getUser } from "../codewars"; |
4 |
| - |
5 |
| -async function getUserInfo(name: string): Promise<string> { |
6 |
| - let user; |
7 |
| - try { |
8 |
| - user = await getUser(name); |
9 |
| - } catch (err) { |
10 |
| - return ''; |
11 |
| - } |
12 |
| - |
13 |
| - const rank = user.ranks.overall.name; |
14 |
| - const honor = user.honor; |
15 |
| - const position = user.leaderboardPosition; |
16 |
| - const authored = user.codeChallenges.totalAuthored; |
17 |
| - const completed = user.codeChallenges.totalCompleted; |
18 |
| - |
19 |
| - return `\`\`\` |
20 |
| -User: ${name} |
21 |
| -Rank: ${rank} |
22 |
| -Honor: ${honor}\ |
23 |
| -${position ? `\nPosition: #${position}`: ''}\ |
24 |
| -${authored ? `\nCreated kata: ${authored}`: ''} |
25 |
| -Completed kata: ${completed} |
26 |
| -\`\`\`` |
27 |
| -}; |
28 |
| - |
29 |
| -export const data = async () => |
30 |
| - new SlashCommandBuilder() |
31 |
| - .setName("userinfo") |
32 |
| - .setDescription("Get info about a Codewars user") |
33 |
| - .addStringOption((opt) => opt.setName("username").setDescription("The username to look up")) |
34 |
| - .addBooleanOption((opt) => opt.setName("ephemeral").setDescription("Hide from others")) |
35 |
| - .toJSON(); |
36 |
| - |
37 |
| -export const call = async (interaction: CommandInteraction) => { |
38 |
| - let username = interaction.options.getString("username"); |
39 |
| - if (!username) { |
40 |
| - const member = interaction.member; |
41 |
| - const displayName = member instanceof GuildMember ? member.displayName : member?.nick; |
42 |
| - if (!displayName) { |
43 |
| - await interaction.reply({ |
44 |
| - content: "Failed to fetch the name of the current user", |
45 |
| - ephemeral: true, |
46 |
| - }); |
47 |
| - return; |
48 |
| - } |
49 |
| - username = displayName; |
50 |
| - } |
51 |
| - |
52 |
| - const ephemeral = interaction.options.getBoolean("ephemeral") || false; |
53 |
| - const content = await getUserInfo(username); |
54 |
| - |
55 |
| - if (!content) { |
56 |
| - await interaction.reply({ |
57 |
| - content: `Could not find user: ${username}`, |
58 |
| - ephemeral: true, |
59 |
| - }); |
60 |
| - return; |
61 |
| - } |
62 |
| - |
63 |
| - await interaction.reply({ |
64 |
| - content: content, |
65 |
| - ephemeral: ephemeral, |
66 |
| - }); |
67 |
| -}; |
| 1 | +import { CommandInteraction, GuildMember, TextChannel } from "discord.js"; |
| 2 | +import { SlashCommandBuilder } from "@discordjs/builders"; |
| 3 | +import { getUser } from "../codewars"; |
| 4 | + |
| 5 | +const REDIRECT: string = "This command is only available in channel **#bot-playground**"; |
| 6 | + |
| 7 | +async function getUserInfo(name: string): Promise<string> { |
| 8 | + let user; |
| 9 | + try { |
| 10 | + user = await getUser(name); |
| 11 | + } catch (err) { |
| 12 | + return ''; |
| 13 | + } |
| 14 | + |
| 15 | + const rank = user.ranks.overall.name; |
| 16 | + const honor = user.honor; |
| 17 | + const position = user.leaderboardPosition; |
| 18 | + const authored = user.codeChallenges.totalAuthored; |
| 19 | + const completed = user.codeChallenges.totalCompleted; |
| 20 | + |
| 21 | + return `\`\`\` |
| 22 | +User: ${name} |
| 23 | +Rank: ${rank} |
| 24 | +Honor: ${honor}\ |
| 25 | +${position ? `\nPosition: #${position}`: ''}\ |
| 26 | +${authored ? `\nCreated kata: ${authored}`: ''} |
| 27 | +Completed kata: ${completed} |
| 28 | +\`\`\`` |
| 29 | +}; |
| 30 | + |
| 31 | +export const data = async () => |
| 32 | + new SlashCommandBuilder() |
| 33 | + .setName("userinfo") |
| 34 | + .setDescription("Get info about a Codewars user") |
| 35 | + .addStringOption((opt) => opt.setName("username").setDescription("The username to look up")) |
| 36 | + .addBooleanOption((opt) => opt.setName("ephemeral").setDescription("Hide from others")) |
| 37 | + .toJSON(); |
| 38 | + |
| 39 | +export const call = async (interaction: CommandInteraction) => { |
| 40 | + let username = interaction.options.getString("username"); |
| 41 | + if (!username) { |
| 42 | + const member = interaction.member; |
| 43 | + const displayName = member instanceof GuildMember ? member.displayName : member?.nick; |
| 44 | + if (!displayName) { |
| 45 | + await interaction.reply({ |
| 46 | + content: "Failed to fetch the name of the current user", |
| 47 | + ephemeral: true, |
| 48 | + }); |
| 49 | + return; |
| 50 | + } |
| 51 | + username = displayName; |
| 52 | + } |
| 53 | + |
| 54 | + const ephemeral = interaction.options.getBoolean("ephemeral") || false; |
| 55 | + |
| 56 | + if (!ephemeral && (interaction.channel as TextChannel).name !== "bot-playground") { |
| 57 | + await interaction.reply({ |
| 58 | + content: REDIRECT, |
| 59 | + ephemeral: true, |
| 60 | + }); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + const content = await getUserInfo(username); |
| 65 | + |
| 66 | + if (!content) { |
| 67 | + await interaction.reply({ |
| 68 | + content: `Could not find user: ${username}`, |
| 69 | + ephemeral: true, |
| 70 | + }); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + await interaction.reply({ |
| 75 | + content: content, |
| 76 | + ephemeral: ephemeral, |
| 77 | + }); |
| 78 | +}; |
0 commit comments