Skip to content

Commit

Permalink
Merge pull request #264 from TannerGabriel/feat/default-volume
Browse files Browse the repository at this point in the history
feat: Add option to change default volume
  • Loading branch information
TannerGabriel authored Feb 9, 2024
2 parents c3cf341 + 3c22e9c commit 62f58e9
Show file tree
Hide file tree
Showing 9 changed files with 339 additions and 38 deletions.
4 changes: 2 additions & 2 deletions .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"requirePragma": false,
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"tabWidth": 4,
"trailingComma": "all",
"useTabs": false,
"vueIndentScriptAndStyle": true,
Expand All @@ -20,4 +20,4 @@
}
}
]
}
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ After cloning the project and installing all dependencies, you need to add your

### Changing the status

You can change the status of your discord bot by editing the `activity` and `activityType` variables inside of the `config.json` file. `activityType` needs to be set to an integer with the following [options](https://discord-api-types.dev/api/discord-api-types-v10/enum/ActivityType).
You can change the status of your discord bot by editing the `activity` and `activityType` variables inside the `config.json` file. `activityType` needs to be set to an integer with the following [options](https://discord-api-types.dev/api/discord-api-types-v10/enum/ActivityType).


### Starting the application

Expand Down
32 changes: 17 additions & 15 deletions commands/play.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {GuildMember, ApplicationCommandOptionType} = require('discord.js');
const {QueryType, useMainPlayer} = require('discord-player');
const {isInVoiceChannel} = require("../utils/voicechannel");
const {ApplicationCommandOptionType} = require('discord.js');
const {useMainPlayer} = require('discord-player');
const {isInVoiceChannel} = require('../utils/voicechannel');

module.exports = {
name: 'play',
Expand All @@ -14,44 +14,46 @@ module.exports = {
},
],
async execute(interaction) {
const {default: Conf} = await import('conf');
try {
const inVoiceChannel = isInVoiceChannel(interaction)
const inVoiceChannel = isInVoiceChannel(interaction);
if (!inVoiceChannel) {
return
return;
}

await interaction.deferReply();

const player = useMainPlayer()
const player = useMainPlayer();
const query = interaction.options.getString('query');
const searchResult = await player.search(query)
if (!searchResult.hasTracks())
return void interaction.followUp({content: 'No results were found!'});
const searchResult = await player.search(query);
if (!searchResult.hasTracks()) return void interaction.followUp({content: 'No results were found!'});

try {
const res = await player.play(interaction.member.voice.channel.id, searchResult, {
const config = new Conf({projectName: 'volume'});

await player.play(interaction.member.voice.channel.id, searchResult, {
nodeOptions: {
metadata: {
channel: interaction.channel,
client: interaction.guild?.members.me,
requestedBy: interaction.user.username
requestedBy: interaction.user.username,
},
leaveOnEmptyCooldown: 300000,
leaveOnEmpty: true,
leaveOnEnd: false,
bufferingTimeout: 0,
volume: 10,
volume: config.get('volume') || 10,
//defaultFFmpegFilters: ['lofi', 'bassboost', 'normalizer']
}
},
});

await interaction.followUp({
content: `⏱ | Loading your ${searchResult.playlist ? 'playlist' : 'track'}...`,
});
} catch (error) {
await interaction.editReply({
content: 'An error has occurred!'
})
content: 'An error has occurred!',
});
return console.log(error);
}
} catch (error) {
Expand Down
1 change: 0 additions & 1 deletion commands/resume.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const {GuildMember} = require('discord.js');
const {useQueue} = require("discord-player");
const {isInVoiceChannel} = require("../utils/voicechannel");

Expand Down
28 changes: 14 additions & 14 deletions commands/volume.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {GuildMember, ApplicationCommandOptionType} = require('discord.js');
const {useQueue} = require("discord-player");
const {isInVoiceChannel} = require("../utils/voicechannel");
const {ApplicationCommandOptionType} = require('discord.js');
const {useQueue} = require('discord-player');
const {isInVoiceChannel} = require('../utils/voicechannel');

module.exports = {
name: 'volume',
Expand All @@ -14,25 +14,25 @@ module.exports = {
},
],
async execute(interaction) {
const inVoiceChannel = isInVoiceChannel(interaction)
if (!inVoiceChannel) {
return
}
const {default: Conf} = await import('conf');

await interaction.deferReply();
const queue = useQueue(interaction.guild.id);
if (!queue || !queue.currentTrack)
return void interaction.followUp({
content: '❌ | No music is being played!',
});

let volume = interaction.options.getInteger('volume');
volume = Math.max(0, volume);
volume = Math.min(200, volume);
const success = queue.node.setVolume(volume);

// Set the general volume (persisted)
const config = new Conf({projectName: 'volume'});
config.set('volume', volume);

// Set the volume of the current queue
const queue = useQueue(interaction.guild.id);
const inVoiceChannel = isInVoiceChannel(interaction);
if (inVoiceChannel && queue && queue.currentTrack) queue.node.setVolume(volume);

return void interaction.followUp({
content: success ? `🔊 | Volume set to ${volume}!` : '❌ | Something went wrong!',
content: `🔊 | Volume set to ${volume}!`,
});
},
};
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"activityType": "0",
"activity": "your music selections"
}
}

8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require('dotenv').config()
require('dotenv').config();

const fs = require('fs');
const Discord = require('discord.js');
Expand All @@ -22,7 +22,7 @@ console.log(client.commands);

const player = new Player(client);

player.extractors.loadDefault().then(r => console.log('Extractors loaded successfully'))
player.extractors.loadDefault().then(r => console.log('Extractors loaded successfully'));

// Still needs to be refactored for 0.6
/*player.events.on('connection', (queue) => {
Expand Down Expand Up @@ -86,8 +86,8 @@ client.on('ready', function () {
console.log('Ready!');
client.user.presence.set({
activities: [{name: config.activity, type: Number(config.activityType)}],
status: Discord.Status.Ready
})
status: Discord.Status.Ready,
});
});

client.once('reconnecting', () => {
Expand Down
Loading

0 comments on commit 62f58e9

Please sign in to comment.