DiscordJS 13用户嵌入显示在特定频道发送的最后一条消息
Posted
技术标签:
【中文标题】DiscordJS 13用户嵌入显示在特定频道发送的最后一条消息【英文标题】:DiscordJS 13 user embed display last message sent in specific channel 【发布时间】:2021-11-24 13:57:23 【问题描述】:我正在为 discord.js 13 构建一个斜杠命令。该命令检查用户角色并显示有关他们的特定信息。我们希望通过介绍渠道添加人员介绍。是否可以从特定频道提取用户发送的最后一条消息?
当前代码
try
await guild.members.fetch();
const member = guild.members.cache.get(UserOption.id);
const roles = member.roles;
const userFlags = UserOption.flags.toArray();
const activity = UserOption.presence?.activities[0];
//create the EMBED
const embeduserinfo = new MessageEmbed()
embeduserinfo.setThumbnail(member.user.displayAvatarURL( dynamic: true, size: 512 ))
embeduserinfo.setAuthor("Information about " + member.user.username + "#" + member.user.discriminator, member.user.displayAvatarURL( dynamic: true ), "https://discord.gg/FQGXbypRf8")
embeduserinfo.addField('**❱ Username**',`<@$member.user.id>\n\`$member.user.tag\``,true)
embeduserinfo.addField('**❱ Avatar**',`[\`Link to avatar\`]($member.user.displayAvatarURL( format: "png" ))`,true)
embeduserinfo.addField('**❱ Joined Discord**', "\`"+moment(member.user.createdTimestamp).format("DD/MM/YYYY") + "\`\n" + "`"+ moment(member.user.createdTimestamp).format("hh:mm:ss") + "\`",true)
embeduserinfo.addField('**❱ Joined MetroVan**', "\`"+moment(member.joinedTimestamp).format("DD/MM/YYYY") + "\`\n" + "`"+ moment(member.joinedTimestamp).format("hh:mm:ss")+ "\`",true)
//DIRECT MESSAGE DISPLAY CODE
if (roles.cache.find(r => r.id === "893305823315492914")) //dms are open
embeduserinfo.addField("**❱ DM STATUS**\n`???? OPEN` ", "** **", true)
【问题讨论】:
你的尝试在哪里?你有吗? 【参考方案1】:如果您只需要频道中的最后一条消息,请使用以下代码。 Get last message sent to channel的可能重复
let channel = bot.channels.get('id-of-the-channel');
channel.messages.fetch( limit: 1 ).then(messages =>
let lastMessage = messages.first();
if (!lastMessage.author.bot)
// Do something with it
)
.catch(console.error);
如果您想要特定用户在频道中发送的最后一条消息,请使用 id 属性对其进行过滤,如下所示-
let channel = bot.channels.get('id-of-the-channel');
channel.messages.fetch().then(messages =>
let lastMessage = messages.filter(m => m.author.id === 'id-of-user').last;
//Do something with it
)
.catch(console.error);
【讨论】:
已实现此代码,但收到错误DiscordAPIError: Missing Access at RequestHandler.execute (/Users/server/Bot Deployed/node_modules/discord.js/src/rest/RequestHandler.js:298:13) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async RequestHandler.push (/Users/server/Bot Deployed/node_modules/discord.js/src/rest/RequestHandler.js:50:14) at async MessageManager._fetchMany (/Users/server/Bot Deployed/node_modules/discord.js/src/managers/MessageManager.js:219:18) method: 'get', path: '/channels/12456789/messages', code: 50001,
尝试给你的机器人管理员权限
今天晚些时候将对此进行调查并报告。应该工作【参考方案2】:
async function searchForLastMessageFromUser(channel, userId)
let messages = await channel.messages.fetch(before: null, cache: true);
while (messages.size > 0)
let result = messages.find(message =>
return message.author.id === userId;
);
if (result !== null) return result;
messages = await channel.messages.fetch(limit: 50, before: messages[messages.length - 1], cache: true);
return null;
embeduserinfo.setDescription("**❱ INTRODUCTION**\n" + introMessageText, "** Add your intro at <#885123781130067988>**");
【讨论】:
这可能会中断的原因 - 如果用户的最后一条消息是最后的第 51 条消息怎么办?它不会被读取以上是关于DiscordJS 13用户嵌入显示在特定频道发送的最后一条消息的主要内容,如果未能解决你的问题,请参考以下文章