0

Get a list of users

by
Published Oct 17, 2025
Script discourse Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Discourse = {
3
  apiKey: string;
4
  defaultHost: string;
5
  apiUsername: string;
6
};
7
/**
8
 * Get a list of users
9
 *
10
 */
11
export async function main(
12
  auth: Discourse,
13
  flag: "active" | "new" | "staff" | "suspended" | "blocked" | "suspect",
14
  order:
15
    | "created"
16
    | "last_emailed"
17
    | "seen"
18
    | "username"
19
    | "email"
20
    | "trust_level"
21
    | "days_visited"
22
    | "posts_read"
23
    | "topics_viewed"
24
    | "posts"
25
    | "read_time"
26
    | undefined,
27
  asc: "true" | undefined,
28
  page: string | undefined,
29
  show_emails: string | undefined,
30
  stats: string | undefined,
31
  email: string | undefined,
32
  ip: string | undefined,
33
) {
34
  const url = new URL(`https://${auth.defaultHost}/admin/users/list/${flag}.json`);
35
  for (const [k, v] of [
36
    ["order", order],
37
    ["asc", asc],
38
    ["page", page],
39
    ["show_emails", show_emails],
40
    ["stats", stats],
41
    ["email", email],
42
    ["ip", ip],
43
  ]) {
44
    if (v !== undefined && v !== "" && k !== undefined) {
45
      url.searchParams.append(k, v);
46
    }
47
  }
48
  const response = await fetch(url, {
49
    method: "GET",
50
    headers: {
51
      "API-KEY": auth.apiKey,
52
      "API-USERNAME": auth.apiUsername,
53
    },
54
    body: undefined,
55
  });
56
  if (!response.ok) {
57
    const text = await response.text();
58
    throw new Error(`${response.status} ${text}`);
59
  }
60
  return await response.json();
61
}
62