hi im working on discord api
and for one section need to get all messages from channel
problem discord api limit : discord have limit to get messages return just 100 message
for give more messages need before query and id
im 80 % pass this limitaion but code need imporove
import axios from "axios";
const api = axios.create({
baseURL:"https://discord.com/api/v9/",
headers:{
Authorization:"xxxx-xxxx--xxxx"
}
});
let contents = [];
async function getContent (query="limit=100"):Promise<[]> {
try {
const content = await api.get(`/channels/111654654546541585/messages?${query}`);
return content.data;
} catch (error) {
throw "[ERROR] - Request error from axios";
}
}
function scrapData(slice:[], cbf:()=>void){
if (slice.length == 0) {
cbf();
return;
}
slice.map(async (content:content, index) => {
contents.push(content.content);
if(index == slice.length - 1) {
const newSlice = await getContent(`before=${content.id}&limit=100`);
setTimeout(()=> scrapData(newSlice, cbf),3000);
}
})
}
getContent().then( (data) => {
scrapData(data, ()=> {console.log(contents)})
})