Skip to content

Commit

Permalink
cur null check
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen3H committed May 1, 2024
1 parent b803781 commit ea6fc9d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 28 deletions.
10 changes: 6 additions & 4 deletions aurora/commands/alliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,15 +459,17 @@ export default {
]}).then(m => setTimeout(() => m.delete(), 10000)).catch(() => {})

const formattedArgs = fn.argsHelper(args, 2)
const nationsToRemove = formattedArgs.asArray()
const nationsToRemove = formattedArgs.asArray() as any[]
const allianceIndex = alliances.findIndex(a => a.allianceName.toLowerCase() == allianceName.toLowerCase())

if (!nationsToRemove) {
console.log(`Failed to remove nations from ${getName(foundAlliance)}`)
return
}

console.log(nationsToRemove.toString())
const len = nationsToRemove.length

for (let i = 0; i < len; i++) {
const cur = nationsToRemove[i]

Expand All @@ -484,9 +486,9 @@ export default {
})
]}).then(m => setTimeout(() => m.delete(), 10000)).catch(() => {})
}
const foundAllianceNations = foundAlliance.nations
const lower = cur.toLowerCase()

const foundAllianceNations = foundAlliance.nations as any[]
const lower = cur?.toLowerCase()

const foundNation = foundAllianceNations.find(nation => nation.toLowerCase() == lower)
const foundNationIndex = foundAllianceNations.findIndex(nation => nation.toLowerCase() == lower)
Expand Down
11 changes: 7 additions & 4 deletions aurora/slashcommands/unsubscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import {

import * as fn from '../../bot/utils/fn.js'

import admin from "firebase-admin"
const FieldValue = admin.firestore.FieldValue
import {
getFirestore, FieldValue, DocumentReference
} from "firebase-admin/firestore"

async function unsub(
subbedChannels: admin.firestore.DocumentReference,
subbedChannels: DocumentReference,
interaction: ChatInputCommandInteraction,
subTypeName: string
) {
Expand Down Expand Up @@ -88,7 +89,9 @@ export default {
invalidUsage.setFooter(fn.devsFooter(client))
], ephemeral: true})

const subsCollection = admin.firestore().collection("subs")
const db = getFirestore()

const subsCollection = db.collection("subs")
unsub(subsCollection.doc(comparator), interaction, comparator)
},
data: new SlashCommandBuilder().setName("unsubscribe")
Expand Down
44 changes: 25 additions & 19 deletions bot/objects/AllianceModal.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import Discord from 'discord.js'
import {
type CommandInteractionOptionResolver,
type ModalSubmitInteraction,
type ChatInputCommandInteraction,
ModalBuilder, ActionRowBuilder,
TextInputBuilder, TextInputStyle
} from 'discord.js'

import cache from 'memory-cache'

const ParagraphStyle = Discord.TextInputStyle.Paragraph
const ParagraphStyle = TextInputStyle.Paragraph
const optionals = ['flag', 'discord', 'fill', 'outline', 'full_name']
const optLower = (opts: Discord.CommandInteractionOptionResolver, k: string) =>
const optLower = (opts: CommandInteractionOptionResolver, k: string) =>
opts.getString(k).toLowerCase()

const fieldVal = (interaction: Discord.ModalSubmitInteraction, name: string) =>
const fieldVal = (interaction: ModalSubmitInteraction, name: string) =>
interaction.fields.getField(name).value

type Alliance = {
Expand All @@ -24,23 +31,22 @@ type Alliance = {
}
}

class AllianceModal extends Discord.ModalBuilder {
class AllianceModal extends ModalBuilder {
rows = []
required = false
alliance: Alliance = null

constructor(id, title, alliance = null) {
constructor(id: string, title: string, alliance = null) {
super({ customId: id, title, components: [] })

this.alliance = alliance
this.required = !alliance ? true : false
}

createRow = (id, label, placeholder = null, style = Discord.TextInputStyle.Short) => {
if (optionals.includes(id)) this.required = false
else this.required = true
createRow = (id: string, label: string, placeholder: string = null, style = TextInputStyle.Short) => {
this.required = !optionals.includes(id)

const comp = new Discord.TextInputBuilder()
const comp = new TextInputBuilder()
.setCustomId(id)
.setLabel(label)
.setStyle(style)
Expand All @@ -51,10 +57,10 @@ class AllianceModal extends Discord.ModalBuilder {
comp.setPlaceholder(str)
}

return new Discord.ActionRowBuilder().addComponents(comp)
return new ActionRowBuilder().addComponents(comp)
}

show = (interaction: Discord.ChatInputCommandInteraction) => {
show = (interaction: ChatInputCommandInteraction) => {
console.log(`Showing creation wizard for ${this.alliance.name}`)

const key = interaction.member.user.id
Expand All @@ -64,10 +70,10 @@ class AllianceModal extends Discord.ModalBuilder {
return interaction.showModal(this)
}

main = (options: Discord.CommandInteractionOptionResolver) => {
main = (options: CommandInteractionOptionResolver) => {
this.rows = [
this.createRow('nations', 'List of Nations', this.alliance?.nations, ParagraphStyle),
this.createRow('leaders', 'Leader(s)', this.alliance?.leaders, ParagraphStyle),
this.createRow('nations', 'List of Nations', this.alliance?.nations.join(", "), ParagraphStyle),
this.createRow('leaders', 'Leader(s)', this.alliance?.leaders.join(", "), ParagraphStyle),
this.createRow('flag', 'Flag Image Link (Optional)', this.alliance?.imageURL),
this.createRow('discord', 'Discord Invite Link (Optional)', this.alliance?.discordInvite),
this.createRow('full_name', 'Full Name (Optional)', this.alliance?.fullName)
Expand All @@ -79,17 +85,17 @@ class AllianceModal extends Discord.ModalBuilder {
return this
}

asObject = (interaction: Discord.ModalSubmitInteraction ) => {
asObject = (interaction: ModalSubmitInteraction ) => {
const obj: any = {
mapName: this.alliance.map,
allianceName: this.alliance.name,
nations: fieldVal(interaction, 'nations').replaceAll(' ', '').split(','),
leaders: fieldVal(interaction, 'leaders')
}

const fullName = fieldVal(interaction, 'full_name'),
discord = fieldVal(interaction, 'discord'),
flag = fieldVal(interaction, 'flag')
const fullName = fieldVal(interaction, 'full_name')
const discord = fieldVal(interaction, 'discord')
const flag = fieldVal(interaction, 'flag')

if (fullName) obj.fullName = fullName
if (discord) obj.discord = discord
Expand Down
2 changes: 1 addition & 1 deletion bot/utils/aurora.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ async function getAlliance(name: string) {
return foundAlliance
}

async function getAlliances(skipCache = false) {
async function getAlliances(skipCache = false): Promise<any[]> {
const cached = cache.get('aurora_alliances')
const skip = !skipCache ? cached : null

Expand Down

0 comments on commit ea6fc9d

Please sign in to comment.