Skip to content

Commit 965c958

Browse files
committed
Split long passive/constellation descriptions
1 parent 634e6c3 commit 965c958

File tree

6 files changed

+48
-19
lines changed

6 files changed

+48
-19
lines changed

src/HuTaoClient.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import Discord, { ActivityType, ClientEvents, GatewayIntentBits, Partials } from "discord.js"
2-
import Enmap from "enmap"
32
import fs from "fs"
43
import { join } from "path"
54

@@ -36,7 +35,7 @@ export default class HuTaoClient extends Discord.Client {
3635
newsManager: NewsManager = new NewsManager()
3736
webManager: WebManager = new WebManager()
3837

39-
commands: Enmap<string, Command> = new Enmap()
38+
commands: Map<string, Command> = new Map()
4039
recentMessages: Discord.Message[] = []
4140

4241
constructor() {
@@ -76,8 +75,7 @@ export default class HuTaoClient extends Discord.Client {
7675
fs.readdir(join(__dirname, "./events/"), (err, files) => {
7776
if (err) return Logger.error(err)
7877
files.forEach(file => {
79-
// eslint-disable-next-line @typescript-eslint/no-var-requires
80-
const event = require(`./events/${file}`)
78+
const event: any = require(`./events/${file}`)
8179
const eventName = file.split(".")[0] as keyof ClientEvents
8280
this.on(eventName, event.handle)
8381
})
@@ -89,8 +87,7 @@ export default class HuTaoClient extends Discord.Client {
8987
if (err) return Logger.error(err)
9088
files.forEach(file => {
9189
if (!(file.endsWith(".js") || file.endsWith(".ts"))) return readDir(dir + file + "/")
92-
// eslint-disable-next-line @typescript-eslint/no-var-requires
93-
const props = require(`${dir}${file}`)
90+
const props: any = require(`${dir}${file}`)
9491
const commandName = file.split(".")[0]
9592
Logger.info(`Loading ${commandName}`)
9693

src/commands/characters/character.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import config from "../../data/config.json"
33
import client from "../../main"
44
import Command from "../../utils/Command"
55
import { BotEmoji, Character, CharacterFull, CommandSource, SendMessage, Skill, TalentTable, TalentValue } from "../../utils/Types"
6-
import { addArg, Bookmarkable, Colors, createTable, findFuzzyBestCandidatesForAutocomplete, getLink, getLinkToGuide, PAD_END, PAD_START, paginator, sendMessage, simplePaginator, urlify } from "../../utils/Utils"
6+
import { Bookmarkable, Colors, PAD_END, PAD_START, addArg, createTable, findFuzzyBestCandidatesForAutocomplete, getLink, getLinkToGuide, paginator, sendMessage, simplePaginator, splitByLength, urlify } from "../../utils/Utils"
77

88

99
const elementTypes = client.data.getCharacters()
@@ -519,7 +519,6 @@ ${ Object
519519

520520
if (skill.video && talentMode == "LITTLE") {
521521
embed.setImage(getLink(skill.video))
522-
.setThumbnail("")
523522
}
524523
}
525524

@@ -543,12 +542,18 @@ ${ Object
543542
embed.setTitle(`${char.name}: Passives`)
544543
.setURL(`${data.baseURL}characters/${urlify(char.name, false)}#${urlify(skills.passive[0].name, false)}`)
545544
for (const passive of skills.passive) {
546-
if (passive.minAscension)
547-
embed.addFields({ name: passive.name, value: `${passive.desc}
545+
const descText = passive.minAscension ? `${passive.desc}
548546
549-
*${passive.minAscension > 0 ? `Unlocks at ascension **${passive.minAscension}**` : "Unlocked by **default**"}*` })
550-
else
551-
embed.addFields({ name: passive.name, value: passive.desc })
547+
*${passive.minAscension > 0 ? `Unlocks at ascension **${passive.minAscension}**` : "Unlocked by **default**"}*` : passive.desc
548+
549+
const splitted = descText.split("\n\n").flatMap(x => splitByLength(x, 1000, "\n"))
550+
for (let i = 0; i < splitted.length; i++) {
551+
const line = splitted[i]
552+
if (i == 0)
553+
embed.addFields({ name: `${passive.minAscension ? "*" : ""}${passive.name}`, value: line })
554+
else
555+
embed.addFields({ name: ` `, value: line })
556+
}
552557
}
553558
return embed
554559
}
@@ -558,8 +563,17 @@ ${ Object
558563
.setURL(`${data.baseURL}characters/${urlify(char.name, false)}#${urlify(skills.constellations[0].name, false)}`)
559564
.setThumbnail(getLink(skills.constellations[0]?.icon))
560565
let c = 0
561-
for (const constellation of skills.constellations)
562-
embed.addFields({ name: `C${++c}: ${constellation.name}`, value: constellation.desc })
566+
for (const constellation of skills.constellations) {
567+
const splitted = constellation.desc.split("\n\n").flatMap(x => splitByLength(x, 1000, "\n"))
568+
569+
for (let i = 0; i < splitted.length; i++) {
570+
const line = splitted[i]
571+
if (i == 0)
572+
embed.addFields({ name: `C${++c}: ${constellation.name}`, value: line })
573+
else
574+
embed.addFields({ name: ` `, value: line })
575+
}
576+
}
563577

564578
return embed
565579
}

src/commands/meta/help.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default class Help extends Command {
2929
}
3030

3131
async autocomplete(source: AutocompleteInteraction): Promise<void> {
32-
const targetNames = client.commands.keyArray()
32+
const targetNames = [...client.commands.keys()]
3333
const search = source.options.getFocused().toString()
3434

3535
if (search == "") {
@@ -61,7 +61,7 @@ export default class Help extends Command {
6161
}
6262

6363
async run(source: CommandSource, name?: string | null): Promise<SendMessage | undefined> {
64-
const { commands } = client
64+
const commands = [...client.commands.values()]
6565
if (!name) {
6666
return this.sendCommands(source)
6767
}

src/events/ready.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export async function handle(): Promise<void> {
1818
client.newsManager.fetchNews().catch(e => Logger.error(e))
1919

2020
if (!client.application?.owner) await client.application?.fetch()
21-
const cmds = client.commands.array()
21+
const cmds = [...client.commands.values()]
2222

2323
try {
2424
if (config.production) {

src/utils/CommandHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function getCommand(command: string): ParsedCommand | false {
1818

1919
// If that command doesn't exist, try to find an alias
2020
if (!cmd) {
21-
cmd = client.commands.find((cmd: Command) => cmd.aliases.includes(command))
21+
cmd = [...client.commands.values()].find((cmd: Command) => cmd.aliases.includes(command))
2222

2323
// If that command doesn't exist, silently exit and do nothing
2424
if (!cmd)

src/utils/Utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,24 @@ function limitIndex(words: string[], maxLength = 50): number {
107107
return end
108108
}
109109

110+
export function splitByLength(text: string, maxLength: number, splitOn: string): string[] {
111+
const splitted = text.split(splitOn)
112+
const result: string[] = []
113+
let currentLine = ""
114+
115+
for (const line of splitted) {
116+
if (currentLine.length + line.length >= maxLength) {
117+
result.push(currentLine)
118+
currentLine = line
119+
} else
120+
currentLine += splitOn + line
121+
}
122+
if (currentLine.length > 0)
123+
result.push(currentLine)
124+
125+
return result
126+
}
127+
110128
export function truncate(text: string, maxLength = 50): string {
111129
const words = text.split(" ")
112130
const end = limitIndex(words, maxLength)

0 commit comments

Comments
 (0)