I am implementing audit logs in my bot.
I want to ask, if its possible to check for multiple channel names.
const logs = message.guild.channels.cache.find(channel => channel.name === "logs");
if (message.guild.me.hasPermission('MANAGE_CHANNELS') && !logs) {
message.guild.channels.create('logs', { type: 'text' });
I am using this to check if a log channel exists, but if not, the bot will create one.
I want to add more channel names, because some servers dont have the same name for logs channel.
I think what you are asking is how to search for multiple names e.g. logs or bot-log for example. All you need to do is use the OR operator (||) like this:
const logs = message.guild.channels.cache.find(channel => channel.name === "logs" || channel.name === "bot-log");
These operators work in many circumstances e.g. if statements
let foo = 'foo'
let bar = 'bar'
if (foo === 'foo' && bar === 'foo') return //this will never call - the && operator means 'AND'
if (foo === 'foo' || bar === 'foo') return //this WILL call - the || operator means 'OR'
Related
I have two routes, /Profile which shows the user their own profile.
(it doesnt need to request this because their profile is already stored in state)
Then the second /Profile/.... which shows the user other profiles.
(it does need to request this because the state does not store all other users)
To have this functionality I have tried inputting this undefined / null check but it doesn't fail when the params are empty:
const getRequest = async () => {
const user_response = null;
console.log("param",params)
console.log("param id",params.id)
if (params !== null || params !== undefined) {
console.log("params true")
if (params.id !== null || params.id !== undefined) {
console.log("id true")
}
else{
console.log("false")
}
}
else{
console.log("false")
}
The following is the console outputs when no params are passed:
> param {}
> param id undefined
> params true
> id true
The following is the console outputs when params are passed:
> param {id: '321'}
> param id 321
> params true
> id true
How can I make it work as intended?
If your user ID can't be falsy (e.g. 0 is not a valid user ID, or '' isn't), then just
const userId = params?.id || null;
without any other fuss – userId it'll be either the ID if it's truthy, or null.
the empty object still return true, so if you want to check the object is empty or not you must do something like this:
const obj = {};
const isEmpty = Object.keys(obj).length === 0;
then you can check the param object with this
It looks like when you're not passing any params, "params" is default an empty object which is bypassing your if statementes ( because it is actually different than null and undefined)
You need a more clear condition; can use lodash library which has a method _.isEmpty() to check for empty objects or as other have stated use the Object.Keys length
I am looking for a cleaner way to close my three dropdowns when I click outside of them. Currently, I am utilizing window.addEventListener('click') with some exclusions to get this done. While it works decently, I do get random errors saying that my event.target is undefined. I haven't had the best luck at repeating the error either. Anyway, my current code is below.
useEffect(() => {
// close dropdown on any click outside of button/dropdown/dropdown options
// these are simple exclusions from the global click - they include dropdowns, dropdown options, and filter/sort buttons
window.addEventListener("click", e => {
if (e.path[4].id.includes('filterSortBar') ||
e.target.id.includes('maxSlider') ||
e.target.id.includes('minSlider') ||
e.target.id.includes('minInputContainer') ||
e.target.id.includes('maxInputContainer') ||
e.target.type === 'number' ||
e.path[4].id.includes('filterButtonsContainer') ||
e.path[4].id.includes('relativeContainer') ||
e.target.textContent === 'Relevance' ||
e.target.textContent === 'Retailers' &&
!e.target.className.includes('buttonPadding')){
} else {
setAddFilterTruthy(false)
setSortTruthy(false)
setPriceTruthy(false)
}
})
}, []);
The ugliness is glaring. Basically, if the click does not contain the element, I set the truthy value to false and the dropdown closes appropriately.
Hello I am using filter with map in react.
props.types.filter(data => (data.is_active === 1)).map((data, index) => {
// some code.
});
I am getting props.types in this component. But sometimes props.types does not contain is_active property. So in that case, map function doesn't return anything. I want to run filter only when data.is_active property exists.
How do I do this ?
UPDATE 1:
As I said above I want to run filter only if is_active is exists but I will always run map.
Add a condition to your filter that checks if is_active is not set.
This way your map will always be executed, except when is_active is not 1.
props.types
.filter((data) => data.is_active === undefined || data.is_active === 1)
.map((data, index) => {
// some code.
});
You can add conditional operator "?" after your filter method.
props.types.filter(data => (data.is_active === 1))?.map((data, index) => {
// some code.
});
I saw this code snippet in React.
const selected = document.activeElement === inputRef?.current?.current;
Does inputRef?.current?.current equal if (inputRef !== undefined && inputRef.current !== undefined) return inputRef.current
Yes, this is the new features for nodeJS v14.0 called Optional Chaining, it check if the value is !undefined
Thus, inputRef?.current?.current equals to
if (inputRef && inputRef.current) return inputRef.current.current
Please read this interesting post on medium about the most important new features for NodeJS
Yesterday I had a behaviour that I don't understand by using Immer.js and setState together. I was using a setState (in a bad way, by the way) when fetching my data and this fetch was called at each endReached of my SectionList.
This setState looked like this:
this.setState((prev) => {
let sections = prev.sections
/* Extract alive topics from "topics"(array retrieved from fetch)*/
let topicsSection1 = topics.filter((card) => !card.states.is_killed)
/* Extract killed topics from "topics"(array retrieved from fetch)*/
let topicsSection2 = topics.filter((card) => card.states.is_killed)
if (sections[0] && sections[0].data)
sections[0].data = positionExtracted > 1 ? sections[0].data.concat(...topicsSection1) : topicsSection1
if (sections[1] && sections[0].data)
sections[1].data = positionExtracted > 1 ? sections[1].data.concat(...topicsSection2) : topicsSection2
return {
sections: sections,
position: response.position,
lastPage: response.laftPage
}
})
and everything worked just fine.
However, I have a function that is called when you press on the topic, and it changes the "opened" value of the topic in the data array to indicate to the list that "this topic" is open.
This function calls the "produce" function of Immer.js
And this function looks like this:
_onPressTopic = (id_card) => {
this.setState(produce((draft) => {
if (draft.sections[0] && draft.sections[0].data)
draft.sections[0].data = draft.sections[0].data.map((item) => {
if (item.id === id_card)
item.opened = !item.opened
return item
})
if (draft.sections[1] && draft.sections[1].data)
draft.sections[1].data = draft.sections[1].data.map((item) => {
if (item.id === id_card)
item.opened = !item.opened
return item
})
}))
}
MY PROBLEM IS:
If I open a topic and then my list data goes through this function, then when an endReached is called again, either I get an error "This object is not expensive", or my list data is not modified at all. And if instead of my first setState, I use a produce from Immer, everything works again.
What I don't understand is: Why does everything work perfectly if I only use Immer.js or just SetState, but as soon as I try to use both together, they don't seem to get along?
Thank you for your answers,
I hope I made it clear !
Viktor