So I'm Having A Problem I Want To See How Many Members My Server Has But It Only Shows 5 I Have More Than 5 Members My Server My Code Is
message.member.guild.members.cache.size
Is There A Way To Fix This? Or Am I Doing Something Wrong...
The easiest way to get the total count of the members in a guild is to use guild.memberCount.
let membercount = message.guild.memberCount
message.channel.send(membercount + " members in this guild.")
You may need to cache the members. The quicker way to do it is like this:
let members = await message.guild.members.fetch();
let amount = members.size; //should give the correct amount
The fetch() method returns a promise, the collection of members in the guild. You must await it though.
Remember to put it in an async function!
You can choose this
let members = (await guild.members.fetch()).filter(m => !m.user.bot); //Filter only the Members
let membersAmount = members.size; //give the correct amount
instead message.member.guild.members.cache.size
Related
I am looking for a way to scan my full server online member list every hour, and change any cancer nicknames. Like names with "!" at the start or "`" pretty much all non letter/number characters. I am using discord.js with all intents enabled. If you can help I will appreciate it :)
The GuildMemberManger.fetch() provides the functionality you require.
members = await <Guild>.members.fetch({query: 'querystring'})
All members with querystring in their username will be returned, in a collection.
After that you can iterate through the collection and do whatever you want.
If you want more control over the conditions you should filter the members cache collection with your own conditions.
condition = (member) => member.nickname.length == 3 //Finds members with 3 letter nicknames.
members = await <Guild>.members.cache.filter(condition)
So I'm trying to create a bot that is more universal and can be added to more than one server. I've managed to reformat commands like announce or join messages etc. by using .find("name", "announcements").
My problem is that the same does not work for roles.
I've tried what little I could find on the internet, such as member.guild.roles.find("name", "Member") or member.guild.roles.find(role => role.name === "Member") but none of these work. A variety of different ways return different errors, but they usually say something along the lines of it not being an integer (I think) or snowflake.
This is the function that I am currently wrestling with:
client.on('guildMemberAdd', (member) => {
var joinResponse = ("Hello **" + member.displayName + "**, welcome to **" + member.guild.name + "**!")
let role = member.guild.roles.find("name", "Member");
member.addRole(role).catch(console.error);
member.guild.channels.find('name', 'general').send(joinResponse);
In short summary, what I'm looking for is the ability to get a role by name and add a user to it.
(I'm nearly certain it's possible, since popular bots like Dyno are capable of using commands to add roles to users.)
client.on('guildMemberAdd', (member) => {
const joinResponse = `Hello **${member.user.username}**, welcome to: **${member.guild.name}**!`
let role = member.guild.roles.get('Member');
if(!role) return console.log("Role doesen't exist.");
member.addRole(role);
const ch = member.guild.channels.get('general');
if(!ch) return console.log("Channel doesen't exist.");
ch.send(joinResponse);
});//You didn't have closing brackets (Could've been a problem)
First of all your string joinResponse was a function and you completely messed up merging it, I recommend using Template literal.
I used get function since it's a lot easier to use and cleaner, you only pass ID or name as a string.
To check if channel exists I used if statements and exclamation mark which means doesen't exist/false.
If I helped you please mark this as answer, thanks <3. If you need anything add comment to answer or message me on Discord (You can find my discriminator on my stack overflow profile).
For further explanation click on Blue words.
Currently, I'm working on processing information I've received from a JSON file, and as it's organized into multiple levels, I need to be able to convert not only the file, but all of its contents into the correct file types. I'm having some issues with this, and I can't find anything for Swift on this issue.
So far, I've tried the methods that are apparent within the code below. I couldn't find much information on alternate methods to do this, and I'm still new to programming, so I haven't been able to do much.
URLSession.shared.dataTask(with: urlRequest) { data, response, error in
if let data = data {
print("Clear")
let returnData = String(data: data, encoding: .utf8)
do {
let image = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
print(image!["results"])
var imgl2 = image!["results"]
imgl2 = imgl2 as! Array
//imgl2 = imgl2![0]
//print(imgl2!["color"])
// var imgl3 = imgl2!["results"]
// var imgl4 = imgl3!["results"]
// var imgl5 = imgl4!["results"]
// var imgl6 = imgl5!["results"]
} catch let error {
print(error)
}
}
}.resume()
What should happen here is that I should be able to convert the JSON's contents into the correct file types. To some extent, this works, and I'm able to obtain the first level of results. However, partway through the process at imgl2 = imgl2 as! Array, I can't convert it to an Array. Instead, I receive the error (Cannot assign value of type 'Array<_>' to type 'Any?'). I'd really like to be able to use the array enclosed, but I can't. After this, there will be several additional levels of content that I will need to sort through in this manner. I've tried looking at the API documentation, but I don't understand the way they've written it (this is Unsplash, by the way), and as such, I've tried this method. How exactly would I go about this?
(For further information, I'm trying to pull out the first image in the results, so that I can then edit it programmatically. I need the URL enclosed in the response to the search query, but the way the documentation is worded is unclear enough, and solutions to this problem so sparse, that I've had to use trial and error to get to where I am. Any insight into how to accomplish this would be greatly appreciated.)
Cannot assign value of type 'Array<_>' to type 'Any?
The error tells you that imgl2 is of type Any?, but you're trying to assign it a value of type Array. Swift doesn't allow changing the type of a variable after it has been initialized. What you could do is to assign imgl2 as! Array to a new variable to avoid this error.
I also suggest you to have a look at Decodable protocol to create models corresponding to the JSON structure.
i am trying desperately to solve the following problem. I researched a lot already but nothing really solved the problem (the used language is Javascript with Node.js and react-bootstrap library).
I want to write the following Array to Buffer and save it to IPFS in a way, that i can read it out later.
Nevertheless, the IPFS.Add() Method demand a buffer object, so i struggle to create the buffer object.
Here the array:
const line_supplier = new Array({
Lieferant: "Yello" ,
Postleitzahl: "13752" ,
Arbeitspreis: "5" ,
Grundpreis: "10" ,
Email: "email"
});
Sounds pretty easy, i know. If i do it like this, the console shows me a an empty buffer.
const line_buffer = await Buffer.from(line_supplier);
console.log(line_buffer);
I also tried ..line_supplier[0], or the recommendations with ..'UTF-8' as offset. I also declared the array directly in Buffer.from(declaration).
Either way, i get an error or just a couple of numbers out. Maybe this is already the mistake. I expect the buffer to be readable like a string?
So i converted the line supplier to a string with
const line_string = JSON.stringify(line_supplier);
But even with this string injected in Buffer.from(), i only get a Uint8Array(84) Object out with a lot of numbers.
I dont know what i am missing. It must be something very small. I read all the description of the methods already, but cant find it.
Thanks in advance!!!
The solution is, the following:
const line_string = JSON.stringify(line_supplier);
console.log(line_string);
const line_buffer = await Buffer.from(line_string);
console.log(line_buffer);
await ipfs.add(line_buffer, (err, ipfsHash) => {
console.log(err,ipfsHash);
Hence you get an array saved the way you want!
I just made a irc client with as3, but the problem is that i don't have too much experience with SharedObject. I need a best way to update an array when a user join other channels.
For example, if i have this array:
var channels:Array = [];
I also have a function where i can get vars if a user join other channels and when the use do that, array must be updated, without removing vars thats already exist in array, but i fails everytime in this. What's the best way to update array for one item such like for "Channels", it must be looks: "'#channel1', '#channel2', '#channel3'". Sorry for my bad english, i am from the netherlands.
To make a bit more clear:, this is one of in switch() for user commands:
case "JOIN":
if(sCmd.split(" ")[1] == "")
{
sendNoticeToServerAndChatroom("Geef een kanaal op.");
} else {
var getJoinChannel = sCmd.split(" ")[1];
sendCommand("JOIN", [getJoinChannel]);
// Here must be a code that sends every channels to a array <- (channels:array = [];)
goToRoom();
}
break;
Thanks!