使用 PHP (Discord.php) 遍历雪花
Looping through a Snowflake with PHP (Discord.php)
这是我的第一个 post,我在这里读了很多书,所以希望我能避免让自己难堪。我已经对该主题进行了大量搜索,但收效甚微,因为我是 PHP
的新手,它也无济于事,而且关于该主题的文档也很少。
PHP
的 Discord API 仅限于您可以请求的某些数据,我正在尝试做的是: 获取成员,检查他们有什么角色,如果有,算上他们。
目前 API 您可以统计公会内的所有成员,但是您不能统计公会内具有特定角色的所有成员。我的最终结论是遍历雪花并自己处理比较。
此代码returns公会的雪花(最多1000):
<?php
$json_options = [
"http" => [
"method" => "GET",
"header" => "Authorization: Bot TOKENREDACTED"
]
];
$json_context = stream_context_create($json_options);
$json_get = file_get_contents('https://discordapp.com/api/guilds/GUILDIDREDACTED/members?limit=1000', false, $json_context);
$json_decode = json_decode($json_get, true);
print_r($json_decode);
?>
我试图遍历的雪花看起来像这样:
Array
(
[0] => Array
(
[nick] => nickname
[user] => Array
(
[username] => username
[discriminator] => 7697
[id] => 123456789012345
[avatar] => 32ad726b873445fff9145e47144a9465
)
[roles] => Array
(
[0] => 123456789012345678
[1] => 123456789012345678
)
[mute] =>
[deaf] =>
[joined_at] => 2018-05-18T07:22:49.562000+00:00
)
[1] => Array (annnd repeat for the next member)
如你所见,雪花在数组方面相当复杂。
我在这里尝试做的是遍历每个数组条目([0]
、[1]
、[2]
等)然后到角色。如果 [user]
具有角色 ID 123456789012345678
(例如),则将该成员添加到要打印的计数中,如果没有匹配项,则它会简单地忽略它并移动到下一个。但我不确定从哪里开始。感谢任何帮助或指导,谢谢。
您可以使用 array-filter and get only the element you need with in-array 然后使用简单的 count
方法计算它们。考虑以下因素:
$arr = array_filter($json_decode, function($e) {return in_array("123456789012345678", $e['roles']);});
echo count($arr);
如果您的 "RoleId" 是动态的,您可以这样做:
$myRole = "123456789012345678";
$arr = array_filter($json_decode, function($e) use ($myRole) {return in_array($myRole, $e['roles']);});
如果你还想显示 username
你可以这样做:
foreach($arr as $e) {echo $e['user']['username'];}
这可能是非常非常糟糕的做法,尤其是对于第一个 post。 @wesley murch 非常感谢你的指导,我现在明白了。另外,请随时回复,以便我将您标记为答案。这是我想与大家分享的工作代码,以及代码作用的以下解释:
<?php
$json_options = [
"http" => [
"method" => "GET",
"header" => "Authorization: Bot Your-Discord-Bot-Token-Here"
]
];
$json_context = stream_context_create($json_options);
$json_get = file_get_contents('https://discordapp.com/api/guilds/your-guild-id-here/members?limit=1000', false, $json_context);
print_r(substr_count ( $json_get ,'Role-Id-To-Look-For'));
?>
此代码将在 API 中查询雪花,然后将雪花转换为字符串,并计算该角色在雪花中出现的次数。在这种情况下,我有 12 个人扮演这个角色,我可以确认它成功地 returned 值 12。在这种情况下,不一定是解析数组的问题,我能够检查雪花作为我正在寻找的 ID 的字符串。该用例的局限性在于它不一定 return 具有该角色的成员的任何详细信息,因此我不会说它完全涵盖了每个用例。
这是我的第一个 post,我在这里读了很多书,所以希望我能避免让自己难堪。我已经对该主题进行了大量搜索,但收效甚微,因为我是 PHP
的新手,它也无济于事,而且关于该主题的文档也很少。
PHP
的 Discord API 仅限于您可以请求的某些数据,我正在尝试做的是: 获取成员,检查他们有什么角色,如果有,算上他们。
目前 API 您可以统计公会内的所有成员,但是您不能统计公会内具有特定角色的所有成员。我的最终结论是遍历雪花并自己处理比较。
此代码returns公会的雪花(最多1000):
<?php
$json_options = [
"http" => [
"method" => "GET",
"header" => "Authorization: Bot TOKENREDACTED"
]
];
$json_context = stream_context_create($json_options);
$json_get = file_get_contents('https://discordapp.com/api/guilds/GUILDIDREDACTED/members?limit=1000', false, $json_context);
$json_decode = json_decode($json_get, true);
print_r($json_decode);
?>
我试图遍历的雪花看起来像这样:
Array
(
[0] => Array
(
[nick] => nickname
[user] => Array
(
[username] => username
[discriminator] => 7697
[id] => 123456789012345
[avatar] => 32ad726b873445fff9145e47144a9465
)
[roles] => Array
(
[0] => 123456789012345678
[1] => 123456789012345678
)
[mute] =>
[deaf] =>
[joined_at] => 2018-05-18T07:22:49.562000+00:00
)
[1] => Array (annnd repeat for the next member)
如你所见,雪花在数组方面相当复杂。
我在这里尝试做的是遍历每个数组条目([0]
、[1]
、[2]
等)然后到角色。如果 [user]
具有角色 ID 123456789012345678
(例如),则将该成员添加到要打印的计数中,如果没有匹配项,则它会简单地忽略它并移动到下一个。但我不确定从哪里开始。感谢任何帮助或指导,谢谢。
您可以使用 array-filter and get only the element you need with in-array 然后使用简单的 count
方法计算它们。考虑以下因素:
$arr = array_filter($json_decode, function($e) {return in_array("123456789012345678", $e['roles']);});
echo count($arr);
如果您的 "RoleId" 是动态的,您可以这样做:
$myRole = "123456789012345678";
$arr = array_filter($json_decode, function($e) use ($myRole) {return in_array($myRole, $e['roles']);});
如果你还想显示 username
你可以这样做:
foreach($arr as $e) {echo $e['user']['username'];}
这可能是非常非常糟糕的做法,尤其是对于第一个 post。 @wesley murch 非常感谢你的指导,我现在明白了。另外,请随时回复,以便我将您标记为答案。这是我想与大家分享的工作代码,以及代码作用的以下解释:
<?php
$json_options = [
"http" => [
"method" => "GET",
"header" => "Authorization: Bot Your-Discord-Bot-Token-Here"
]
];
$json_context = stream_context_create($json_options);
$json_get = file_get_contents('https://discordapp.com/api/guilds/your-guild-id-here/members?limit=1000', false, $json_context);
print_r(substr_count ( $json_get ,'Role-Id-To-Look-For'));
?>
此代码将在 API 中查询雪花,然后将雪花转换为字符串,并计算该角色在雪花中出现的次数。在这种情况下,我有 12 个人扮演这个角色,我可以确认它成功地 returned 值 12。在这种情况下,不一定是解析数组的问题,我能够检查雪花作为我正在寻找的 ID 的字符串。该用例的局限性在于它不一定 return 具有该角色的成员的任何详细信息,因此我不会说它完全涵盖了每个用例。