Basically I'm trying to create a pagination limit per embed, So if there are 10+ values then there would be separate embeds per 10. So far I was doing, But it just spams it and doesn't work any ideas?
const pages = []
const result = []
const limit = 10;
console.log(result.length)
for (let i = 0; i < result.length; i++) {
if(limit >= result.length){
let reactionhistry = new Discord.MessageEmbed()
.setDescription(result)
pages.push(reactionhistry)
paginationEmbed(message, pages)
}
}
Related
I want to make once the code is redeemed it not be able to be used
const codes = ['5345345345345345','23123123123','312312312321q3']
for (var i = 0; i < codes.length; i++) {
if (message.content.includes(`redeem ${codes[i]}`)) {
message.channel.send("YES IT WORKED")
break;
}
}
You can essentially use Array.prototype.splice() to remove elements from the array therefore modifying it, so i would do something like this
const codes = ['5345345345345345','23123123123','312312312321q3']
for (var i = 0; i < codes.length; i++) {
if (message.content.includes(`redeem ${codes[i]}`)) {
// finding index of the code
const index = codes.indexOf(codes[i]);
//splicing from array
codes.splice(index, 1) // splice(index of element, number of elements to delete)
message.channel.send("YES IT WORKED")
break;
}
}
I am trying to achieve a method in which the array steps got filled with new data every time I click on the button of Create New array, but instead of that, the data is getting appended instead of updating.
here are my states :
const [arr , setArray] = useState(createArray())
const [steps , setSteps] = useState([]);
const [selectedAlgorithm , setSelectedAlgorithm] = useState ();
here is my create new Array function :
const handleCreateNewData = ()=>{
let newarr = createArray();
setArray ([]);
setArray([...newarr]);
setSteps ([]);
setTimeout(()=>{
if ( algorithms[selectedAlgorithm] !== undefined){
algorithms[selectedAlgorithm](arr, steps , setSteps);
console.log('running')
}
},2000)
}
here is my bubble sort algorithm :
export const BubbleSort = (array , steps ,setSteps) =>{
let funarray = new Array();
funarray = [...array] ;
for (let i = 0 ; i < funarray.length-1 ; i++){
for(let j = 0 ; j < funarray.length-1 ; j++){
if(funarray[j]>funarray[j+1]){
[funarray[j],funarray[j+1]] = [funarray[j+1],funarray[j]]
setSteps([...steps, funarray])
steps.push(funarray.slice());
console.log('Working')
}
}
}
return funarray;
}
What is supposed to do is every time I click on create new array it should generate a new set of arrays but instead of creating new arrays it just appending the new arrays in the old steps.
You can create a temp array to hold the steps, then when the loops are done, call setSteps:
const BubbleSort = (array, steps, setSteps) => {
let funarray = [];
funarray = [...array];
let temp = [];
for (let i = 0; i < funarray.length - 1; i++) {
for (let j = 0; j < funarray.length - 1; j++) {
if (funarray[j] > funarray[j + 1]) {
[funarray[j], funarray[j + 1]] = [funarray[j + 1], funarray[j]];
temp.push(funarray)
}
}
}
setSteps(temp);
return funarray;
};
Sample: https://codesandbox.io/s/cool-wind-ijj7z?file=/src/App.js
I am new to React and working on a project to get a better grasp of all its concepts. I am currently building a time-tracking application, that allows users to track time across tasks from different projects.
I am using Redux and storing in my app state a list of Projects each with a list of Tasks. Each task has a totalDurationInSeconds property.
I want to create a Reports page. Currently on the reports page, I only want to display the total duration in seconds across all projects. When I first start the application, the time is 0. If I add a task to one of the projects, the time gets updated.
However, when I add a second task to either the same project or a different project, the value does not get updated and it still only displays the duration of the first task.
const ReportsPage: React.FC<Props> = (props): React.ReactElement => {
const [totalDuration, setTotalDuration] = useState(0);
useEffect(() => {
for (let i = 0; i < props.projects.length; i++) {
for (let j = 0; i < props.projects[i].tasks.length; i++) {
setTotalDuration(totalDuration + props.projects[i].tasks[j].totalDurationInSeconds);
}
}
}, [])
return (
<div>
<p>Total time spent across all projects : {totalDuration}</p>
</div>
);
};
My component is connected to the ReduxStore and Props is of type StateProps & ReportsPageProps.
Your inner loop condition and increment is using i instead of j
this is what you want:
for (let i = 0; i < props.projects.length; i++) {
for (let j = 0; j < props.projects[i].tasks.length; j++) {
setTotalDuration(totalDuration + props.projects[i].tasks[j].totalDurationInSeconds);
}
}
When useEffect function used without any dependency it executed one time, but you want the totalDuration will update when any task added.
useEffect(() => {
for (let i = 0; i < props.projects.length; i++) {
for (let j = 0; j < props.projects[i].tasks.length; j++) {
setTotalDuration(totalDuration + props.projects[i].tasks[j].totalDurationInSeconds);
}
}
}, [props.projects])
I've been trying to create a system that automatically sets the slowmode in a channel to a certain amount depending on how many messages have been sent. Lua is my primary language, and not Node.js, therefore I'm having quite a bit of trouble as to how I would go about this. If anyone had any suggestions, please let me know.
Two ways to go about it:
Use discord.js's <TextChannel>.setRateLimitPerUser(number)
https://discord.js.org/#/docs/main/stable/class/TextChannel?scrollTo=setRateLimitPerUser
Not sure if this actually does what you want though, the other option is creating a session storage of the text channels msgCount and compare it to time, i found setRateLimitPerUser in the middle of writing the code so didn't finish it, be here's a start:
const { Client, Collection } = require("discord.js");
const client = new Client();
client.slowdown = new Collection();
client.on("message", msg => {
const id = msg.channel.id;
const attempt = client.slowdown.get(id);
//4 messages at most per second
const ratio = 4;
//look at last how many seconds
const timeSpace = 5;
//TODO: check if channel already has cooldown
if (attempt) {
attempt.msgCount++;
const currentTime = Date.now();
const timePassed = (currentTime - attempt.time) / 1000;
if (attempt.msgCount >= ratio && attempt.msgCount / timePassed >= ratio) {
//setCoolDown
}
if (timePassed >= timeSpace) {
attempt.time = currentTime;
attempt.msgCount = 0;
}
} else {
client.slowdown.set(id, {
time: Date.now(),
msgCount: 1
});
}
});
I developing my toy project with riot api.(league of legends game)
And I have a question. as you can see I request to riot api in for loop.
It take 20 seconds... this is the problem. I think riot api is not a problem.
I think just request in for loop is the problem.
How can I make it faster than now? help me please ㅠ.ㅠ
for (let cnt = 0; cnt < 20; cnt++) {
let temp = await api.getMatchInfo(res.matches[cnt].gameId);
if (temp.gameMode === "CLASSIC" && temp.gameDuration >= 800) {
console.log("gameList:", temp);
temp["cnt"] = cnt;
gameList.push(temp);
rankCnt++;
}
}
Instead of sequence request each for loop, you can make it parallel by Promise.all. You can see more here https://developer.mozilla.org/vi/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
const promises = []
for (let cnt = 0; cnt < 20; cnt++) {
promises.push(api.getMatchInfo(res.matches[cnt].gameId)
}
Promise.all(promises).then(games => {
console.log(games)
})
//or
const games = await Promise.all(promises)
console.log(games)