刚开始编码,最近开始使用 JS 制作 Discord 机器人。这是一个 bot,其中某个 mp4 播放特定片段。
我遇到了问题,当我输入命令时,mp4 不发送,只是嵌入消息。基本上,如果我执行 -snip kratos
,机器人会发送嵌入消息而不是 mp4。
这是我目前所拥有的:
const fs = require('fs');
const { Client, MessageAttachment } = require('discord.js');
const config = require('./config.json');
const { prefix, token } = require('./config.json');
const client = new Client();
client.commands = new Discord.Collection();```
And here are the command events:
``` client.on('message', message => {
if (message.content === `${prefix}snip kratos`) {
if (message.author.bot) return
const kratos = new Discord.MessageEmbed()
.setColor('#ffb638')
.setTitle('Kratos')
.setDescription('Sending 1 snippet(s)...')
.setTimestamp()
.setFooter('SkiBot');
message.channel.send(kratos);
client.on('message', message => {
if (message.content === `${prefix}snip kratos`) {
if (message.author.bot) return
const attachment = new MessageAttachment('./snippets/kratos/Kratos.mp4');
message.channel.send(attachment);
}
});
}
});
client.on('message', message => {
if (message.content === `${prefix}snip johnny bravo`) {
if (message.author.bot) return
const kratos = new Discord.MessageEmbed()
.setColor('#ffb638')
.setTitle('Johnny Bravo')
.setDescription('Sending 1 snippet(s)...')
.setTimestamp()
.setFooter('SkiBot');
message.channel.send(kratos);
client.on('message', message => {
if (message.content === `${prefix}snip johnny bravo`) {
if (message.author.bot) return
const attachment = new MessageAttachment('./snippets/Johnny_Bravo/Johnny_Bravo.mp4');
message.channel.send(attachment);
}
});
}
});```
答案 0 :(得分:0)
你应该可以做到
message.channel.send({
files: [
"./snippets/kratos/Kratos.mp4"
]})
此处所指https://discord.js.org/#/docs/main/stable/class/TextChannel?scrollTo=send
也在这里 client.commands = new Discord.Collection();
在这里您正在调用 Discord 但未定义 Discord
答案 1 :(得分:0)
问题在于您正在嵌套事件侦听器。删除嵌套的 client.on('message', ...)
部分并在发送嵌入后发送消息附件。
client.on('message', (message) => {
if (message.author.bot) {
return
}
if (message.content === `${prefix}snip kratos`) {
const kratos = new MessageEmbed()
.setColor('#ffb638')
.setTitle('Kratos')
.setDescription('Sending 1 snippet...')
.setTimestamp()
.setFooter('SkiBot')
message.channel.send(kratos)
const attachment = new MessageAttachment('./snippets/kratos/Kratos.mp4')
message.channel.send(attachment)
}
})
而且您不需要多个 message
事件侦听器。您可以通过使用有效命令创建对象来简化代码。
client.on('message', (message) => {
const { content, author, channel } = message
if (author.bot) {
return
}
const embeds = {
[`${prefix}snip kratos`]: {
title: 'Kratos',
attachmentPath: './snippets/kratos/Kratos.mp4',
},
[`${prefix}snip johnny bravo`]: {
title: 'Johnny Bravo',
attachmentPath: './snippets/Johnny_Bravo/Johnny_Bravo.mp4',
},
}
const embed = embeds[content]
if (embed) {
const { title, attachmentPath } = embed
channel.send(
new MessageEmbed()
.setColor('#ffb638')
.setTitle(title)
.setDescription('Sending 1 snippet...')
.setTimestamp()
.setFooter('SkiBot')
)
channel.send(new MessageAttachment(attachmentPath))
}
})
如果你没有很多命令,上面的解决方案应该足够了。查看 this 指南以了解如何创建单独的命令文件以保持代码井井有条。