Skip to content

Commit

Permalink
qol
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen3H committed Jul 20, 2024
1 parent 1023492 commit 77f96d6
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 78 deletions.
69 changes: 37 additions & 32 deletions aurora/commands/online.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,68 @@ import {
import { Aurora } from "earthmc"
import { CustomEmbed } from '../../bot/objects/CustomEmbed.js'

import * as fn from '../../bot/utils/fn.js'
import {
fetchError,
staff, sortByKey,
devsFooter
} from '../../bot/utils/fn.js'

const embed = (client: Client, msg: Message) => new EmbedBuilder()
.setColor(0x556b2f)
.setAuthor({ name: msg.author.username, iconURL: msg.author.displayAvatarURL() })
.setTimestamp()
.setFooter(fn.devsFooter(client))
.setFooter(devsFooter(client))

const pageFromArgs = (args: string[], page = 1) => {
if (args[0]) page = parseInt(args[0])
if (isNaN(page)) page = 0
else page--

return page
}

export default {
name: "online",
slashCommand: true,
run: async (client: Client, message: Message, args: string[]) => {
const req = args.join(" ")
const m = await message.reply({embeds: [new EmbedBuilder()
.setTitle("<a:loading:966778243615191110> Fetching activity data, this might take a moment.")
.setColor(0x556b2f)]
})

if (!req) return m.edit({embeds: [new EmbedBuilder()
if (!req) return message.reply({embeds: [new EmbedBuilder()
.setColor(Colors.Red)
.setTitle("No Arguments Given")
.setDescription("Arguments: `all`, `staff`/`mods`, `mayors`, `kings`")
]}).then((m => setTimeout(() => m.delete(), 10000))).catch(() => {})

const m = await message.reply({embeds: [new EmbedBuilder()
.setTitle("<a:loading:966778243615191110> Fetching activity data, this might take a moment.")
.setColor(0x556b2f)]
})

const onlinePlayers = await Aurora.Players.online().catch(err => console.log(err))
if (!onlinePlayers) return await m.edit({embeds: [fn.fetchError]}).then((m => setTimeout(() => m.delete(), 10000))).catch(() => {})
if (!onlinePlayers) return await m.edit({ embeds: [fetchError] })
.then((m => setTimeout(() => m.delete(), 10000))).catch(() => {})

switch(args[0].toLowerCase()) {
case "all": {
// Alphabetical sort
fn.sortByKey(onlinePlayers, 'name')
sortByKey(onlinePlayers, 'name')

let page = 1
if (isNaN(page)) page = 0
else page--
// let page = 1
// if (isNaN(page)) page = 0
// else page--

const allData = onlinePlayers.map(
op => op.name != op.nickname ? `${op.name} (${op.nickname})` : op.name
).join('\n').match(/(?:^.*$\n?){1,20}/mg)

return await new CustomEmbed(client, "(Aurora) Online Activity | All")
.setPage(page)
.setPage(1)
.setColor(0x556b2f)
.paginate(allData, "```", "```")
.editMessage(m)
}
case "staff":
case "mods": {
const onlineStaff = fn.staff.all().filter(sm => onlinePlayers.find(op => op.name.toLowerCase() == sm.toLowerCase()))
const onlineStaff = staff.all().filter(sm => onlinePlayers.find(op => op.name.toLowerCase() == sm.toLowerCase()))
return m.edit({embeds: [
embed(client, message)
.setTitle("(Aurora) Online Activity | Staff")
Expand All @@ -67,36 +80,28 @@ export default {
arr.filter(t => onlinePlayers.find(op => op.name == t.mayor)))

if (!towns) return
fn.sortByKey(towns, 'mayor')

let page = 1
if (req.split(" ")[0]) page = parseInt(req.split(" ")[0])
if (isNaN(page)) page = 0
else page--
sortByKey(towns, 'mayor')

const allData = towns.map(town => `${town.mayor} (${town.name})`).join('\n').match(/(?:^.*$\n?){1,20}/mg)
return await new CustomEmbed(client, "(Aurora) Online Activity | Mayors")
.setPage(page)
.setPage(pageFromArgs(req.split(" ")))
.setColor(0x556b2f)
.paginate(allData, `Total: ${towns.length}$` + "```", "```")
.editMessage(m)
}
case "kings": {
const allNations = await Aurora.Nations.all().catch(err => console.log(err))
if (!allNations || allNations.length < 1)
return await m.edit({embeds: [fn.fetchError]}).then((m => setTimeout(() => m.delete(), 10000))).catch(() => {})
if (!allNations || allNations.length < 1) {
return await m.edit({ embeds: [fetchError] })
.then((m => setTimeout(() => m.delete(), 10000))).catch(() => {})
}

const nations = allNations.filter(n => onlinePlayers.find(op => op.name == n.king))
fn.sortByKey(nations, 'king')

let page = 1
if (req.split(" ")[0]) page = parseInt(req.split(" ")[0])
if (isNaN(page)) page = 0
else page--
const nations = allNations.filter(n => onlinePlayers.some(op => op.name == n.king))
sortByKey(nations, 'king')

const allData = nations.map(nation => `${nation.king} (${nation.name})`).join('\n').match(/(?:^.*$\n?){1,20}/mg)
return await new CustomEmbed(client, "(Aurora) Online Activity | Kings")
.setPage(page)
.setPage(pageFromArgs(req.split(" ")))
.setColor(0x556b2f)
.paginate(allData, `Total: ${nations.length}` + "```", "```")
.editMessage(m)
Expand Down
10 changes: 5 additions & 5 deletions aurora/commands/town.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import type { DBNation, DBSquaremapTown, TownDataItem } from "../../bot/types.js
const invalidUsageEmbed = () => new EmbedBuilder()
.setColor(Colors.Red)
.setTitle("Invalid Arguments")
.setDescription(
"**Command Usage**:\n" +
"Get info on a single town - `/town <name>`\n" +
"Show a page-by-page display of all towns - `/town list`\n"
)
.setDescription(`
**Command Usage**:
Get info on a single town - \`/town <name>\`
Show a page-by-page display of all towns - \`/town list\`
`)

export default {
name: "town",
Expand Down
5 changes: 3 additions & 2 deletions aurora/slashcommands/alliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
} from "discord.js"

import * as database from '../../bot/utils/database.js'
import * as fn from '../../bot/utils/fn.js'
import { botDevs } from '../../bot/utils/fn.js'

import AllianceModal from '../../bot/objects/AllianceModal.js'
import type { SlashCommand } from "../../bot/types.js"

Expand All @@ -18,7 +19,7 @@ const checkEditor = async (interaction: ChatInputCommandInteraction) => {
const isEditor = editingChannels.includes(interaction.channelId) &&
author.roles.cache.has('966359842417705020')

if (!fn.botDevs.includes(author.id) && !isEditor) {
if (!botDevs.includes(author.id) && !isEditor) {
return interaction.reply({embeds: [new EmbedBuilder()
.setTitle("That command is for editors only!\nIf you are an editor, you're probably in the wrong channel.")
.setAuthor({ name: author.user.username, iconURL: author.displayAvatarURL() })
Expand Down
4 changes: 2 additions & 2 deletions aurora/slashcommands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "discord.js"

//import { Service } from 'koyeb.js'
import * as fn from '../../bot/utils/fn.js'
import { botDevs } from '../../bot/utils/fn.js'

import dotenv from 'dotenv'
dotenv.config()
Expand All @@ -24,7 +24,7 @@ export default {

const member = interaction.member as GuildMember

if (!fn.botDevs.includes(member.id)) {
if (!botDevs.includes(member.id)) {
try {
const m = await interaction.reply({embeds: [
embed.setTitle("Goofy ah :skull:")
Expand Down
6 changes: 3 additions & 3 deletions aurora/slashcommands/nether.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Colors, EmbedBuilder, SlashCommandBuilder
} from 'discord.js'

import * as fn from '../../bot/utils/fn.js'
import { devsFooter } from '../../bot/utils/fn.js'

const formatCoord = (coord: number) => coord.toString().replace(/[, ]/g, " ")
const convertToOverworld = (coord: number) => Math.floor(coord / 8).toString()
Expand All @@ -17,7 +17,7 @@ export default {
.setDescription("<:red_tick:1036290475012915270> Please enter 2 numerical arguments, divided by a space or comma.")
.setColor(Colors.Red)
.setTimestamp()
.setFooter(fn.devsFooter(client))
.setFooter(devsFooter(client))

const x = interaction.options.getInteger("x"),
z = interaction.options.getInteger("z")
Expand All @@ -37,7 +37,7 @@ export default {
.setColor(Colors.Green)
.setDescription(`\n${convertToOverworld(arg0)}, ${convertToOverworld(arg1)}`)
.setTimestamp()
.setFooter(fn.devsFooter(client))
.setFooter(devsFooter(client))
]})
}, data: new SlashCommandBuilder()
.setName("nether")
Expand Down
25 changes: 14 additions & 11 deletions aurora/slashcommands/online.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ import {
Colors
} from 'discord.js'

import { Aurora, type Player } from "earthmc"
import { CustomEmbed } from '../../bot/objects/CustomEmbed.js'

import * as fn from '../../bot/utils/fn.js'
import { Aurora, type Player } from "earthmc"
import {
devsFooter, fetchError, sortByKey,
staff
} from '../../bot/utils/fn.js'

const EMBED_COLOUR = "#d67a82"

function displayOnlineStaff(client: Client, interaction: ChatInputCommandInteraction, ops: Player[]) {
const onlineStaff = fn.staff.all().filter(sm => ops.find(op => op.name.toLowerCase() == sm.toLowerCase()))
const onlineStaff = staff.all().filter(sm => ops.find(op => op.name.toLowerCase() == sm.toLowerCase()))
return interaction.editReply({embeds: [new EmbedBuilder()
.setTitle("Online Activity | Staff")
.setDescription(onlineStaff.length >= 1 ? "```" + onlineStaff.join(", ").toString() + "```" : "No staff are online right now! Try again later.")
.setColor(EMBED_COLOUR)
.setTimestamp()
.setFooter(fn.devsFooter(client))
.setFooter(devsFooter(client))
]})
}

Expand All @@ -31,12 +34,12 @@ export default {
await interaction.deferReply()

const ops: Player[] | null = await Aurora.Players.online().catch(() => null)
if (!ops) return interaction.editReply({ embeds: [fn.fetchError] /*ephemeral: true */ })
if (!ops) return interaction.editReply({ embeds: [fetchError] /*ephemeral: true */ })

switch(interaction.options.getSubcommand().toLowerCase()) {
case "all": {
// Alphabetical sort
fn.sortByKey(ops, 'name')
sortByKey(ops, 'name')

const allData = ops
.map(op => op.name === op.nickname ? op.name : `${op.name} (${op.nickname})`)
Expand All @@ -55,12 +58,12 @@ export default {
case "mayors": {
const allTowns = await Aurora.Towns.all().catch(() => {})
if (!allTowns || allTowns.length < 1) return await interaction.editReply({
embeds: [fn.fetchError]
embeds: [fetchError]
//ephemeral: true
})

const towns = allTowns.filter(t => ops.find(op => op.name == t.mayor))
fn.sortByKey(towns, 'mayor')
sortByKey(towns, 'mayor')

const allData = towns.map(town => `${town.mayor} (${town.name})`).join('\n').match(/(?:^.*$\n?){1,20}/mg)
return await new CustomEmbed(client, "Online Activity | Mayors")
Expand All @@ -72,12 +75,12 @@ export default {
case "kings": {
const allNations = await Aurora.Nations.all().catch(err => console.log(err))
if (!allNations || allNations.length < 1) return await interaction.editReply({
embeds: [fn.fetchError]
embeds: [fetchError]
//ephemeral: true
})

const nations = allNations.filter(n => ops.find(op => op.name == n.king))
fn.sortByKey(nations, 'king')
sortByKey(nations, 'king')

const allData = nations.map(nation => `${nation.king} (${nation.name})`).join('\n').match(/(?:^.*$\n?){1,20}/mg)
return await new CustomEmbed(client, "Online Activity | Kings")
Expand All @@ -90,7 +93,7 @@ export default {
.setColor(Colors.Red)
.setTitle("Invalid Arguments")
.setDescription("Arguments: `all`, `staff`, `mayors`, `kings`")
] /* ephemeral: true */ })
]})
}
}, data: new SlashCommandBuilder()
.setName("online")
Expand Down
24 changes: 15 additions & 9 deletions aurora/slashcommands/playerposition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {
import striptags from 'striptags'

import * as db from "../../bot/utils/database.js"
import * as fn from '../../bot/utils/fn.js'
import {
botDevs, devsFooter,
embedField
} from '../../bot/utils/fn.js'

export default {
name: "playerposition",
Expand All @@ -19,23 +22,26 @@ export default {
.setTitle("Error while using /playerposition:")
.setDescription("Not enough arguments, please provide a valid playername.")
.setTimestamp()
.setFooter(fn.devsFooter(client))
.setFooter(devsFooter(client))
], ephemeral: true})

const opsData = await db.Aurora.getOnlinePlayerData()
if (!opsData) return interaction.reply({embeds: [new EmbedBuilder()
.setTimestamp()
.setColor(Colors.Red)
.setTitle("Connection Issues")
.setAuthor({name: interaction.user.username, iconURL: interaction.user.displayAvatarURL()})
.setAuthor({
name: interaction.user.username,
iconURL: interaction.user.displayAvatarURL()
})
.setDescription("Unable to fetch Towny data, the server may be down for maintenance.\n\nPlease try again later.")
.setFooter(fn.devsFooter(client))
.setFooter(devsFooter(client))
]}).then(m => setTimeout(() => m.delete(), 10000)).catch(() => {})

const ops = opsData.players
const foundPlayer = ops.find(op => op.name.toLowerCase() == player.toLowerCase())

if (foundPlayer && !fn.botDevs.includes(player.toLowerCase())) {
if (foundPlayer && !botDevs.includes(player.toLowerCase())) {
const acc = foundPlayer.name

if (foundPlayer.world == "-some-other-bogus-world-") {
Expand All @@ -52,16 +58,16 @@ export default {
.setThumbnail(`https://crafatar.com/avatars/${acc}/256.png`)
.setColor(Colors.DarkVividPink)
.setTimestamp()
.setFooter(fn.devsFooter(client))
.setFooter(devsFooter(client))

const foundPlayerNickname = striptags(foundPlayer.display_name)
if (acc !== foundPlayerNickname)
locationEmbed.addFields(fn.embedField("Nickname", foundPlayerNickname))
locationEmbed.addFields(embedField("Nickname", foundPlayerNickname))

const { x, z } = foundPlayer
locationEmbed.addFields(
fn.embedField("Coordinates", `X: ${x}\nZ: ${z}`),
fn.embedField("Dynmap Link", `[${x}, ${z}](https://map.earthmc.net?worldname=earth&mapname=flat&zoom=6&x=${x}&y=64&z=${z})`)
embedField("Coordinates", `X: ${x}\nZ: ${z}`),
embedField("Dynmap Link", `[${x}, ${z}](https://map.earthmc.net?worldname=earth&mapname=flat&zoom=6&x=${x}&y=64&z=${z})`)
)

return interaction.reply({ embeds: [locationEmbed] }).catch(() => {})
Expand Down
Loading

0 comments on commit 77f96d6

Please sign in to comment.