-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.js
129 lines (111 loc) · 4.1 KB
/
audio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const {
ActionRowBuilder,
ButtonBuilder,
ButtonStyle
} = require('discord.js');
const {
createAudioPlayer,
createAudioResource,
joinVoiceChannel,
} = require('@discordjs/voice');
const {
formatInteger,
getIntegerLength,
justFileName,
partition,
} = require('./util.js');
const config = require('./config.json');
async function trackList(interaction) {
const formatLength = getIntegerLength(config.AUDIO_FILE_PATHS.length);
let response = '## Track list\n';
for (let index = 0; index < config.AUDIO_FILE_PATHS.length; index++) {
const path = config.AUDIO_FILE_PATHS[index];
response = response + `${formatInteger(formatLength, index)}: ${justFileName(path)}\n`;
}
await interaction.reply({ content: response, ephemeral: true });
}
function makeButtonRow(buttons) {
const row = new ActionRowBuilder();
for (let index = 0; index < buttons.length; index++) {
row.addComponents(buttons[index]);
}
return row;
}
function limitStrLen(str, len) {
if (str.length > len){
return str.slice(0, len);
} else {
return str;
};
}
function makeButton(path, index) {
return new ButtonBuilder()
.setCustomId('play:' + index)
.setLabel(limitStrLen(justFileName(path), 80))
.setStyle(ButtonStyle.Primary);
}
async function trackButtons(interaction) {
const buttons = config.AUDIO_FILE_PATHS.map(makeButton);
// Discord limits the number of buttons per row and the number of rows per message... so lets respect that
const numButtonsPerRow = 5;
const numRowsPermessage = 5;
const partitionedButtons = partition(partition(buttons, numButtonsPerRow), numRowsPermessage);
// Send first block of buttons as a reply
const firstMessageButtons = partitionedButtons[0].map(makeButtonRow);
await interaction.reply({ content: 'Audio tracks', components: firstMessageButtons, ephemeral: true });
// Send addition blocks of buttons as a follow up
for (let index = 1; index < partitionedButtons.length; index++) {
const laterMessageButtons = partitionedButtons[index].map(makeButtonRow);
await interaction.followUp({ content: 'More audio tracks', components: laterMessageButtons, ephemeral: true });
}
}
async function playFromButton(interaction) {
const trackNum = interaction.customId.split(':')[1];
return _play(interaction, trackNum);
}
async function playFromOption(interaction) {
const trackNum = interaction.options.getInteger('track-num');
return _play(interaction, trackNum);
}
async function _play(interaction, trackNum) {
if (!interaction.member.voice.channel) {
await interaction.reply({ content: 'Join a voice channel then try again!', ephemeral: true });
return;
}
// Join the same voice channel as the user who issued the command
const channel = interaction.member.voice.channel;
const voiceChannelConnection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
});
// Get the existing audio player for this channel or create a new one
let player;
if (voiceChannelConnection.state && voiceChannelConnection.state.subscription && voiceChannelConnection.state.subscription.player) {
player = voiceChannelConnection.state.subscription.player;
} else {
player = createAudioPlayer();
voiceChannelConnection.subscribe(player);
}
// ...........................................................................
const trackPath = config.AUDIO_FILE_PATHS[trackNum % config.AUDIO_FILE_PATHS.length];
const trackName = justFileName(trackPath);
// Play the requested audio file
let resource = createAudioResource(trackPath);
player.play(resource);
if (interaction.isButton()) {
interaction.deferUpdate();
} else {
const replayButton = makeButtonRow([
makeButton(trackPath, trackNum)
]);
interaction.reply({ content: `Playing ${trackName}`, components: [replayButton], ephemeral: true });
}
console.log(`${interaction.member.user.username} is playing ${trackName}`);
}
module.exports = {
trackList: trackList,
trackButtons: trackButtons,
playFromButton: playFromButton,
playFromOption: playFromOption,
};