Skip to content

Commit

Permalink
Merge pull request #47 from barthofu/15-feature-info-command
Browse files Browse the repository at this point in the history
feat(#15): `info` command
  • Loading branch information
Sykhodev authored Aug 31, 2022
2 parents 576bd2e + 20d3838 commit 162318c
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 30 deletions.
140 changes: 140 additions & 0 deletions src/commands/General/info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { injectable } from "tsyringe"
import { Client } from "discordx"
import { Category } from "@discordx/utilities"
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, CommandInteraction, EmbedBuilder, EmbedField } from "discord.js"
import relativeTime from 'dayjs/plugin/relativeTime'
import dayjs from 'dayjs'
dayjs.extend(relativeTime)

import { Discord, Slash } from "@decorators"
import { Guard } from "@guards"
import { formatDate, getColor, isValidUrl, timeAgo } from "@utils/functions"
import { generalConfig } from "@config"
import { Stats } from "@services"

import packageJSON from '../../../package.json'

const links = [
{ label: 'Invite me!', url: generalConfig.links.invite },
{ label: 'Support server', url: generalConfig.links.supportServer },
{ label: 'Github', url: generalConfig.links.gitRemoteRepo }
]

@Discord()
@injectable()
@Category('General')
export default class InfoCommand {

constructor(
private stats: Stats
) {}

@Slash({
name: 'info',
})
@Guard()
async info(
interaction: CommandInteraction,
client: Client,
{ localize }: InteractionData
) {

const embed = new EmbedBuilder()
.setAuthor({
name: interaction.user.username,
iconURL: interaction.user.displayAvatarURL(),
})
.setTitle(client.user!.tag)
.setThumbnail(client.user!.displayAvatarURL())
.setColor(getColor('primary'))
.setDescription(packageJSON.description)

const fields: EmbedField[] = []

/**
* Owner field
*/
const owner = await client.users.fetch(generalConfig.ownerId)
if (owner) {
fields.push({
name: 'Owner',
value: `\`${owner.tag}\``,
inline: true,
})
}

/**
* Uptime field
*/
const uptime = timeAgo(new Date(Date.now() - client.uptime!))
fields.push({
name: 'Uptime',
value: uptime,
inline: true,
})

/**
* Totals field
*/
const totalStats = await this.stats.getTotalStats()
fields.push({
name: 'Totals',
value: `**${totalStats.TOTAL_GUILDS}** guilds\n**${totalStats.TOTAL_USERS}** users\n**${totalStats.TOTAL_COMMANDS}** commands`,
inline: true,
})

/**
* Bot version field
*/
fields.push({
name: 'Bot version',
value: `v${packageJSON.version}`,
inline: true,
})

/**
* Framework/template field
*/
fields.push({
name: 'Framework/template',
value: `[TSCord](https://github.com/barthofu/tscord) (*v${generalConfig.__templateVersion}*)`,
inline: true,
})

/**
* Libraries field
*/
fields.push({
name: 'Libraries',
value: `[discord.js](https://discord.js.org/) (*v${packageJSON.dependencies['discord.js'].replace('^', '')}*)\n[discordx](https://discordx.js.org/) (*v${packageJSON.dependencies['discordx'].replace('^', '')}*)`,
inline: true,
})

// add the fields to the embed
embed.addFields(fields)

/**
* Define links buttons
*/
const buttons = links
.map(link => {
const url = link.url.split('_').join('')
if (isValidUrl(url)) {
return new ButtonBuilder()
.setLabel(link.label)
.setURL(url)
.setStyle(ButtonStyle.Link)
} else return null
})
.filter(link => link) as ButtonBuilder[]
const row = new ActionRowBuilder<ButtonBuilder>()
.addComponents(...buttons)

// finaly send the embed
interaction.followUp({
embeds: [embed],
components: [row],
})

}
}
2 changes: 1 addition & 1 deletion src/commands/General/invite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class InviteCommand {

const embed = new EmbedBuilder()
.setTitle(localize.COMMANDS.INVITE.EMBED.TITLE())
.setDescription(localize.COMMANDS.INVITE.EMBED.DESCRIPTION({link: generalConfig.inviteLink}))
.setDescription(localize.COMMANDS.INVITE.EMBED.DESCRIPTION({link: generalConfig.links.invite}))
.setColor(getColor('primary'))
.setFooter({ text : 'Powered by DiscBot Team ❤'})

Expand Down
7 changes: 6 additions & 1 deletion src/config/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ export const generalConfig: GeneralConfigType = {
simpleCommandsPrefix: '!',
ownerId: '260908777446965248',
timezone: 'Europe/Paris',
inviteLink: 'https://www.change_invite_link_here.com',

links: {
invite: 'https://www.change_invite_link_here.com',
supportServer: 'https://discord.com/your_invitation_link',
gitRemoteRepo: 'https://github.com/barthofu/tscord',
},

automaticUploadImagesToImgur: false,

Expand Down
5 changes: 5 additions & 0 deletions src/utils/functions/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ const dateMasks = {
export const formatDate = (date: Date, mask: keyof typeof dateMasks = 'default') => {

return datejs(date).format(dateMasks[mask])
}

export const timeAgo = (date: Date) => {

return dayjs(date).fromNow()
}
10 changes: 10 additions & 0 deletions src/utils/functions/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,14 @@ export const constantPreserveDots = (string: string) => {
.split('.')
.map(word => constant(word))
.join('.')
}

export const isValidUrl = (url: string) => {

try {
new URL(url)
return true
} catch {
return false
}
}
61 changes: 33 additions & 28 deletions src/utils/types/configs.d.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
type GeneralConfigType = {

__templateVersion: string,
__templateVersion: string

name: string,
description: string,
defaultLocale: import('@i18n').Locales,
simpleCommandsPrefix: string,
ownerId: string,
timezone: string,
automaticUploadImagesToImgur: boolean,
inviteLink: string,
name: string
description: string
defaultLocale: import('@i18n').Locales
simpleCommandsPrefix: string
ownerId: string
timezone: string
automaticUploadImagesToImgur: boolean

devs: string[],
links: {
invite: string
supportServer: string
gitRemoteRepo: string
}

devs: string[]

eval: {
name: string,
name: string
onlyOwner: boolean
},
}

activities: {
text: string,
text: string
type: "PLAYING" | "STREAMING" | "LISTENING" | "WATCHING" | "CUSTOM" | "COMPETING"
}[],
}[]

}

Expand All @@ -30,44 +35,44 @@ type DatabaseConfigType = {
path: `${string}/`

backup: {
enabled: boolean,
enabled: boolean
path: `${string}/`
}
}

type LogsConfigType = {

debug: boolean,
debug: boolean

interaction: {
file: boolean,
console: boolean,
channel: string | null,
file: boolean
console: boolean
channel: string | null

exclude: InteractionsConstants[]
}

simpleCommand: {
file: boolean,
console: boolean,
file: boolean
console: boolean
channel: string | null
}

newUser: {
file: boolean,
console: boolean,
file: boolean
console: boolean
channel: string | null
}

guild: {
file: boolean,
console: boolean,
file: boolean
console: boolean
channel: string | null
}

error: {
file: boolean,
console: boolean,
file: boolean
console: boolean
channel: string | null
}
}
Expand All @@ -82,5 +87,5 @@ type StatsConfigType = {

type APIConfigType = {

port: number,
port: number
}

0 comments on commit 162318c

Please sign in to comment.