Skip to content

Commit

Permalink
honor moment
Browse files Browse the repository at this point in the history
  • Loading branch information
XboxBedrock committed Mar 24, 2024
1 parent 737dab8 commit 37466a2
Show file tree
Hide file tree
Showing 11 changed files with 299 additions and 111 deletions.
12 changes: 7 additions & 5 deletions config/extensions/messages/messages_en.json5
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
slowmodeTooLow: [
"How would that even work? Time travel?",
"Dont go breaking the space time continuum.",
"Time travel is not possible. Try a larger number.",
"At this point, you know what you are doing wrong."
"Time travel is not possible. Try a larger number."
],
slowmodeTooHigh: [
"That is a lot of seconds.",
Expand Down Expand Up @@ -231,8 +230,8 @@
(this also applies to quoted arguments)."
],
removedUser: ["Removed %1s."],
honoredUserExisting: ["Honored %1s for 3 months (it was going to end in %2s)."],
honoredUserNew: ["Honored %1s for 3 months."],
honoredUserExisting: ["Honored %1s for %2s months (it was going to end in %3s)."],
honoredUserNew: ["Honored %1s for %2s months."],
honoredProgressMessage: [
"%1s has been awarded for their cool build! Congratulations and great work!"
],
Expand Down Expand Up @@ -295,5 +294,8 @@
rrAddR: ["Added required role!"],
rrRemR: ["Removed required role!"],
rrNoExist: ["The requested role is not in the blacklist/whitelist!"],
dmBased: ["Error: That channel is in DM based"]
dmBased: ["Error: That channel is in DM based"],
zeroDuration: ["You're achieving nothing, bozo"],
honorDNE: ["This builder has not been honored!"],
validUntilHonor: ["The honor is now valid until %1s"]
}
311 changes: 233 additions & 78 deletions src/commands/honor.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import Command from "../struct/Command.js"
import GuildMember from "../struct/discord/GuildMember.js"
import Guild from "../struct/discord/Guild.js"

import { humanizeArray, ms, noop } from "@buildtheearth/bot-utils"
import {
formatTimestamp,
humanizeArray,
humanizeConstant,
ms,
noop
} from "@buildtheearth/bot-utils"
import AdvancedBuilder from "../entities/AdvancedBuilder.entity.js"
import CommandMessage from "../struct/CommandMessage.js"
import Discord from "discord.js"
Expand All @@ -13,46 +19,94 @@ export default new Command({
name: "honor",
aliases: ["advance"],
description: "Add or remove a user as an honored builder.",
inheritGlobalArgs: true,
permission: [
globalThis.client.roles.BUILDER_COUNCIL,
globalThis.client.roles.MANAGER
],
args: [
{
name: "user",
description: "User to advance.",
description: "User to honor.",
required: true,
optionType: "USER"
},
}
],

subcommands: [
{
name: "type",
description: "The type of special builder role!",
required: true,
optionType: "STRING",
choices: ["advanced", "cool_build"]
name: "add",
description: "Honor a new builder",
args: [
{
name: "duration",
description: "The duration of honor (in months)",
optionType: "NUMBER",
required: true
},
{
name: "type",
description: "The type of special builder role!",
required: true,
optionType: "STRING",
choices: ["advanced", "cool_build"]
}
]
},
{
name: "demote",
description: "Optional arg to demote advanced builder",
required: false,
optionType: "STRING",
choices: ["remove"]
description: "Demote a honored builder",
args: []
},
{
name: "extend",
description: "Extend the duration for an honored builder",
args: [
{
name: "duration",
description: "The duration to extend by (in months)",
optionType: "NUMBER",
required: true
},
{
name: "type",
description: "The type of special builder role!",
required: true,
optionType: "STRING",
choices: ["advanced", "cool_build"]
}
]
},
{
name: "lower",
description: "Lower the duration for an honored builder",
args: [
{
name: "duration",
description: "The duration to lower by (in months)",
optionType: "NUMBER",
required: true
},
{
name: "type",
description: "The type of special builder role!",
required: true,
optionType: "STRING",
choices: ["advanced", "cool_build"]
}
]
},
{
name: "info",
description: "Give info about a honored builder",
args: []
}
],

async run(client: Client, message: CommandMessage, args: Args) {
const subcommand = args.consumeSubcommand()
const user = await args.consumeUser("user")
const type = args.consumeIf(["advanced", "cool_build"], "type")
if (!type) {
return message.sendErrorMessage(
"chooseBuilder",
humanizeArray(["advanced", "cool_build"], true, "or")
)
}

const roleName =
type.toLowerCase() === "cool_build" ? "COOL_BUILD" : "ADVANCED_BUILDER"

const remove = !!args.consumeIf("remove", "demote")
if (!user)
return message.sendErrorMessage(user === undefined ? "noUser" : "invalidUser")

Expand All @@ -64,80 +118,181 @@ export default new Command({
!GuildMember.hasRole(member, globalThis.client.roles.BUILDER, client)
)
return message.sendErrorMessage("noBuilder")
const role = Guild.role(await client.customGuilds.main(), client.roles[roleName])

await message.continue()

if (remove) {
const record = await AdvancedBuilder.findOne(user.id, {
where: { roleName: roleName }
})
if (subcommand == "info") {
const record = await AdvancedBuilder.findOne(user.id)

if (!record) return message.sendErrorMessage("notAdvancedBuilder")
else {
const expire = new Date(record.givenAt.getTime())

expire.setMonth(record.givenAt.getMonth() + record.duration)

return message.sendSuccess({
title: "Honor Info",
thumbnail: {
url:
member.displayAvatarURL({
size: 128,
extension: "png",
forceStatic: false
}) || "https://cdn.discordapp.com/embed/avatars/5.png"
},

description: `Honor info for <@${member.id}>`,

fields: [
{
name: "Role",
value: humanizeConstant(record.roleName)
},
{
name: "Expiry Date",
value: formatTimestamp(expire)
}
]
})
}
}

if (subcommand == "demote") {
const record = await AdvancedBuilder.findOne(user.id)
if (!record) return message.sendErrorMessage("notAdvancedBuilder")
await record.removeBuilder(client)
return message.sendSuccessMessage("removedUser", user)
} else {
const type = args.consumeIf(["advanced", "cool_build"], "type")
if (!type) {
return message.sendErrorMessage(
"chooseBuilder",
humanizeArray(["advanced", "cool_build"], true, "or")
)
}

const roleName =
type.toLowerCase() === "cool_build" ? "COOL_BUILD" : "ADVANCED_BUILDER"

const role = Guild.role(
await client.customGuilds.main(),
client.roles[roleName]
)

const durationReal = args.consumeNumber("duration")

console.log(durationReal)
let duration = durationReal

if (!duration || duration < 0) duration = 3

const existingRecord = await AdvancedBuilder.findOne(user.id)
if (existingRecord) {
const oldTime = existingRecord.givenAt
existingRecord.givenAt = new Date()
await existingRecord.save()
existingRecord.schedule(client)

oldTime.setMonth(oldTime.getMonth() + 3)
const formattedTime = ms(oldTime.getTime() - Date.now(), { long: true })
if (subcommand == "add") {
if (existingRecord) {
const oldTime = existingRecord.givenAt
existingRecord.givenAt = new Date()
existingRecord.duration = duration
await existingRecord.save()
existingRecord.schedule(client)

return message.sendSuccessMessage(
"honoredUserExisting",
user,
formattedTime
)
} else {
const record = new AdvancedBuilder()
record.builder = user.id
record.roleName = roleName as "COOL_BUILD" | "ADVANCED_BUILDER"
await record.save()
record.schedule(client)
await member.roles.add(role)

if (client.roles[roleName] === globalThis.client.roles.COOL_BUILD) {
let progressChannel = client.customGuilds
.main()
.channels.cache.find(
ch => ch.name == "builder-chat"
) as Discord.TextChannel
if (!progressChannel) {
progressChannel = client.customGuilds
oldTime.setMonth(oldTime.getMonth() + existingRecord.duration)
const formattedTime = ms(oldTime.getTime() - Date.now(), {
long: true
})

return message.sendSuccessMessage(
"honoredUserExisting",
user,
duration,
formattedTime
)
} else {
const record = new AdvancedBuilder()
record.builder = user.id
record.duration = duration
record.roleName = roleName as "COOL_BUILD" | "ADVANCED_BUILDER"
await record.save()
record.schedule(client)
await member.roles.add(role)

if (client.roles[roleName] === globalThis.client.roles.COOL_BUILD) {
let progressChannel = client.customGuilds
.main()
.channels.cache.find(
ch => ch.name == "builders-chat"
ch => ch.name == "builder-chat"
) as Discord.TextChannel
if (!progressChannel) {
progressChannel = client.customGuilds
.main()
.channels.cache.find(
ch => ch.name == "builders-chat"
) as Discord.TextChannel
}
if (progressChannel) {
await client.response.sendSuccess(
progressChannel,
message.getMessage("honoredProgressMessage", user)
)
}
return message.sendSuccessMessage(
"honoredUserNew",
user,
duration
)
}
if (progressChannel) {
await client.response.sendSuccess(
progressChannel,
message.getMessage("honoredProgressMessage", user)

if (
client.roles[roleName] ===
globalThis.client.roles.ADVANCED_BUILDER
) {
await user
.createDM()
.then(dms =>
dms.send({
embeds: [
{
color: role.color,
description:
"You have been added as an advanced builder. Good job!"
}
]
})
)
.catch(noop)
return message.sendSuccessMessage(
"honoredUserNew",
user,
duration
)
}
return message.sendSuccessMessage("honoredUserNew", user)
}
} else {
if (!durationReal || durationReal == 0)
return message.sendErrorMessage("zeroDuration")

if (client.roles[roleName] === globalThis.client.roles.ADVANCED_BUILDER) {
await user
.createDM()
.then(dms =>
dms.send({
embeds: [
{
color: role.color,
description:
"You have been added as an advanced builder. Good job!"
}
]
})
)
.catch(noop)
return message.sendSuccessMessage("honoredUserNew", user)
let newDuration = Math.abs(durationReal)

if (subcommand == "lower") {
newDuration = -newDuration
}

if (!existingRecord) return message.sendErrorMessage("honorDNE")

const res = existingRecord.extend(client, newDuration)

if (!res) return message.sendErrorMessage("slowmodeTooLow")

const expire = new Date(existingRecord.givenAt.getTime())

expire.setMonth(
existingRecord.givenAt.getMonth() + existingRecord.duration
)

return message.sendSuccessMessage(
"validUntilHonor",
formatTimestamp(expire)
)
}
}
}
Expand Down
Loading

0 comments on commit 37466a2

Please sign in to comment.