問題描述
我正在嘗試找出是否可以使用 discord.js
I'm trying to find out if its possible to get the time/information of users last activity retrospectively using discord.js
說我有類似的東西
client.guilds.find('id', 'SERVER ID').fetchMembers().then(members => {
const role = members.roles.find('name', 'Newbies')
role.members.forEach(member => {
console.log(member.user.lastMessage) // null
})
})
除非會員已經發帖,因為客戶端在監聽,所以lastMessage始終為null.
Unless the member has posted, since the client is listening, the lastMessage is always null.
有沒有辦法找到最后一個活動?還是一種解決方法,例如返回所有用戶消息的查詢,然后我可以從中獲取最新消息?
Is there a way to find the last activity? or a workaround, like a query to return all the users messages, which I can then take the most recent one from?
實際上,我想知道用戶上次發布的日期/時間,以便我們可以監控非貢獻帳戶.
Effectively I want to know what date/time the user last posted so we can monitor non-contributing accounts.
謝謝
推薦答案
查看文檔后,我也沒有找到任何東西,所以我想出了一個手動搜索功能.
After looking thought the documentation I didn't find something neither so I came up with a manual search function.
基本上,它會掃描每個頻道,直到找到來自 X 用戶的消息,或者頻道中的消息結束.然后它會比較來自每個渠道的用戶的最后一條消息并打印最后一條.
Basically, it will scan every channels until finding a message from X user, or the end of the messages in the channel. It then compare the last messages of the users from every channels and print the last one.
如果用戶很久沒有寫,它可能會很長.當然,您必須在嘗試之前檢查 lastMessage
.
It can be very long if the user hasn't write since a long time. Of course, you have to check lastMessage
before trying this.
我可能會添加一個時間限制.因為如果您有數千條消息,該函數將永遠運行.
I would add a time limit maybe. Because if you have thousand of messages, the function will run eternally.
如果找到的最后一條消息在接受的時間內不被踢/做任何事情,您可以停止該功能.
You can stop the function if the last message found is in the accepted time to not be kick/do whatever.
如果在 fetched messaged 包中找到的第一條消息超過了封禁限制,我就停止了搜索,但是,如果第一條消息不舊,請記住它意味著另一個,所以我們仍然需要檢查它們(也可以通過檢查包的最后一條消息來避免).
I made the search stop if the first message found in the pack of fetched messaged is older than the ban limit, however, if the first message is not older, remember that it means for the other, so we still need to check them (it can be avoided by checking the last message of the pack as well).
async function fetchMessageUser(chan, id, res) {
let option = {};
if (typeof res !== 'undefined'){
option = {before: res.id};
}
return await chan.fetchMessages(option)
.then(async msgs => {
if (msgs.size === 0){
return {continue: false, found: false};
};
if ((Date.now() - (msgs.first().createdTimestamp)) > 86400000 ) { // 1 day
return {continue: false, found: false};
}
let msgByAuthor = msgs.find(msg => {
return msg.author.id === id;
});
if (msgByAuthor === null){
return {continue: true, id: msgs.last().id};
} else {
return {continue: false, found: true, timestamp: msgByAuthor.createdTimestamp};
}
})
.catch(err => console.log('ERR>>', err));
}
client.on('message', async (msg) => {
let timestamp = [];
for (let [id, chan] of msg.guild.channels){
if (chan.type !== 'text'){ continue; }
let id = '587692527826763788'; // id of the user, here a non verified account
let res;
do {
res = await fetchMessageUser(chan, id, res);
} while(res.continue);
if (res.found) {
timestamp.push(res.timestamp);
}
}
console.log(timestamp);
let first = timestamp.sort((a,b) => (b-a))[0];
console.log(new Date(first));
});
更好的變體是為一組用戶運行它,檢查來自每個頻道的所有 50 條最新消息,如果他寫了一條,則將每個用戶與他最近的消息相關聯,并這樣做直到所有消息中的所有消息頻道太舊,無法避免踢/無論如何.然后為所有沒有關聯消息的用戶做一些事情.
A better variant would be to run it for an array of users, checking all 50 last messages from every channel, and associating each users with his most recent messages if he wrote one, and doing this until all the messages in all the channels are too old to avoid a kick/whatever. And then do something for all the users who don't have an associated messages.
這篇關于Discord.js - 讓用戶最后一次活動?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!