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(user): support finding user by discord mention tag #149

Merged
merged 1 commit into from
May 13, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 29 additions & 39 deletions src/commands/user.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import { getUser, getUserBySteamId } from '@zeepkist/gtr-api'
import {
getUser,
getUserByDiscordId,
getUserBySteamId
} from '@zeepkist/gtr-api'
import {
ApplicationCommandOptionType,
ApplicationCommandType,
CommandInteraction,
EmbedBuilder,
inlineCode
CommandInteraction
} from 'discord.js'

import { Command } from '../command.js'
import { userEmbed } from '../components/embeds/userEmbed.js'
import { userNotFoundEmbed } from '../components/embeds/userNotFoundEmbed.js'
import { database } from '../services/database.js'
import { log } from '../utils/index.js'

export const user: Command = {
name: 'user',
description: 'Get information about a user.',
type: ApplicationCommandType.ChatInput,
options: [
{
name: 'user',
description: 'Discord User',
type: ApplicationCommandOptionType.User,
required: false
},
{
name: 'steamid',
description: "User's Steam ID.",
Expand All @@ -36,51 +43,34 @@ export const user: Command = {
],
ephemeral: false,
run: async (interaction: CommandInteraction) => {
const linkedAccount = await database('linked_accounts')
.select('steamId')
.where({
discordId: interaction.user.id
})
log.info(`Found ${linkedAccount.length} linked accounts.`, interaction)
let discordUser = interaction.options.data.find(
option => option.name === 'user'
)?.user

let steamId = interaction.options.data.find(
const steamId = interaction.options.data.find(
option => option.name === 'steamid'
)?.value as string

const id = interaction.options.data.find(option => option.name === 'id')
?.value as number
log.info(`Steam ID: ${steamId}, ID: ${id}`, interaction)

if ((!linkedAccount || linkedAccount.length === 0) && !steamId && !id) {
log.info(
'No linked account or option arguments provided. Ending interaction.',
interaction
)

const embed = new EmbedBuilder()
.setColor(0xff_92_00)
.setTitle('User not linked')
.setDescription(
`You must provide either a Steam ID or a user ID.\n\nIf you link your Steam account with ${inlineCode(
'/verify'
)}, you can use this command without providing a Steam ID or user ID.`
)
.setTimestamp()

await interaction.editReply({
embeds: [embed]
})
return
}
log.info(
`Discord ID: ${discordUser?.id}, Steam ID: ${steamId}, ID: ${id}`,
interaction
)

if (!steamId && !id) {
steamId = linkedAccount[0].steamId
log.info(`Using linked account Steam ID: ${steamId}`, interaction)
if (!discordUser?.id && !steamId && !id) {
discordUser = interaction.user
}

try {
const user = steamId ? await getUserBySteamId(steamId) : await getUser(id)
const user = discordUser?.id
? await getUserByDiscordId(discordUser.id)
: steamId
? await getUserBySteamId(steamId)
: await getUser(id)
log.info(`Found user: ${user.steamName}`, interaction)
await userEmbed(interaction, user)
await userEmbed(interaction, user, discordUser)
} catch (error) {
log.error(String(error), interaction)
userNotFoundEmbed(interaction)
Expand Down
5 changes: 0 additions & 5 deletions src/models/database/linkedAccount.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/services/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ const initialiseDatabase = async () => {
})
}

const linkedAccounts = await database.schema.hasTable('linked_accounts')
if (!linkedAccounts) {
log.info('Creating table: linked_accounts')
await database.schema.createTable('linked_accounts', table => {
table.string('discordId', 18).notNullable().primary().unique().index()
table.string('steamId', 17).notNullable().unique().index()
table.timestamp('createdAt').notNullable().defaultTo(database.fn.now())
})
}

const paginatedMessages = await database.schema.hasTable('paginated_messages')
if (!paginatedMessages) {
log.info('Creating table: paginated_messages')
Expand Down