我正在尝试创建一个命令来添加对特定消息的反应。
命令是:/react "Channel-ID" "Message-ID" "Emoji"
但是在运行命令时出现此错误:
<块引用>(node:4) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid 表单正文:channel_id:值“845345565700128788 <:XPLANE11:845383490286518322>" 不是雪花。
有什么简单的方法可以解决这个问题吗?
谢谢
client.on("message", message => {
if (message.author.bot) return;
let messageArray = message.content.split(" ");
let command = messageArray[0];
let channelid = messageArray.slice(1);
let messageid = messageArray.slice(2);
let emoji = messageArray.slice(3);
if (message.channel.type === "dm") return;
if (!message.content.startsWith('/')) return;
if (command === '/react') {
let memberrole = message.member.roles.cache.find(role => role.name === "CEO");
if (!memberrole) return message.channel.send('Insufficiant Perms');
message.client.channels.fetch(channelid.slice(1).join(" ")).then(channel => {
channel.messages.fetch(messageid.slice(2).join(" ")).then(message => {
message.react(emoji.slice(3).join(" "));
})
})
}});
对于任何想知道的人来说,这是有效的代码:
client.on('message', async (message) => {
if (
message.author.bot ||
message.channel.type === 'dm' ||
!message.content.startsWith(prefix)
)
return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'react') {
const [channelId, messageId, emoji] = args;
if (!channelId)
return message.channel.send(`You need to provide a channel ID`);
const memberrole = message.member.roles.cache.find((role) => role.name === 'CEO');
if (!memberrole) return message.channel.send('Insufficiant perms');
try {
const channel = await message.client.channels.fetch(channelId);
if (!channel)
return message.channel.send(`No channel found with the ID ${channelId}`);
const msg = await channel.messages.fetch(messageId);
if (!msg)
return message.channel.send(`No message found with the ID ${messageId}`);
msg.react(emoji);
} catch (error) {
console.log(error);
message.channel.send('Oh-oh, there was an error...');
}
}
});
答案 0 :(得分:2)
问题是您错误地使用了 Array#slice()
。 Array#slice()
将数组的一部分的浅拷贝返回到新数组中。当您使用 messageArray.slice(1)
时,您实际上是通过切掉命令 messageArray
的第一个元素来创建一个新数组。对于 messageid
,您将去掉 messageArray
的前两个元素,留下消息 ID 和表情符号。看看下面的片段。如果你运行它,你可以看到每个变量的值:
const message = { content: '/react 845345565700128700 845345565700128788 <:XPLANE11:845383490286518322>' }
let messageArray = message.content.split(' ');
let command = messageArray[0];
let channelid = messageArray.slice(1);
let messageid = messageArray.slice(2);
let emoji = messageArray.slice(3);
console.log({ command, channelid, messageid, emoji })
所以,此时,channelid
是一个包含三个元素的数组;频道 ID、消息 ID 和表情符号。在 channels.fetch()
方法中,您再次创建一个新数组,方法是先切掉第一个元素,然后将其余元素 join
切掉一个空格。所以,它变成了845345565700128788 <:XPLANE11:845383490286518322>
。看看下面的片段:
// messageArray.slice(1);
const channelid = [
'845345565700128700',
'845345565700128788',
'<:XPLANE11:845383490286518322>',
]
console.log(channelid.slice(1).join(' '))
如果您检查尝试获取的值,它正是错误消息中的值,Value "845345565700128788 <:XPLANE11:845383490286518322>" is not snowflake
。这不是有效的雪花。它实际上是单个字符串中的消息 ID 和表情符号。
要解决这个问题,您可以简单地将 messageArray
的第二个元素作为 channelid
,将第三个元素作为 messageid
等:
let messageArray = message.content.split(' ');
let command = messageArray[0];
let channelid = messageArray[1];
let messageid = messageArray[2];
let emoji = messageArray[3];
您也可以使用数组解构来获得相同的效果:
let messageArray = message.content.split(' ');
let [command, channelid, messageid, emoji] = messageArray;
这里是完整的代码:
// use a prefix variable
const prefix = '/';
client.on('message', async (message) => {
if (
message.author.bot ||
message.channel.type === 'dm' ||
!message.content.startsWith(prefix)
)
return;
// create an args variable that slices off the prefix and splits it into an array
const args = message.content.slice(prefix.length).split(/ +/);
// create a command variable by taking the first element in the array
// and removing it from args
const command = args.shift().toLowerCase();
if (command === 'react') {
// destructure args
const [channelId, messageId, emoji] = args;
// check if there is channelId, messageId, and emoji provided
// if not, send an error message
if (!channelId)
return message.channel.send(`You need to provide a channel ID`);
// same with messageId and emoji
// ...
const memberrole = message.member.roles.cache.find((role) => role.name === 'CEO');
if (!memberrole) return message.channel.send('Insufficiant perms');
try {
const channel = await message.client.channels.fetch(channelId);
if (!channel)
return message.channel.send(`No channel found with the ID ${channelId}`);
const fetchedMessage = await channel.messages.fetch(messageId);
if (!fetchedMessage)
return message.channel.send(`No message found with the ID ${messageId}`);
fetchedMessage.react(emoji);
} catch (error) {
console.log(error);
message.channel.send('Oh-oh, there was an error...');
}
}
});