Spikers
Spikers
Explore posts from servers
DIAdiscord.js - Imagine an app
Created by Spikers on 10/26/2023 in #djs-questions
Upgrading from djs v12 (commando) to djs v14 (and sapphire framework) - Unsure on how to Parse Types
Will leave this up in case someone from here is able to help, but yes I understand the push for slash commands.
9 replies
DIAdiscord.js - Imagine an app
Created by Spikers on 10/26/2023 in #djs-questions
Upgrading from djs v12 (commando) to djs v14 (and sapphire framework) - Unsure on how to Parse Types
I appreciate it, I'll go there now👍
9 replies
DIAdiscord.js - Imagine an app
Created by Spikers on 10/26/2023 in #djs-questions
Upgrading from djs v12 (commando) to djs v14 (and sapphire framework) - Unsure on how to Parse Types
I joined their server as well, will dive into that if necessary, but I was just wondering if there was a vanilla djs v14 way of doing such (as it was just a helper function)!
9 replies
DIAdiscord.js - Imagine an app
Created by Spikers on 10/26/2023 in #djs-questions
Upgrading from djs v12 (commando) to djs v14 (and sapphire framework) - Unsure on how to Parse Types
My Sapphire.js styled ConfigCommand:
import { Message } from "discord.js"
import { Args, PieceContext } from "@sapphire/framework"
import { ConfigurableCouncilData, ConfigurableCouncilDataSerializers, OptionalCouncilData } from "../../CouncilData"
import { getDefaultValue, getProps, parseType, response, ResponseType } from "../../Util"
import ICommand from "../ICommand"

const makeDisplay = (displayer?: (value: any) => string) => (value: any) => {
if (value === undefined || value === null) {
return "None"
} else if (displayer) {
return displayer(value)
} else {
return value.toString()
}
}

export default class ConfigCommand extends ICommand {
constructor(client: PieceContext) {
super(client, {
name: "config",
aliases: ["votumconfig", "cfg", "vconfig", "vcfg", "councilconfig"],
description: "Designates a specific role for councilors.",
adminOnly: true,
})
}

async execute(msg: Message, args: Args): Promise<Message | Message[]> {
const argsKey = args.next();
const argsValue = args.next();
if (argsKey == null || argsKey.length === 0) {
return msg.reply({
embeds: [response(
ResponseType.Neutral,
`Available configuration points are:\n${Object.keys(
getProps(ConfigurableCouncilData),
)
.map((n) => `~${n}~`)
.join(",\n ")}.`,
).embed],
})
}

const key = argsKey.replace(/\.([a-z0-9])/g, (_, l) =>
l.toUpperCase(),
) as keyof ConfigurableCouncilData

if (!(key in getProps(ConfigurableCouncilData))) {
return msg.reply({
embeds: [response(
ResponseType.Bad,
`:x: \`${key}\` is not a valid configuration point.`,
).embed],
})
}

const serializer = ConfigurableCouncilDataSerializers[key]
const display = makeDisplay(serializer.display)

if (argsValue == null || argsValue.length === 0) {
return msg.reply({
embeds: [response(
ResponseType.Neutral,
`Configuration point ${argsKey} is currently set to ~${display(
this.council.getConfig(key),
)}~.`,
).embed],
})
}

if (argsValue === "$remove" && key in getProps(OptionalCouncilData)) {
this.council.setConfig(key, getDefaultValue(key, OptionalCouncilData))
return msg.reply({
embeds: [response(
ResponseType.Neutral,
`Set configuration point ~${key}~ back to default state.`,
).embed],
})
}

// Line & function in question
const value = await parseType(this.client, msg, argsValue, serializer)
console.log(value)

if (value !== null) {
const serializedValue = serializer.serialize(value)

this.council.setConfig(key, serializedValue)

return msg.reply({
embeds: [response(
ResponseType.Good,
`Set configuration point ~${key}~ to ${display(value)}`,
).embed],
})
} else {
return msg.reply({
embeds: [response(
ResponseType.Bad,
`~${argsValue}~ is not a valid **${serializer.type}**`,
).embed],
})
}
}
}
import { Message } from "discord.js"
import { Args, PieceContext } from "@sapphire/framework"
import { ConfigurableCouncilData, ConfigurableCouncilDataSerializers, OptionalCouncilData } from "../../CouncilData"
import { getDefaultValue, getProps, parseType, response, ResponseType } from "../../Util"
import ICommand from "../ICommand"

const makeDisplay = (displayer?: (value: any) => string) => (value: any) => {
if (value === undefined || value === null) {
return "None"
} else if (displayer) {
return displayer(value)
} else {
return value.toString()
}
}

export default class ConfigCommand extends ICommand {
constructor(client: PieceContext) {
super(client, {
name: "config",
aliases: ["votumconfig", "cfg", "vconfig", "vcfg", "councilconfig"],
description: "Designates a specific role for councilors.",
adminOnly: true,
})
}

async execute(msg: Message, args: Args): Promise<Message | Message[]> {
const argsKey = args.next();
const argsValue = args.next();
if (argsKey == null || argsKey.length === 0) {
return msg.reply({
embeds: [response(
ResponseType.Neutral,
`Available configuration points are:\n${Object.keys(
getProps(ConfigurableCouncilData),
)
.map((n) => `~${n}~`)
.join(",\n ")}.`,
).embed],
})
}

const key = argsKey.replace(/\.([a-z0-9])/g, (_, l) =>
l.toUpperCase(),
) as keyof ConfigurableCouncilData

if (!(key in getProps(ConfigurableCouncilData))) {
return msg.reply({
embeds: [response(
ResponseType.Bad,
`:x: \`${key}\` is not a valid configuration point.`,
).embed],
})
}

const serializer = ConfigurableCouncilDataSerializers[key]
const display = makeDisplay(serializer.display)

if (argsValue == null || argsValue.length === 0) {
return msg.reply({
embeds: [response(
ResponseType.Neutral,
`Configuration point ${argsKey} is currently set to ~${display(
this.council.getConfig(key),
)}~.`,
).embed],
})
}

if (argsValue === "$remove" && key in getProps(OptionalCouncilData)) {
this.council.setConfig(key, getDefaultValue(key, OptionalCouncilData))
return msg.reply({
embeds: [response(
ResponseType.Neutral,
`Set configuration point ~${key}~ back to default state.`,
).embed],
})
}

// Line & function in question
const value = await parseType(this.client, msg, argsValue, serializer)
console.log(value)

if (value !== null) {
const serializedValue = serializer.serialize(value)

this.council.setConfig(key, serializedValue)

return msg.reply({
embeds: [response(
ResponseType.Good,
`Set configuration point ~${key}~ to ${display(value)}`,
).embed],
})
} else {
return msg.reply({
embeds: [response(
ResponseType.Bad,
`~${argsValue}~ is not a valid **${serializer.type}**`,
).embed],
})
}
}
}
9 replies