diff --git a/src/listeners/inviteroles.js b/src/listeners/inviteroles.js index 21807a3..8f10e1a 100644 --- a/src/listeners/inviteroles.js +++ b/src/listeners/inviteroles.js @@ -63,7 +63,7 @@ async function addInviteRolesToNewMember (member) { // const invite = newInvites.find(i => i.uses > oldInvites.get(i.code)) // updateGuildInvites(member.guild) let source - for (let i = 0; i < 3; i++) { + for (let i = 0; i < 5; i++) { const result = await searchByID(member.guild, member.id) source = result?.source_invite_code console.log(i, !!source) @@ -130,3 +130,5 @@ async function addInviteRolesToNewMember (member) { client.on(Events.GuildMemberAdd, addInviteRolesToNewMember) client.on(Events.GuildMemberAvailable, m => console.log('member available')) +client.on(Events.GuildMemberUpdate, m => console.log('member updated', m.displayName)) +client.addListener(Events.Raw, m => console.log('EVENT:', m.t)) diff --git a/src/modules/members.js b/src/modules/members.js index 91e4772..af633ea 100644 --- a/src/modules/members.js +++ b/src/modules/members.js @@ -20,7 +20,7 @@ async function searchByID (guild, id) { const userIDQuery = { user_id: { or_query: [id] } } const rest = new REST({ version: '10' }).setToken(client.token) const response = await rest.post(`/guilds/${guild.id}/members-search`, { - body: { and_query: userIDQuery, limit: 1 } + body: { and_query: userIDQuery, limit: 1, sort: 1 } }) console.log(response, guild.id, id) const result = response.members[0] @@ -28,4 +28,23 @@ async function searchByID (guild, id) { return result } -module.exports = { searchByID } +/** + * Returns a search result with invite information + * @param {Guild} guild The guild to search + * @returns {Promise} + */ +async function searchByRecent (guild) { + const range = { gte: Date.now() - 3600000 } + const rest = new REST({ version: '10' }).setToken(client.token) + const response = await rest.post(`/guilds/${guild.id}/members-search`, { + body: { and_query: { guild_joined_at: { range } }, limit: 5, sort: 1 } + }) + console.log(response, guild.id) + const results = response.members + if (results) { + results.forEach(m => { m.member = new GuildMember(client, m.member, guild) }) + } + return results +} + +module.exports = { searchByID, searchByRecent } diff --git a/src/routes/api/guilds/index.js b/src/routes/api/guilds/index.js index 886d338..c6b2fe9 100644 --- a/src/routes/api/guilds/index.js +++ b/src/routes/api/guilds/index.js @@ -5,6 +5,7 @@ const router = express.Router() const Joi = require('joi') const { validateAPIKey } = require('../../../modules/api-key-validation') +const { searchByID, searchByRecent } = require('../../../modules/members') router.get('/:guild/user-ids', validateAPIKey, async (req, res) => { if (req.query.users == null || !/^([^#]+#\d{4}(,|$))*/.test(req.query.users)) { @@ -90,4 +91,11 @@ router.get('/:guild/announcements-for', validateAPIKey, async (req, res) => { res.send(result) }) +router.get('/member-test', async (req, res) => { + const guild = await client.guilds.fetch('957408720088891473') + const member = (await searchByID(guild, '437808476106784770')) ?? null + const recent = await searchByRecent(guild, 3) + res.send({ member, recent }) +}) + module.exports = router