Skip to content

Commit

Permalink
v2.3.2 - Various fixes related to .env file
Browse files Browse the repository at this point in the history
  • Loading branch information
reishimanfr committed Apr 20, 2024
1 parent 1ac10e2 commit 21c62d9
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 23 deletions.
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
{
"scripts": {
"watch": "nodemon --quiet --watch src/** --exec sucrase-node src/index.ts",
"start": "sucrase-node src/index.ts",
"lavalink": "java -jar ./lavalink.jar",
"deploy": "sucrase-node src/Helpers/DeployCommands.ts"
"start": "sucrase-node src/index.ts"
},
"version": "2.3.1",
"version": "2.3.2",
"dependencies": {
"axios": "^1.6.0",
"discord.js": "^14.13.0",
Expand Down
7 changes: 2 additions & 5 deletions src/Classes/Bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,23 +162,20 @@ class Bot extends Client<true> {
}

this.createIcons()
if (process.env.REGISTER_COMMANDS_ON_START) await this.registerCommands(token)
}

public async registerCommands(token: string) {
logger.info('Registering (/) commands...')

await this.login(token)

const commandModFiles = this.loadModFiles<Command>(COMMANDS_PATH, true)
const jsonData: RESTPostAPIChatInputApplicationCommandsJSONBody[] = commandModFiles.map(m => m.data.setDMPermission(false).toJSON())

await new REST()
.setToken(token)
.put(Routes.applicationCommands(this.user.id), { body: jsonData })
.then(_ => logger.info('Success!'))
.then(_ => logger.info('(/) commands registered successfully!'))
.catch(error => logger.error(`Failed to register (/) commands: ${error.stack}`))

process.exit()
}
}

Expand Down
3 changes: 0 additions & 3 deletions src/Events/Bot/InteractionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ const InteractionCreate: Event = {
defaults: { guildId: interaction.guildId, lastActive: new Date() }
})

console.log(`last active: ${record.getDataValue('lastActive')}`)

record.update({ lastActive: new Date() }, { where: { guildId: interaction.guild.id }})


const handler = InteractionMap[interaction.type]

if (!handler) {
Expand Down
7 changes: 5 additions & 2 deletions src/Events/Bot/VoiceStateUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const UpdateVoiceState: Event = {
name: Events.VoiceStateUpdate,
once: false,
execute: (oldState: VoiceState, newState: VoiceState) => {
console.time()
const guildId = oldState?.guild?.id ?? newState?.guild?.id
const player = client.poru.players.get(guildId) as ExtPlayer | undefined

Expand All @@ -22,14 +23,16 @@ const UpdateVoiceState: Event = {

// Bot disconnect
if (!newChannel) return player.destroy()

const membersWithoutBots = newChannel.members.filter(m => !m.user.bot)

// Everyone left voice
if (membersWithoutBots.size === 0) {
player.pause(true)
player.messageManger.updatePlayerMessage()
player.controller.setupPlayerTimeout(60000) // 1 minute for someone to join
} else {
player.controller.cancelPlayerTimeout()
player.controller.cancelPlayerTimeout()
}
}
}
Expand Down
28 changes: 23 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Bot } from './Classes/Bot'
import cron from 'node-cron'
import { serverStats, ServerStatsI } from './Models'
import sequelize from './Models/Connection'
import { Track } from 'poru'
import { clipString } from './Funcs/ClipString'
require('dotenv').config()

export const client = new Bot({
Expand Down Expand Up @@ -43,6 +45,8 @@ client.initialize(process.env.BOT_TOKEN)
// 100 guilds limit set for unverified bots.
// You can disable this through the .env file
cron.schedule('0 0 * * *', async () => {
if (!process.env.LEAVE_INACTIVE_GUILDS) return

const guilds = await client.guilds.fetch()

for (const [_, guild] of guilds) {
Expand Down Expand Up @@ -72,18 +76,32 @@ cron.schedule('0 0 * * *', async () => {
// Update the bot's status every minute. This will only
// fire when the new presence to be set is different to the
// current presence to not send unneeded requests.
cron.schedule('* * * * *', () => {
const activePlayers = client.poru.players.size
cron.schedule('*/1 * * * *', () => {
const activePlayers = client.poru.players
const currentActivity = client.user?.presence?.activities[0]
let activityName = 'with YouTube\'s API'

if (client.poru.players.size) {
activityName = `music in ${activePlayers} server${activePlayers > 1 ? 's' : ''}`
console.log('updating')

const currentlyPlayedSongs: string[] = []

activePlayers.forEach(player => {
if (player.currentTrack && player.isPlaying) {
currentlyPlayedSongs.push(`${player.currentTrack.info.title} - ${player.currentTrack.info.author}`)
}
})

console.log(currentlyPlayedSongs)

if (currentlyPlayedSongs.length) {
const randomSong = currentlyPlayedSongs[Math.floor(Math.random() * currentlyPlayedSongs.length)]

activityName = clipString({ string: randomSong, maxLength: 100, sliceEnd: '...' })
}

if (currentActivity?.name === activityName) return

client.user.setPresence({
activities: [{ name: activityName, type: ActivityType.Playing }]
activities: [{ name: activityName, type: ActivityType.Listening }]
})
})
4 changes: 0 additions & 4 deletions src/types/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ExtPlayer } from '../Helpers/ExtendedPlayer'
import { Bot } from '../Classes/Bot'

export interface Command<T = true> {
// Command data
data: Omit<SlashCommandBuilder, 'addSubcommandGroup' | 'addSubcommand'> | SlashCommandSubcommandsOnlyBuilder

disabled?: boolean
Expand All @@ -26,11 +25,8 @@ export interface Command<T = true> {
}

musicOptions?: {
/** Must be in a voice channel to be used */
requiresVc?: boolean
/** Must be playing music to use */
requiresPlaying?: boolean
/** Must have the DJ role to use */
requiresDjRole?: boolean
}

Expand Down
2 changes: 2 additions & 0 deletions src/types/process-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ declare global {
TENOR_API_KEY: string

CUSTOM_EMOJIS_GUILD_ID: string
REGISTER_COMMANDS_ON_START: boolean
LEAVE_INACTIVE_GUILDS: boolean

DATABASE_DIALECT: 'sqlite' | 'postgres'
DATABASE_HOST: string
Expand Down

0 comments on commit 21c62d9

Please sign in to comment.