Skip to content

Commit

Permalink
more debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
bristermitten committed Aug 8, 2023
1 parent a10e441 commit b430480
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "devdenbot",
"version": "7.0.2",
"version": "7.0.3",
"type": "module",
"main": "src/index.ts",
"scripts": {
Expand Down
38 changes: 20 additions & 18 deletions src/modules/learning/learning.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { LearningResource } from './learningResource.model.js'
const resources: Array<{ name: string, value: string }> = []

export async function updateResourcesForCommands () {
logger.debug('Updating resource for commands')
await updateAllResources()
const result = getAllCachedResources()
.map(([fileName, res]) => ({
Expand All @@ -24,10 +25,11 @@ export async function updateResourcesForCommands () {
}))
resources.length = 0
resources.push(...result)
logger.debug(`resources = ${JSON.stringify(resources)}`)
}

const extraFooter =
'\n\n[**Contribute to our resource collection!**](https://github.com/TheDeveloperDen/LearningResources)'
'\n\n[**Contribute to our resource collection!**](https://github.com/TheDeveloperDen/LearningResources)'

function createBulletList (title: string, entries: string[]) {
if (entries.length === 0) return ''
Expand All @@ -44,17 +46,17 @@ export function getResourceEmbed (
const embed = createStandardEmbed(member)
.setTitle(resourceSet.name)
.setDescription(
`**${resourceSet.description}**\n\n` +
resourceSet.resources
.map((res) => {
const pros = createBulletList('Pros', res.pros)
const cons = createBulletList('Cons', res.cons)
const description = res.description ? `${res.description}\n` : ''
const linkedName = `[${res.name}](${res.url})`
const price = res.price ? `${res.price}` : 'Free!'
return `${linkedName} - ${price}${description}\n${pros}\n${cons}`.trim()
}).join('\n\n') +
extraFooter
`**${resourceSet.description}**\n\n` +
resourceSet.resources
.map((res) => {
const pros = createBulletList('Pros', res.pros)
const cons = createBulletList('Cons', res.cons)
const description = res.description ? `${res.description}\n` : ''
const linkedName = `[${res.name}](${res.url})`
const price = res.price ? `${res.price}` : 'Free!'
return `${linkedName} - ${price}${description}\n${pros}\n${cons}`.trim()
}).join('\n\n') +
extraFooter
)

if (!user || !member) {
Expand All @@ -76,7 +78,7 @@ export function getResourceEmbed (

if (!emoji) {
logger.warn(
`Could not find emoji ${resourceSet.emoji} for resource ${resourceSet.name}`
`Could not find emoji ${resourceSet.emoji} for resource ${resourceSet.name}`
)
} else {
embed.setTitle(`${stringifyEmoji(emoji)} ${resourceSet.name}`)
Expand Down Expand Up @@ -104,7 +106,7 @@ const LearningGetSubcommand: ExecutableSubcommand = {
const resource = await getResource(name)
if (resource == null) {
return await interaction.reply(
`Could not find resource ${name}`
`Could not find resource ${name}`
)
}

Expand Down Expand Up @@ -154,10 +156,10 @@ const LearningListSubcommand: ExecutableSubcommand = {
.setFooter({
...standardFooter(),
text: `Requested by ${
pseudoMention(
interaction.user
)
} | Learning Resources`
pseudoMention(
interaction.user
)
} | Learning Resources`
})

await interaction.reply({
Expand Down
3 changes: 2 additions & 1 deletion src/modules/learning/resourcesCache.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function updateAllResources () {
cache.clear();
(await queryAll()).forEach(([fileName, resource]) => {
cache.set(fileName, resource)
logger.info(`Updated cache for ${resource.name}`)
logger.info(`Updated cache for ${resource.name} to ${JSON.stringify(resource)}`)
})
}

Expand All @@ -39,6 +39,7 @@ export function getAllCachedResources (): Array<[FileName, LearningResource]> {
const baseUrl = 'https://learningresources.developerden.org'

async function queryResource (fileName: FileName): Promise<LearningResource | null> {
logger.debug(`Querying resource ${fileName}...`)
const resource = (await fetch(`${baseUrl}/${fileName}`))
.text()
.then((r) => parse(r))
Expand Down
44 changes: 22 additions & 22 deletions src/util/users.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import {GuildMember, PartialGuildMember, User} from 'discord.js'
import {config} from '../Config.js'
import { GuildMember, PartialGuildMember, User } from 'discord.js'
import { config } from '../Config.js'

export const userShouldBePinged = (
user: GuildMember | PartialGuildMember
user: GuildMember | PartialGuildMember
): boolean =>
!user.roles.cache.has(
config.roles.noPing
)
!user.roles.cache.has(
config.roles.noPing
)

export const pseudoMention = (user: User): string =>
user.discriminator === '0' ? user.username : `${user.username}#${user.discriminator}`
user.discriminator === '0' ? user.username : `${user.username}#${user.discriminator}`

export const mention = (
user: GuildMember | User | PartialGuildMember
user: GuildMember | User | PartialGuildMember
): string => {
if (user instanceof User) {
return actualMention(user)
} else {
return userShouldBePinged(user)
? actualMention(user)
: pseudoMention(
user.user
)
}
if (user instanceof User) {
return actualMention(user)
} else {
return userShouldBePinged(user)
? actualMention(user)
: pseudoMention(
user.user
)
}
}
export const actualMention = (
user: GuildMember | User | PartialGuildMember
user: GuildMember | User | PartialGuildMember
): string => `<@${user.id}>`

export const mentionWithNoPingMessage = (user: GuildMember): string =>
userShouldBePinged(user)
? `<@${user.id}> (Don't want to be pinged? </role No Ping:1059214166075912222>)`
: pseudoMention(user.user)
userShouldBePinged(user)
? `<@${user.id}> (Don't want to be pinged? </role No Ping:1059214166075912222>)`
: pseudoMention(user.user)

export const isSpecialUser = (user: GuildMember): boolean =>
user.premiumSinceTimestamp !=
user.premiumSinceTimestamp !=
null ||
user.roles.cache.has(config.roles.staff) ||
user.roles.cache.has(config.roles.notable ?? '')

0 comments on commit b430480

Please sign in to comment.