Get Total Number of Members in Discord Using PHP

Count discord users with json

This will work for you:

   <?php

$jsonIn = file_get_contents('https://discordapp.com/api/guilds/140805434654195712/embed.json');
$JSON = json_decode($jsonIn, true);

$membersCount = count($JSON['members']);

echo "Number of members: " . $membersCount;
?>

What this does is take the JSON string in from your URL, then encodes the string to make it a JSON object (an array of nested arrays pretty much). The JSON string has an 'array' of members. By using a foreach loop you can count how many members objects are in that 'array'.

PHP/HTML - Show discord server statistics on my website

There are plenty of PHP libraries available to get Discord information from their webhooks.

  • https://discordphp.readme.io/docs
  • https://www.restcord.com/

There is also bots available that can give you dashboards you can sign into to get information about your Discord channel.

  • https://docs.statbot.net/docs/

So yes it is available to get information like this with HTML and PHP but there are lots of options available for you. You'll just have to find out which direction you want to go.

Discord bot website, how to get Guild count

This is what i ended up doing:

I made a project with google firebase and added a firestore database.
I made reading public and writing private. I connected the website to the firestore database just by using vanilla javascript. (After retrieving data i used InnerHTML to insert it to the website)

In my bot's script i added some stuff so it posts the info from the bot to the database.

DJS - Member count excluding bots

There are two potential problems with your code. First, you're not fetching the member collection and are instead interacting with the cache; if I remember correctly the cache usually only shows you online members and not offline ones. Second, the dreaded issue of rate limits.

The first issue is an easy fix. You just need to fetch the members instead of using the cache before all of your setInterval code:

setInterval(async () => {
const memberCount = (await guild.members.fetch()).filter(member => !member.user.bot).size;
const channel = guild.channels.cache.get('959556115383861298');
channel.setName(`╭・Members: ${memberCount.toLocaleString()}`);
console.log('Updating Member Count');
console.log(memberCount);
}, 5000);

That was probably causing the issue you were having. However, there's an additional problem here, one you will run into if you use this code. Rate limits. The normal rate limit for most actions in the Discord API is, if I remember correctly, 10,000 requests per 10 mins. However, the rate limit for changing the name of a channel is just 2 requests per 10 mins (last I checked; I don't know if they've increased this limit since last year). Your code is changing the name of a channel every 5 seconds. In just 10-15 seconds, you'll surpass the rate limit.

I have created channels that keep track of server stats for my bots in the past as well, and ran into this issue when I was doing so. Luckily, I brewed up a way to bypass the rate limit issue back when I was creating my own stats system. Though changing channel names has an incredibly low rate limit, fully creating channels has the normal rate limit of 10,000 requests per 10 mins. Therefore, instead of changing the channel name directly, you could instead: a) clone the channel; b) change the clone's name while creating it; c) set the clone's position in the channel list to the same position as the actual channel; d) delete the original channel. The cloned channel will keep all of the perms, settings, etc of the original. This all assumes, of course, that the original channel isn't a text channel with important messages or such inside, as those messages would all disappear with this method. I used voice channels for my system.

Here's how this bypass could look:

setInterval(async () => {
const memberCount = (await guild.members.fetch()).filter(member => !member.user.bot).size;
const channel = guild.channels.cache.get('959556115383861298');
const pos = channel.position;

const clone = await channel.clone({
name: `╭・Members: ${memberCount.toLocaleString()}`
});
await clone.setPosition(pos);
console.log('Updating Member Count');
console.log(memberCount);

await channel.delete();
}, 5000);

With this approach, rate limits are no longer an issue. Note that this can still be further improved, however. For example, inside the interval you could check if the member count has changed from the previous count, and only modify the channel name if and only if the count has changed. The entire approach potentially could be changed as well; perhaps you could use guildMemberAdd and guildMemberRemove events to track when the member count increases or decreases, and only modify the name when one of those happens (and only if the member being added/removed is not a bot). But whether to add either of those optional potential improvements is up to you.

Note: Please try this code out on a test channel before using it on the actual channel you are using, just to be safe.

Restcord API listGuildMembers

Ok it turns out that Discord never returns email address of a user untill it is pre authenticated by the user. It was returning NUll because user of which I was trying to fetch email was never authenticated my script to fetch email.

how to display total registered admin or users count saperately?

Note :-
you have to use count() function with IF condition in SQL QUERY.

<?php  

$connection = mysqli_connect("localhost","root","","register");
$query = "select
COUNT(if(usertypes='admin',1,NULL)) as admin,
COUNT(if(usertypes='user',1,NULL)) as user
from register";

$query_run = mysqli_query($connection, $query);

while($query_array= mysqli_fetch_array($query_run)){
$admin = $query_array['admin'];
$user = $query_array['user'];

echo '<h4> Total Admin: '.$admin.'</h4>';
echo '<h4> Total user: '.$user.'</h4>';

}
?>

Note:- (1). There is no need to use $row = mysqli_num_rows($query_run);

(2).you have to use while Loop after execution of your sql Query.



Related Topics



Leave a reply



Submit