Skip to content

Commit

Permalink
refactor: add comments for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkGuy10 committed May 28, 2022
1 parent 69fec72 commit b2efcac
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ const client = new NukecordClient({
})

// ================================= Piece Functions
const getToken = async () => {
/**
* Get token from either environment variable or user input.
*/
const getToken = async (): Promise<string> => {
if (process.env.NUKECORD_SELFBOT_TOKEN) {
Log.info(`Token found from env: ${chalk.bold(process.env.NUKECORD_SELFBOT_TOKEN)}`)
const { _useFromEnv } = await prompts(
Expand Down Expand Up @@ -65,15 +68,18 @@ const getToken = async () => {

// ================================= Error Handling
process.on('uncaughtException', (error, origin) => {
// All errors are fatal and the script exits with code 1
Log.error(error)
process.exit(1)
})

// ================================= Start Of Script
// Welcome human
Log.banner()
token = await getToken()
_spinner = spinner('Attempting Login...').start()

// Try login
try {
await client.login(token)
_spinner.text = 'Building client cache...'
Expand All @@ -83,6 +89,7 @@ try {
throw error
}

// Wait for client to get ready
await client.getReady()
_spinner.stop()
Log.success(
Expand All @@ -91,8 +98,10 @@ Log.success(
)} ${chalk.gray.dim(`[ID: ${client.user?.id}]`)}`
)

// Sassy sleep statement
await sleep(500)

// Select target guild
Log.info(`Retrieved ${chalk.bold(`${client.guilds.cache.size} guilds`)}`)

const { target }: { target: Guild } = await prompts(
Expand All @@ -112,15 +121,18 @@ const { target }: { target: Guild } = await prompts(
{ onCancel }
)

// Filter raids by available permissions
const permittedRaids = raidModules.filter(
raid => target.me?.permissions.has(raid.permission) ?? false
)

// The client doesn't have sufficient permission for executing any raid
if (!permittedRaids.size) {
Log.warn("Client doesn't have permissions for executing any raid")
process.exit(0)
}

// Ask user to select atleast 1 raid module out of the permitted ones
const { selectedRaids }: { selectedRaids: Collection<RaidName, Raid> } = await prompts(
{
type: 'multiselect',
Expand All @@ -137,8 +149,10 @@ const { selectedRaids }: { selectedRaids: Collection<RaidName, Raid> } = await p
{ onCancel }
)

// Warning, because people may second guess their decisions
Log.warn(`Think twice before moving ahead, violence isn't always the answer (but sometimes it is)`)

// Are you really really sure?
const { _confirmRaid } = await prompts(
{
type: 'confirm',
Expand All @@ -151,10 +165,13 @@ const { _confirmRaid } = await prompts(
{ onCancel }
)

// That's an abort innit
if (!_confirmRaid) throw userAbortError

// Execute the selected raids
for (const raid of selectedRaids.values()) await raid.execute(target)

// We're done!
Log.success('Raid completed, exitting script.')
process.exit(0)

Expand Down

0 comments on commit b2efcac

Please sign in to comment.