?
在discord.js中,我们可以使用分页嵌入(pagination embed)来发送带有音乐列表的消息。分页嵌入允许我们将大量的信息分成多个页面,并通过反应来切换页面。
以下是一个示例代码,演示如何使用discord.js发送带有音乐列表的分页嵌入:
const { Client, MessageEmbed } = require('discord.js');
const client = new Client();
// 音乐列表
const musicList = [
{ title: '歌曲1', duration: '3:30' },
{ title: '歌曲2', duration: '4:15' },
{ title: '歌曲3', duration: '2:55' },
// 添加更多音乐...
];
client.on('message', message => {
if (message.content === '!music') {
// 创建分页嵌入
const embed = new MessageEmbed()
.setColor('#0099ff')
.setTitle('音乐列表')
.setThumbnail('https://example.com/music.png')
.setFooter('页码 1/3');
// 添加音乐列表内容
for (const music of musicList) {
embed.addField(music.title, `Duration: ${music.duration}`);
}
// 发送分页嵌入
message.channel.send(embed).then(sentEmbed => {
// 添加页码反应
sentEmbed.react('⬅️'); // 上一页
sentEmbed.react('➡️'); // 下一页
// 创建一个过滤器,用于检测用户反应
const filter = (reaction, user) =>
['⬅️', '➡️'].includes(reaction.emoji.name) && user.id === message.author.id;
// 创建反应监听器
const collector = sentEmbed.createReactionCollector(filter, { time: 60000 });
// 设置初始页码
let page = 1;
collector.on('collect', reaction => {
// 判断用户反应并切换页面
if (reaction.emoji.name === '⬅️') {
page--;
} else if (reaction.emoji.name === '➡️') {
page++;
}
// 防止页码越界
if (page < 1) page = 1;
if (page > 3) page = 3;
// 更新嵌入页码
embed.setFooter(`页码 ${page}/3`);
// 更新已发送的分页嵌入
sentEmbed.edit(embed);
});
});
}
});
client.login('YOUR_DISCORD_TOKEN');
在上述示例代码中,我们首先创建了一个音乐列表(musicList
),其中包含了多个音乐的标题和时长。当用户发送!music
命令时,我们会创建一个初始页码为1的分页嵌入,并将音乐列表内容添加到嵌入中。
然后,我们通过message.channel.send(embed)
来发送分页嵌入,并使用.then()
方法获取已发送的嵌入消息对象sentEmbed
。接着,我们给嵌入消息对象添加了两个反应⬅️
和➡️
,并创建了一个反应过滤器和反应监听器。
当用户点击反应时,监听器会检测用户反应并切换页面。每次页面切换时,我们更新嵌入的页码并通过sentedEmbed.edit(embed)
方法来更新已发送的分页嵌入。
这样,用户就可以通过点击不同的反应来浏览音乐列表的不同页面。
以上是关于如何发送带有音乐列表discord.js的分页嵌入的解答。希望对你有帮助!
领取专属 10元无门槛券
手把手带您无忧上云