Skip to content

Commit

Permalink
hopper and add method improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoToast committed Feb 2, 2022
1 parent 4ed666a commit 217bc90
Show file tree
Hide file tree
Showing 12 changed files with 476 additions and 430 deletions.
98 changes: 65 additions & 33 deletions .github/configSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,85 @@
"description": "Link to to the bot's source code.",
"default": "https://github.com/NachoToast/Jukebot"
},
"readyTimeout": {
"type": "integer",
"description": "If the bot doesn't connect to Discord after this many seconds, the process will terminate.\nSet to 0 to never terminate.",
"default": 10
},
"levenshteinThreshold": {
"type": "number",
"description": "Minimum string similarity of search result and queried search.\nResults below this number won't be queued.\nUsed to prevent unintentionally queueing random videos.",
"default": 0.1
},
"activityString": {
"type": "string",
"description": "What Discord shows the bot is listening to.", "default": "sus remixes"
},
"colourTheme": {
"type": "string",
"description": "This is the colour that embeds will have.\nMust be a HEX colour string (and start with a #).",
"default": "#794c36",
"examples": [
"#FF00FF",
"#c0c0c0"
]
"description": "What Discord shows the bot is listening to.",
"default": "sus remixes"
},
"maxQueueSize": {
"type": "integer",
"description": "Won't allow adding items to queues greater than or equal to this in length.\nSet to `0` to have no maximum size.",
"description": "Maximum number of items in the queue at any given time.\nSet to 0 to have no maximum size.",
"default": 1000
},
"volumeModifier": {
"type": "number",
"description": "Modifies the playback volume, valid range is between 0 and 1, with 1 being no modification and 0 muting audio entirely.",
"description": "Global volume modifier, with 1 being no modification and 0 muting audio entirely.",
"default": 0.5
},
"maxReadyTime": {
"type": "number",
"description": "The maximum time to wait (in seconds) for the bot to connect and start playing audio once.\nConnecting and playing audio counts as 2 seperate tasks, so the maximum time to connect AND play is 2 x this value",
"default": 5
"colourTheme": {
"type": "string",
"description": "The colour that embeds will have. Must be a hexadecimal colour string.",
"default": "#794c36"
},
"inactivityTimeout": {
"levenshteinThreshold": {
"type": "number",
"description": "The maximum time (in seconds) Jukebot will stay in voice channels without playing anything.\nSet to 0 to stay in voice channels forever.",
"default": 60
"description": "Minimum string similarity of search result and queried search.\nResults below this number won't be queued.\nUsed to prevent unintentionally queueing random videos.",
"default": 0.1
},
"releaseRecentThreshold": {
"type": "number",
"description": "Releases from more than this many minutes ago won't be announced.Doesn't apply in devmode.\n",
"default": 5
"announcementSystem": {
"type": "object",
"description": "Detect and announce new releases on startup.",
"properties": {
"dontAnnounceOlderThan": {
"type": "integer",
"description": "Don't announce releases older than this many minutes.",
"default": 5
},
"enabledInDevelopment": {
"type": "boolean",
"default": false
},
"enabledInProduction": {
"type": "boolean",
"default": true
}
}
},
"timeoutThresholds": {
"type": "object",
"description": "Maximum time in seconds to perform a certain action. Set to `0` to have no maximum time.",
"properties": {
"connect": {
"type": "integer",
"description": "Connect to a voice channel.",
"default": 3
},
"generateResource": {
"type": "integer",
"description": "Generate audio resource from stream.",
"default": 3
},
"getSearchResult": {
"type": "integer",
"description": "Load initial results from search term or link.",
"default": 3
},
"inactivity": {
"type": "integer",
"description": "Leave after not playing anything.",
"default": 10
},
"login": {
"type": "integer",
"description": "Login to Discord.",
"default": 5
},
"play": {
"type": "integer",
"description": "Start playing an audio resource.",
"default": 3
}
}
}
}
}
23 changes: 16 additions & 7 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
{
"$schema": ".github/configSchema.json",
"sourceCode": "https://github.com/NachoToast/Jukebot",
"readyTimeout": 10,
"levenshteinThreshold": 0.1,
"sourceCode":"https://github.com/NachoToast/Jukebot",
"activityString": "sus remixes",
"colourTheme": "#794c36",
"maxQueueSize": 1000,
"volumeModifier": 0.5,
"maxReadyTime": 5,
"inactivityTimeout": 60,
"releaseRecentThreshold": 5
"colourTheme": "#794c36",
"levenshteinThreshold": 0.1,
"announcementSystem": {
"dontAnnounceOlderThan": 5,
"enabledInDevelopment": true,
"enabledInProduction": true
},
"timeoutThresholds": {
"login": 5,
"connect": 3,
"generateResource": 3,
"getSearchResult": 3,
"inactivity": 10,
"play": 3
}
}
8 changes: 4 additions & 4 deletions src/classes/Announcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ export class Announcer {
);
return;
}
if (
Math.floor((Date.now() - new Date(latestTime).getTime()) / 1000) >
Jukebot.config.releaseRecentThreshold * 60
) {

const minsSinceRelease = 60 * Math.floor((Date.now() - new Date(latestTime).getTime()) / 1000);

if (minsSinceRelease > Jukebot.config.announcementSystem.dontAnnounceOlderThan) {
console.log(
`[Announcer] latest release (${Colours.FgMagenta}${latest.name}${
Colours.Reset
Expand Down
41 changes: 19 additions & 22 deletions src/classes/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { FullInteraction, GuildedInteraction } from '../types/Interactions';
import { Jukebox } from './Jukebox';
import { getAuth, getConfig } from '../helpers/getConfig';
import { Announcer } from './Announcer';
import { entersState, VoiceConnectionStatus } from '@discordjs/voice';
import Auth from '../types/Auth';

export class Jukebot {
Expand Down Expand Up @@ -65,18 +64,20 @@ export class Jukebot {
this.client.on('interactionCreate', (int) => this.onInteractionCreate(int));

// logging in
const timeout = Jukebot.config.readyTimeout
const timeout = Jukebot.config.timeoutThresholds.login
? setTimeout(() => {
console.log(`took too long to login (max ${Jukebot.config.readyTimeout}s)`);
console.log(`took too long to login (max ${Jukebot.config.timeoutThresholds.login}s)`);
process.exit(1);
}, Jukebot.config.readyTimeout * 1000)
}, Jukebot.config.timeoutThresholds.login * 1000)
: null;
try {
await this.client.login(loginToken);
if (timeout) clearTimeout(timeout);
} catch (error) {
if (error instanceof Error && error.message === 'TOKEN_INVALID') {
console.log(`invalid token in ${Colours.FgMagenta}auth.json${Colours.Reset} file`);
console.log(
`invalid ${this.devMode ? 'dev' : ''}token in ${Colours.FgMagenta}auth.json${Colours.Reset} file`,
);
} else console.log(error);
process.exit(1);
}
Expand Down Expand Up @@ -107,6 +108,16 @@ export class Jukebot {
// deploying commands
if (this.devMode) await this.guildDeploy(token, toDeploy);
else await this.globalDeploy(token, toDeploy);

if (this.devMode) {
if (Jukebot.config.announcementSystem.enabledInDevelopment) {
new Announcer(this.client.guilds);
}
} else {
if (Jukebot.config.announcementSystem.enabledInProduction) {
new Announcer(this.client.guilds);
}
}
}

private async onInteractionCreate(interaction: Interaction): Promise<void> {
Expand Down Expand Up @@ -175,28 +186,14 @@ export class Jukebot {
console.log(error);
process.exit(1);
}

new Announcer(this.client.guilds);
}

/** Gets an existing Jukebox, or makes one if nonexistent.
* @throws Will throw an error if the Jukebox instance it made doesn't connect in time.
*/
public async getOrMakeJukebox(interaction: FullInteraction): Promise<Jukebox> {
/** Gets an existing Jukebox, or makes one if nonexistent. */
public getOrMakeJukebox(interaction: FullInteraction): Jukebox {
const existingBlock = this.getJukebox(interaction);
if (existingBlock) return existingBlock;

const newBlock = new Jukebox(interaction, (guildId) => this._removeJukebox(guildId));
try {
await entersState(newBlock.connection, VoiceConnectionStatus.Ready, Jukebot.config.maxReadyTime * 1000);
} catch (error) {
// TODO: pass a "notTracked" parameter down command chain to avoid this
console.log(
`About to destroy an untracked Jukebox (connection status: ${newBlock.connection.state.status}), sorry :P`,
);
newBlock.cleanup();
throw new Error(`Failed to connect in reasonable time (${Jukebot.config.maxReadyTime} seconds)`);
}
this._jukeboxes.set(interaction.guildId, newBlock);
return newBlock;
}
Expand All @@ -205,7 +202,7 @@ export class Jukebot {
return this._jukeboxes.get(interaction.guildId);
}

/** Used internally by Jukebox instances wanting to kill themselves. */
/** Callback from Jukebox instances wanting to kill themselves. */
private _removeJukebox(guildId: Snowflake): void {
const deleted = this._jukeboxes.delete(guildId);
if (!deleted) {
Expand Down
Loading

0 comments on commit 217bc90

Please sign in to comment.