zad-cli 0.7.2

Command-line interface for zad — connects AI agents to external services (Discord, Slack, Google Calendar, Spotify, Telegram, YouTube Music, 1Password) via scoped service configurations.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
//! `zad discord <verb>` — runtime commands against a configured Discord
//! bot. Credential resolution mirrors `zad service enable discord`: the
//! project-local config wins over the global one, and the matching
//! keychain entry holds the bot token. The project must already have
//! enabled the Discord service.

use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};

use clap::{Args, Subcommand};
use serde::Serialize;

use zad::config::directory::{self as dir, Directory};
use zad::config::{self, DiscordServiceCfg};
use zad::error::{Result, ZadError};
use zad::permissions::attachments::AttachmentInfo;
use zad::secrets::{self, Scope};
use zad::service::discord::permissions::{self as perms, DiscordFunction};
use zad::service::discord::{DiscordHttp, DiscordTransport, DryRunDiscordTransport};
use zad::service::{ChannelId, Target, UserId, default_dry_run_sink};

// ---------------------------------------------------------------------------
// subcommand plumbing
// ---------------------------------------------------------------------------

#[derive(Debug, Args)]
pub struct DiscordArgs {
    #[command(subcommand)]
    pub action: Option<Action>,
}

#[derive(Debug, Subcommand)]
pub enum Action {
    /// Send a message to a channel or DM.
    Send(SendArgs),
    /// Read recent messages from a channel.
    Read(ReadArgs),
    /// List channels in a guild.
    Channels(ChannelsArgs),
    /// Join a thread channel (Discord only allows explicit joins on threads).
    Join(JoinArgs),
    /// Leave a thread channel.
    Leave(LeaveArgs),
    /// Best-effort walk of the bot's visible guilds, channels, and
    /// members, writing a name -> snowflake map to this project's
    /// `directory.toml`. Safe to re-run; preserves hand-authored entries.
    Discover(DiscoverArgs),
    /// Inspect or hand-edit the name -> snowflake directory.
    Directory(DirectoryArgs),
    /// Inspect, scaffold, or dry-run the permissions policy that narrows
    /// what this service may actually do.
    Permissions(PermissionsArgs),
    /// Manage the Discord user ID resolved from the literal `@me` in
    /// `--dm` targets. Show, set (with API validation), or clear.
    #[command(name = "self")]
    SelfCmd(SelfArgs),
}

pub async fn run(args: DiscordArgs) -> Result<()> {
    let action = args
        .action
        .ok_or_else(|| ZadError::Invalid("missing subcommand. Run `zad discord --help`.".into()))?;
    match action {
        Action::Send(a) => run_send(a).await,
        Action::Read(a) => run_read(a).await,
        Action::Channels(a) => run_channels(a).await,
        Action::Join(a) => run_join(a).await,
        Action::Leave(a) => run_leave(a).await,
        Action::Discover(a) => run_discover(a).await,
        Action::Directory(a) => run_directory(a),
        Action::Permissions(a) => run_permissions(a),
        Action::SelfCmd(a) => run_self(a).await,
    }
}

// ---------------------------------------------------------------------------
// send
// ---------------------------------------------------------------------------

#[derive(Debug, Args)]
pub struct SendArgs {
    /// Destination channel ID (snowflake). Mutually exclusive with `--dm`.
    #[arg(long, conflicts_with = "dm")]
    pub channel: Option<String>,

    /// Destination user ID (snowflake) for a direct message. Mutually
    /// exclusive with `--channel`.
    #[arg(long, conflicts_with = "channel")]
    pub dm: Option<String>,

    /// Read the message body from standard input instead of the positional
    /// argument.
    #[arg(long, conflicts_with = "body")]
    pub stdin: bool,

    /// Attach a file to the message. Repeat up to Discord's per-message
    /// cap of 10 to attach multiple files. When at least one `--file` is
    /// given the message body may be empty.
    #[arg(long = "file", value_name = "PATH", action = clap::ArgAction::Append)]
    pub files: Vec<PathBuf>,

    /// Message body. Required unless `--stdin` is set or at least one
    /// `--file` is attached.
    pub body: Option<String>,

    /// Emit machine-readable JSON instead of human-readable text.
    #[arg(long)]
    pub json: bool,

    /// Preview the outgoing call without contacting Discord. Scope and
    /// permission checks still run; no bot token is loaded. Prints what
    /// would have been sent as JSON on stdout.
    #[arg(long)]
    pub dry_run: bool,
}

#[derive(Debug, Serialize)]
struct SendOutput {
    command: &'static str,
    target: &'static str,
    target_id: String,
    message_id: String,
}

async fn run_send(args: SendArgs) -> Result<()> {
    let (cfg, _scope) = effective_config()?;
    let directory = dir::load().unwrap_or_default();
    let context_guild = default_guild_name(&cfg, &directory);
    let permissions = crate::cli::echo::load_effective_or_echo(perms::load_effective)?;
    permissions.check_time(DiscordFunction::Send)?;
    let target = match (&args.channel, &args.dm) {
        (Some(c), None) => {
            let id = resolve_channel(c, &directory, context_guild.as_deref())?;
            permissions.check_send_channel(c, id, &directory)?;
            Target::Channel(ChannelId(id))
        }
        (None, Some(u)) => {
            let id = resolve_user_or_self(u, cfg.self_user_id.as_deref(), &directory)?;
            permissions.check_send_dm(u, id, &directory)?;
            Target::Dm(UserId(id))
        }
        (None, None) => {
            return Err(ZadError::Invalid(
                "missing destination: pass --channel <ID> or --dm <USER_ID>".into(),
            ));
        }
        (Some(_), Some(_)) => unreachable!("clap enforces mutual exclusion"),
    };

    let body = if args.files.is_empty() {
        resolve_body(args.body.as_deref(), args.stdin)?
    } else {
        resolve_body_or_empty(args.body.as_deref(), args.stdin)?
    };
    let len = body.chars().count();
    if len > zad::service::discord::client::DISCORD_MAX_MESSAGE_LEN {
        return Err(ZadError::Invalid(format!(
            "message body is {len} characters; Discord's hard limit is {}",
            zad::service::discord::client::DISCORD_MAX_MESSAGE_LEN
        )));
    }
    if args.files.len() > zad::service::discord::client::DISCORD_MAX_ATTACHMENTS {
        return Err(ZadError::Invalid(format!(
            "{} attachments is above Discord's per-message cap of {}",
            args.files.len(),
            zad::service::discord::client::DISCORD_MAX_ATTACHMENTS
        )));
    }
    permissions.check_send_body(&body)?;

    let infos: Vec<AttachmentInfo> = args
        .files
        .iter()
        .map(|p| {
            AttachmentInfo::probe(p).map_err(|e| {
                ZadError::Invalid(format!("attachment `{}` not readable: {e}", p.display()))
            })
        })
        .collect::<Result<_>>()?;
    permissions.check_send_attachments(&infos)?;

    let http = discord_http_for("messages.send", args.dry_run)?;
    let msg_id = http.send(target.clone(), &body, &args.files).await?;

    // When --dry-run is active the transport already emitted a preview
    // record (human summary via `tracing::info!`, JSON payload on
    // stdout). Skip the trailing "Sent …" / SendOutput print so we
    // never claim success for an operation we didn't actually perform.
    if args.dry_run {
        return Ok(());
    }
    if crate::cli::echo::echo_active() {
        crate::cli::echo::render_and_clear(args.json);
        return Ok(());
    }

    let (kind, tid) = match &target {
        Target::Channel(ChannelId(id)) => ("channel", id.to_string()),
        Target::Dm(UserId(id)) => ("dm", id.to_string()),
    };

    if args.json {
        let out = SendOutput {
            command: "discord.send",
            target: kind,
            target_id: tid,
            message_id: msg_id.0.to_string(),
        };
        println!("{}", serde_json::to_string_pretty(&out).unwrap());
    } else {
        println!("Sent message {} to {kind} {tid}.", msg_id.0);
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// read
// ---------------------------------------------------------------------------

#[derive(Debug, Args)]
pub struct ReadArgs {
    /// Channel ID (snowflake) to read from.
    #[arg(long)]
    pub channel: String,

    /// Maximum number of messages to fetch (1–100). Defaults to 20.
    #[arg(long, default_value_t = 20)]
    pub limit: usize,

    /// Emit machine-readable JSON instead of human-readable text.
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Serialize)]
struct ReadOutput {
    command: &'static str,
    channel: String,
    count: usize,
    messages: Vec<ReadMessage>,
}

#[derive(Debug, Serialize)]
struct ReadMessage {
    id: String,
    author: String,
    body: String,
}

async fn run_read(args: ReadArgs) -> Result<()> {
    if args.limit == 0 || args.limit > 100 {
        return Err(ZadError::Invalid(
            "--limit must be between 1 and 100 (Discord API maximum)".into(),
        ));
    }
    let (cfg, _scope) = effective_config()?;
    let directory = dir::load().unwrap_or_default();
    let context_guild = default_guild_name(&cfg, &directory);
    let permissions = crate::cli::echo::load_effective_or_echo(perms::load_effective)?;
    permissions.check_time(DiscordFunction::Read)?;
    let id = resolve_channel(&args.channel, &directory, context_guild.as_deref())?;
    permissions.check_read_channel(&args.channel, id, &directory)?;
    let channel_id = ChannelId(id);
    let http = discord_http_for("messages.read", false)?;
    let msgs = http.history(channel_id.clone(), args.limit).await?;

    if crate::cli::echo::echo_active() {
        crate::cli::echo::render_and_clear(args.json);
        return Ok(());
    }

    if args.json {
        let out = ReadOutput {
            command: "discord.read",
            channel: channel_id.0.to_string(),
            count: msgs.len(),
            messages: msgs
                .iter()
                .map(|m| ReadMessage {
                    id: m.id.0.to_string(),
                    author: m.author.0.to_string(),
                    body: m.body.clone(),
                })
                .collect(),
        };
        println!("{}", serde_json::to_string_pretty(&out).unwrap());
        return Ok(());
    }

    if msgs.is_empty() {
        println!("(no messages)");
        return Ok(());
    }
    // Discord returns newest-first; print oldest-first so a human reads
    // top-to-bottom in chronological order.
    for m in msgs.iter().rev() {
        println!("[{}] <{}> {}", m.id.0, m.author.0, m.body);
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// channels
// ---------------------------------------------------------------------------

#[derive(Debug, Args)]
pub struct ChannelsArgs {
    /// Guild (server) ID. Defaults to the configured `default_guild` if
    /// unset.
    #[arg(long)]
    pub guild: Option<String>,

    /// Emit machine-readable JSON instead of human-readable text.
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Serialize)]
struct ChannelsOutput {
    command: &'static str,
    guild: String,
    count: usize,
    channels: Vec<ChannelRow>,
}

#[derive(Debug, Serialize)]
struct ChannelRow {
    id: String,
    name: String,
    kind: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    parent: Option<String>,
    position: u16,
}

async fn run_channels(args: ChannelsArgs) -> Result<()> {
    let (cfg, _scope) = effective_config()?;
    let directory = dir::load().unwrap_or_default();
    let permissions = crate::cli::echo::load_effective_or_echo(perms::load_effective)?;
    permissions.check_time(DiscordFunction::Channels)?;
    let guild = resolve_guild_arg(
        args.guild.as_deref(),
        cfg.default_guild.as_deref(),
        &directory,
    )?;
    let guild_input = args
        .guild
        .clone()
        .or_else(|| cfg.default_guild.clone())
        .unwrap_or_else(|| guild.to_string());
    permissions.check_channels_guild(&guild_input, guild, &directory)?;
    let http = discord_http_for("guilds", false)?;
    let channels = http.list_channels(guild).await?;

    if crate::cli::echo::echo_active() {
        crate::cli::echo::render_and_clear(args.json);
        return Ok(());
    }

    if args.json {
        let rows: Vec<ChannelRow> = channels
            .iter()
            .map(|c| ChannelRow {
                id: c.id.0.to_string(),
                name: c.name.clone(),
                kind: c.kind.clone(),
                parent: c.parent.as_ref().map(|p| p.0.to_string()),
                position: c.position,
            })
            .collect();
        let out = ChannelsOutput {
            command: "discord.channels",
            guild: guild.to_string(),
            count: rows.len(),
            channels: rows,
        };
        println!("{}", serde_json::to_string_pretty(&out).unwrap());
        return Ok(());
    }

    if channels.is_empty() {
        println!("(no channels in guild {guild})");
        return Ok(());
    }
    println!("{:<20}  {:<14}  NAME", "ID", "KIND");
    for c in &channels {
        println!("{:<20}  {:<14}  {}", c.id.0, c.kind, c.name);
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// join / leave (thread members)
// ---------------------------------------------------------------------------

#[derive(Debug, Args)]
pub struct JoinArgs {
    /// Channel ID (snowflake). Must refer to a thread channel.
    #[arg(long)]
    pub channel: String,

    /// Emit machine-readable JSON instead of human-readable text.
    #[arg(long)]
    pub json: bool,

    /// Preview the outgoing call without contacting Discord. Scope and
    /// permission checks still run; no bot token is loaded.
    #[arg(long)]
    pub dry_run: bool,
}

#[derive(Debug, Args)]
pub struct LeaveArgs {
    /// Channel ID (snowflake). Must refer to a thread channel.
    #[arg(long)]
    pub channel: String,

    /// Emit machine-readable JSON instead of human-readable text.
    #[arg(long)]
    pub json: bool,

    /// Preview the outgoing call without contacting Discord. Scope and
    /// permission checks still run; no bot token is loaded.
    #[arg(long)]
    pub dry_run: bool,
}

#[derive(Debug, Serialize)]
struct MembershipOutput {
    command: &'static str,
    channel: String,
}

async fn run_join(args: JoinArgs) -> Result<()> {
    let (cfg, _scope) = effective_config()?;
    let directory = dir::load().unwrap_or_default();
    let context_guild = default_guild_name(&cfg, &directory);
    let permissions = crate::cli::echo::load_effective_or_echo(perms::load_effective)?;
    permissions.check_time(DiscordFunction::Join)?;
    let id = resolve_channel(&args.channel, &directory, context_guild.as_deref())?;
    permissions.check_join_channel(&args.channel, id, &directory)?;
    let channel = ChannelId(id);
    let http = discord_http_for("guilds", args.dry_run)?;
    http.join_channel(channel.clone()).await?;
    if args.dry_run {
        return Ok(());
    }
    if crate::cli::echo::echo_active() {
        crate::cli::echo::render_and_clear(args.json);
        return Ok(());
    }
    if args.json {
        let out = MembershipOutput {
            command: "discord.join",
            channel: channel.0.to_string(),
        };
        println!("{}", serde_json::to_string_pretty(&out).unwrap());
    } else {
        println!("Joined channel {}.", channel.0);
    }
    Ok(())
}

async fn run_leave(args: LeaveArgs) -> Result<()> {
    let (cfg, _scope) = effective_config()?;
    let directory = dir::load().unwrap_or_default();
    let context_guild = default_guild_name(&cfg, &directory);
    let permissions = crate::cli::echo::load_effective_or_echo(perms::load_effective)?;
    permissions.check_time(DiscordFunction::Leave)?;
    let id = resolve_channel(&args.channel, &directory, context_guild.as_deref())?;
    permissions.check_leave_channel(&args.channel, id, &directory)?;
    let channel = ChannelId(id);
    let http = discord_http_for("guilds", args.dry_run)?;
    http.leave_channel(channel.clone()).await?;
    if args.dry_run {
        return Ok(());
    }
    if crate::cli::echo::echo_active() {
        crate::cli::echo::render_and_clear(args.json);
        return Ok(());
    }
    if args.json {
        let out = MembershipOutput {
            command: "discord.leave",
            channel: channel.0.to_string(),
        };
        println!("{}", serde_json::to_string_pretty(&out).unwrap());
    } else {
        println!("Left channel {}.", channel.0);
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// credential / config plumbing
// ---------------------------------------------------------------------------

enum EffectiveScope {
    Global,
    Local(String),
}

fn effective_config() -> Result<(DiscordServiceCfg, EffectiveScope)> {
    let project_path = config::path::project_config_path()?;
    let project_cfg = config::load_from(&project_path)?;
    if !project_cfg.has_service("discord") {
        return Err(ZadError::Invalid(format!(
            "discord is not enabled for this project ({}). \
             Run `zad service enable discord` first.",
            project_path.display()
        )));
    }

    let slug = config::path::project_slug()?;
    let local_path = config::path::project_service_config_path_for(&slug, "discord")?;
    if let Some(cfg) = config::load_flat::<DiscordServiceCfg>(&local_path)? {
        return Ok((cfg, EffectiveScope::Local(slug)));
    }
    let global_path = config::path::global_service_config_path("discord")?;
    if let Some(cfg) = config::load_flat::<DiscordServiceCfg>(&global_path)? {
        return Ok((cfg, EffectiveScope::Global));
    }
    Err(ZadError::Invalid(format!(
        "no Discord credentials found for this project.\n\
         looked in:\n  {}\n  {}",
        local_path.display(),
        global_path.display()
    )))
}

fn load_token(scope: &EffectiveScope) -> Result<String> {
    let account = match scope {
        EffectiveScope::Global => secrets::account("discord", "bot", Scope::Global),
        EffectiveScope::Local(slug) => secrets::account("discord", "bot", Scope::Project(slug)),
    };
    secrets::load(&account)?.ok_or_else(|| {
        ZadError::Invalid(format!(
            "bot token missing from keychain (account `{account}`). \
             Re-run `zad service create discord` to reinstall it."
        ))
    })
}

/// Resolve config + token + scope set into a ready-to-call client, and
/// fail fast with [`ZadError::ScopeDenied`] if `required` isn't declared.
/// The fail-fast scope check happens *before* the keychain read so a
/// denied op never touches secrets; [`DiscordHttp`] still guards the
/// same scope internally, which covers library callers (`DiscordService`)
/// that bypass this helper.
///
/// When `dry_run` is `true` the scope check still runs (so preview
/// respects the caller's policy boundary), but the keychain read is
/// skipped and a [`DryRunDiscordTransport`] is returned instead of a
/// live client. That lets `--dry-run` work before the operator has
/// configured a bot, and guarantees no token is ever loaded into memory
/// for a preview.
fn discord_http_for(required: &'static str, dry_run: bool) -> Result<Box<dyn DiscordTransport>> {
    let (cfg, scope) = effective_config()?;
    let config_path = match &scope {
        EffectiveScope::Local(slug) => {
            config::path::project_service_config_path_for(slug, "discord")?
        }
        EffectiveScope::Global => config::path::global_service_config_path("discord")?,
    };
    let scopes: std::collections::BTreeSet<String> = cfg.scopes.iter().cloned().collect();
    if !scopes.contains(required) {
        return Err(ZadError::ScopeDenied {
            service: "discord",
            scope: required,
            config_path,
        });
    }
    if dry_run || crate::cli::echo::echo_active() {
        let sink = if crate::cli::echo::echo_active() {
            crate::cli::echo::dry_run_sink_for_echo()
        } else {
            default_dry_run_sink()
        };
        return Ok(Box::new(DryRunDiscordTransport::new(sink)));
    }
    let token = load_token(&scope)?;
    Ok(Box::new(DiscordHttp::new(&token, scopes, config_path)))
}

fn resolve_guild_arg(
    flag: Option<&str>,
    default: Option<&str>,
    directory: &Directory,
) -> Result<u64> {
    let raw = flag.or(default).ok_or_else(|| {
        ZadError::Invalid(
            "no guild specified: pass --guild <ID|name> or set `default_guild` in the config"
                .into(),
        )
    })?;
    directory.resolve_guild(raw).ok_or_else(|| {
        ZadError::Invalid(format!(
            "--guild `{raw}` is neither a numeric snowflake nor a known directory entry. \
             Run `zad discord discover` or map it manually with \
             `zad discord directory set guild {raw} <id>`."
        ))
    })
}

fn resolve_channel(input: &str, directory: &Directory, context_guild: Option<&str>) -> Result<u64> {
    directory
        .resolve_channel(input, context_guild)
        .ok_or_else(|| {
            let key = input.strip_prefix('#').unwrap_or(input);
            ZadError::Invalid(format!(
                "--channel `{input}` is neither a numeric snowflake nor a known directory entry. \
             Run `zad discord discover` or map it manually with \
             `zad discord directory set channel {key} <id>`."
            ))
        })
}

fn resolve_user(input: &str, directory: &Directory) -> Result<u64> {
    directory.resolve_user(input).ok_or_else(|| {
        let key = input.strip_prefix('@').unwrap_or(input);
        ZadError::Invalid(format!(
            "--dm `{input}` is neither a numeric snowflake nor a known directory entry. \
             Run `zad discord discover` or map it manually with \
             `zad discord directory set user {key} <id>`."
        ))
    })
}

fn resolve_user_or_self(
    input: &str,
    self_user_id: Option<&str>,
    directory: &Directory,
) -> Result<u64> {
    if input.eq_ignore_ascii_case("@me") {
        return match self_user_id {
            Some(id) => id.parse::<u64>().map_err(|_| {
                ZadError::Invalid(format!(
                    "stored self-user id `{id}` is not a numeric snowflake; \
                     run `zad discord self set <id>` with a valid id"
                ))
            }),
            None => Err(ZadError::Invalid(
                "`@me` has no self-user configured. Run \
                 `zad discord self set <id>` with your Discord user ID \
                 (Settings → Advanced → Developer Mode → right-click \
                 yourself → \"Copy User ID\")."
                    .into(),
            )),
        };
    }
    resolve_user(input, directory)
}

fn parse_snowflake(v: &str, field: &'static str) -> Result<u64> {
    v.parse::<u64>().map_err(|_| {
        ZadError::Invalid(format!(
            "{field} must be a numeric Discord snowflake, got `{v}`"
        ))
    })
}

fn default_guild_name(cfg: &DiscordServiceCfg, directory: &Directory) -> Option<String> {
    let raw = cfg.default_guild.as_deref()?;
    if let Ok(id) = raw.parse::<u64>() {
        return directory.guild_name_for(id).map(str::to_owned);
    }
    if directory.guilds.contains_key(raw) {
        return Some(raw.to_owned());
    }
    None
}

// ---------------------------------------------------------------------------
// discover
// ---------------------------------------------------------------------------

#[derive(Debug, Args)]
pub struct DiscoverArgs {
    /// Scope discovery to a single guild (by ID or known name). Without
    /// this flag, every guild the bot can see is walked.
    #[arg(long)]
    pub guild: Option<String>,

    /// Skip the member-listing phase. Use this when the bot doesn't have
    /// the privileged `GUILD_MEMBERS` intent enabled and you want to
    /// suppress the warning it would otherwise emit.
    #[arg(long)]
    pub skip_members: bool,

    /// Emit machine-readable JSON instead of a human-readable summary.
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Serialize)]
struct DiscoverOutput {
    command: &'static str,
    guilds: usize,
    channels: usize,
    users: usize,
    warnings: Vec<String>,
}

async fn run_discover(args: DiscoverArgs) -> Result<()> {
    let permissions = crate::cli::echo::load_effective_or_echo(perms::load_effective)?;
    permissions.check_time(DiscordFunction::Discover)?;
    // Discover walks the directory by issuing many read calls and writes
    // to disk afterwards. None of that should happen against a file we
    // can't trust, so bail out early with the echo envelope before the
    // first network call.
    if crate::cli::echo::echo_active() {
        crate::cli::echo::render_and_clear(args.json);
        return Ok(());
    }
    let http = discord_http_for("guilds", false)?;
    let mut directory = dir::load().unwrap_or_default();
    let mut warnings: Vec<String> = vec![];

    let guilds = match http.list_guilds().await {
        Ok(g) => g,
        Err(e) => {
            warnings.push(format!("list guilds: {e}"));
            vec![]
        }
    };

    let scoped: Option<u64> = args
        .guild
        .as_deref()
        .map(|raw| -> Result<u64> {
            directory.resolve_guild(raw).ok_or_else(|| {
                ZadError::Invalid(format!(
                    "--guild `{raw}` is not numeric and not in the directory; \
                     run `zad discord discover` without --guild first, or pass an ID."
                ))
            })
        })
        .transpose()?;

    // Filter the walk to guilds the operator actually allowed discovery
    // into. `guilds.allow`/`guilds.deny` for the `discover` block narrows
    // the walk; silently skipping a denied guild is the right shape
    // because `discover` is already best-effort.
    let targets: Vec<_> = match scoped {
        Some(id) => guilds.iter().filter(|g| g.id == id).cloned().collect(),
        None => guilds
            .iter()
            .filter(|g| {
                permissions
                    .check_discover_guild(&g.name, g.id, &directory)
                    .is_ok()
            })
            .cloned()
            .collect(),
    };
    if let Some(id) = scoped
        && let Some(g) = guilds.iter().find(|g| g.id == id)
    {
        permissions.check_discover_guild(&g.name, g.id, &directory)?;
    }

    for g in &guilds {
        directory.guilds.insert(g.name.clone(), g.id.to_string());
    }

    for g in &targets {
        match http.list_channels(g.id).await {
            Ok(chans) => {
                for c in chans {
                    let qualified = format!("{}/{}", g.name, c.name);
                    directory.channels.insert(qualified, c.id.0.to_string());
                    // Bare-name convenience key. If multiple guilds share
                    // a channel name (e.g. `general`), the last one
                    // written wins; the qualified key always
                    // disambiguates when the caller needs it to.
                    directory.channels.insert(c.name, c.id.0.to_string());
                }
            }
            Err(e) => warnings.push(format!("channels for `{}`: {e}", g.name)),
        }

        if args.skip_members {
            continue;
        }
        match http.list_members(g.id, 1000).await {
            Ok(members) => {
                for m in members {
                    directory
                        .users
                        .insert(m.display_name.clone(), m.id.0.to_string());
                }
            }
            Err(e) => warnings.push(format!(
                "members for `{}` (needs GUILD_MEMBERS privileged intent): {e}",
                g.name
            )),
        }
    }

    directory.generated_at_unix = Some(
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0),
    );
    dir::save(&directory)?;

    let guilds_n = directory.guilds.len();
    let channels_n = directory.channels.len();
    let users_n = directory.users.len();

    if args.json {
        let out = DiscoverOutput {
            command: "discord.discover",
            guilds: guilds_n,
            channels: channels_n,
            users: users_n,
            warnings: warnings.clone(),
        };
        println!("{}", serde_json::to_string_pretty(&out).unwrap());
    } else {
        println!(
            "Wrote directory: {guilds_n} guilds, {channels_n} channel entries, {users_n} users."
        );
        for w in &warnings {
            crate::output::warn(w);
        }
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// directory
// ---------------------------------------------------------------------------

#[derive(Debug, Args)]
pub struct DirectoryArgs {
    #[command(subcommand)]
    pub action: Option<DirectoryAction>,

    /// When no subcommand is given, print the directory as JSON.
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Subcommand)]
pub enum DirectoryAction {
    /// Upsert a name -> snowflake mapping. `<kind>` is one of
    /// `guild`, `channel`, or `user`. Channel keys may include a
    /// `guild/channel` qualifier.
    Set(DirectorySetArgs),
    /// Remove a single mapping. Silent no-op if the key is absent.
    Remove(DirectoryRemoveArgs),
    /// Wipe every entry. Use with `--force`.
    Clear(DirectoryClearArgs),
}

#[derive(Debug, Args)]
pub struct DirectorySetArgs {
    /// One of `guild`, `channel`, `user`.
    pub kind: DirectoryKind,
    /// Human-readable name to map from.
    pub name: String,
    /// Numeric snowflake to map to.
    pub id: String,
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct DirectoryRemoveArgs {
    pub kind: DirectoryKind,
    pub name: String,
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct DirectoryClearArgs {
    #[arg(long)]
    pub force: bool,
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Clone, Copy, clap::ValueEnum)]
pub enum DirectoryKind {
    Guild,
    Channel,
    User,
}

#[derive(Debug, Serialize)]
struct DirectoryOutput<'a> {
    command: &'static str,
    path: String,
    generated_at_unix: Option<u64>,
    guilds: &'a std::collections::BTreeMap<String, String>,
    channels: &'a std::collections::BTreeMap<String, String>,
    users: &'a std::collections::BTreeMap<String, String>,
}

#[derive(Debug, Serialize)]
struct DirectoryMutation {
    command: &'static str,
    kind: &'static str,
    name: String,
    id: Option<String>,
    removed: bool,
}

fn require_discord_enabled() -> Result<()> {
    let project_path = config::path::project_config_path()?;
    let project_cfg = config::load_from(&project_path)?;
    if !project_cfg.has_service("discord") {
        return Err(ZadError::Invalid(format!(
            "discord is not enabled for this project ({}). \
             Run `zad service enable discord` first.",
            project_path.display()
        )));
    }
    Ok(())
}

fn kind_as_str(k: DirectoryKind) -> &'static str {
    match k {
        DirectoryKind::Guild => "guild",
        DirectoryKind::Channel => "channel",
        DirectoryKind::User => "user",
    }
}

fn run_directory(args: DirectoryArgs) -> Result<()> {
    require_discord_enabled()?;
    match args.action {
        None => run_directory_list(args.json),
        Some(DirectoryAction::Set(a)) => run_directory_set(a),
        Some(DirectoryAction::Remove(a)) => run_directory_remove(a),
        Some(DirectoryAction::Clear(a)) => run_directory_clear(a),
    }
}

fn run_directory_list(json: bool) -> Result<()> {
    let path = dir::path_current()?;
    let directory = dir::load_from(&path)?;
    if json {
        let out = DirectoryOutput {
            command: "discord.directory",
            path: path.display().to_string(),
            generated_at_unix: directory.generated_at_unix,
            guilds: &directory.guilds,
            channels: &directory.channels,
            users: &directory.users,
        };
        println!("{}", serde_json::to_string_pretty(&out).unwrap());
        return Ok(());
    }
    if directory.total() == 0 {
        println!("(empty) {}", path.display());
        println!("Run `zad discord discover` to populate it.");
        return Ok(());
    }
    println!("# {}", path.display());
    if !directory.guilds.is_empty() {
        println!("\n[guilds]");
        for (n, id) in &directory.guilds {
            println!("  {n:<24}  {id}");
        }
    }
    if !directory.channels.is_empty() {
        println!("\n[channels]");
        for (n, id) in &directory.channels {
            println!("  {n:<40}  {id}");
        }
    }
    if !directory.users.is_empty() {
        println!("\n[users]");
        for (n, id) in &directory.users {
            println!("  {n:<24}  {id}");
        }
    }
    Ok(())
}

fn run_directory_set(args: DirectorySetArgs) -> Result<()> {
    let id = parse_snowflake(&args.id, "<id>")?;
    let path = dir::path_current()?;
    let mut directory = dir::load_from(&path)?;
    let bucket = match args.kind {
        DirectoryKind::Guild => &mut directory.guilds,
        DirectoryKind::Channel => &mut directory.channels,
        DirectoryKind::User => &mut directory.users,
    };
    bucket.insert(args.name.clone(), id.to_string());
    dir::save_to(&path, &directory)?;

    if args.json {
        let out = DirectoryMutation {
            command: "discord.directory.set",
            kind: kind_as_str(args.kind),
            name: args.name,
            id: Some(id.to_string()),
            removed: false,
        };
        println!("{}", serde_json::to_string_pretty(&out).unwrap());
    } else {
        println!(
            "Mapped {} `{}` -> {id} in {}.",
            kind_as_str(args.kind),
            args.name,
            path.display()
        );
    }
    Ok(())
}

fn run_directory_remove(args: DirectoryRemoveArgs) -> Result<()> {
    let path = dir::path_current()?;
    let mut directory = dir::load_from(&path)?;
    let bucket = match args.kind {
        DirectoryKind::Guild => &mut directory.guilds,
        DirectoryKind::Channel => &mut directory.channels,
        DirectoryKind::User => &mut directory.users,
    };
    let removed = bucket.remove(&args.name).is_some();
    if removed {
        dir::save_to(&path, &directory)?;
    }

    if args.json {
        let out = DirectoryMutation {
            command: "discord.directory.remove",
            kind: kind_as_str(args.kind),
            name: args.name,
            id: None,
            removed,
        };
        println!("{}", serde_json::to_string_pretty(&out).unwrap());
    } else if removed {
        println!(
            "Removed {} `{}` from {}.",
            kind_as_str(args.kind),
            args.name,
            path.display()
        );
    } else {
        println!("No {} entry named `{}`.", kind_as_str(args.kind), args.name);
    }
    Ok(())
}

fn run_directory_clear(args: DirectoryClearArgs) -> Result<()> {
    if !args.force {
        return Err(ZadError::Invalid(
            "refusing to clear the directory without --force".into(),
        ));
    }
    let path = dir::path_current()?;
    let directory = Directory::default();
    dir::save_to(&path, &directory)?;
    if args.json {
        println!(
            "{}",
            serde_json::to_string_pretty(&serde_json::json!({
                "command": "discord.directory.clear",
                "path": path.display().to_string(),
            }))
            .unwrap()
        );
    } else {
        println!("Cleared {}.", path.display());
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// permissions — inspect / scaffold / dry-run the permissions policy
// ---------------------------------------------------------------------------

#[derive(Debug, Args)]
pub struct PermissionsArgs {
    #[command(subcommand)]
    pub action: Option<PermissionsAction>,

    /// When no subcommand is given, behave like `show`.
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Subcommand)]
pub enum PermissionsAction {
    /// Print the effective policy (global + local) for this project.
    Show(PermissionsShowArgs),
    /// Write a starter `permissions.toml` at the selected scope.
    Init(PermissionsInitArgs),
    /// Print the paths considered for this project, in precedence order.
    Path(PermissionsPathArgs),
    /// Dry-run: ask whether a proposed action would be admitted *without*
    /// hitting Discord. Useful for agents that want to pre-flight.
    Check(PermissionsCheckArgs),
    /// Staged-commit workflow: queue mutations in a `.pending` file and
    /// only sign on `commit`. See [`cli::permissions`].
    #[command(flatten)]
    Staging(crate::cli::permissions::StagingAction),
}

#[derive(Debug, Args)]
pub struct PermissionsShowArgs {
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct PermissionsInitArgs {
    /// Write to the project-local `permissions.toml`. Default is global.
    #[arg(long)]
    pub local: bool,

    /// Overwrite any existing file at that scope.
    #[arg(long)]
    pub force: bool,

    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct PermissionsPathArgs {
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct PermissionsCheckArgs {
    /// Function to check: `send`, `read`, `channels`, `join`, `leave`,
    /// `discover`, `manage`.
    #[arg(long)]
    pub function: String,

    /// Channel name or snowflake to test against the channel list for
    /// `send` / `read` / `join` / `leave`.
    #[arg(long, conflicts_with = "user")]
    pub channel: Option<String>,

    /// User name or snowflake to test against the DM list for `send`.
    #[arg(long, conflicts_with = "channel")]
    pub user: Option<String>,

    /// Guild name or snowflake to test against the guild list for
    /// `channels` / `discover`.
    #[arg(long)]
    pub guild: Option<String>,

    /// Body to test against `content` rules (applies only to `send`).
    #[arg(long)]
    pub body: Option<String>,

    #[arg(long)]
    pub json: bool,
}

fn run_permissions(args: PermissionsArgs) -> Result<()> {
    match args.action {
        None => run_permissions_show(PermissionsShowArgs { json: args.json }),
        Some(PermissionsAction::Show(a)) => run_permissions_show(a),
        Some(PermissionsAction::Init(a)) => run_permissions_init(a),
        Some(PermissionsAction::Path(a)) => run_permissions_path(a),
        Some(PermissionsAction::Check(a)) => run_permissions_check(a),
        Some(PermissionsAction::Staging(a)) => {
            crate::cli::permissions::run::<perms::PermissionsService>(a)
        }
    }
}

#[derive(Debug, Serialize)]
struct PermissionsShowOutput {
    command: &'static str,
    global: PermissionsScopeBlock,
    local: PermissionsScopeBlock,
}

#[derive(Debug, Serialize)]
struct PermissionsScopeBlock {
    path: String,
    present: bool,
}

fn run_permissions_show(args: PermissionsShowArgs) -> Result<()> {
    let global_p = perms::global_path()?;
    let local_p = perms::local_path_current()?;
    let global_present = global_p.exists();
    let local_present = local_p.exists();

    // Pre-load to surface any compile errors up front, before printing.
    let effective = perms::load_effective()?;
    let _ = effective;

    if args.json {
        let out = PermissionsShowOutput {
            command: "discord.permissions.show",
            global: PermissionsScopeBlock {
                path: global_p.display().to_string(),
                present: global_present,
            },
            local: PermissionsScopeBlock {
                path: local_p.display().to_string(),
                present: local_present,
            },
        };
        println!("{}", serde_json::to_string_pretty(&out).unwrap());
        return Ok(());
    }

    println!("# permissions");
    println!(
        "  global : {} ({})",
        global_p.display(),
        if global_present {
            "present"
        } else {
            "not present (no restrictions at this scope)"
        }
    );
    println!(
        "  local  : {} ({})",
        local_p.display(),
        if local_present {
            "present"
        } else {
            "not present (no restrictions at this scope)"
        }
    );
    println!();
    if !global_present && !local_present {
        println!("No permission files found. Every declared scope is currently unrestricted.");
        println!("Run `zad discord permissions init` to scaffold a starter policy.");
        return Ok(());
    }
    for p in [&global_p, &local_p] {
        if !p.exists() {
            continue;
        }
        println!("## {}", p.display());
        match std::fs::read_to_string(p) {
            Ok(body) => {
                for line in body.lines() {
                    println!("  {line}");
                }
            }
            Err(e) => println!("  (failed to read: {e})"),
        }
        println!();
    }
    Ok(())
}

#[derive(Debug, Serialize)]
struct PermissionsInitOutput {
    command: &'static str,
    scope: &'static str,
    path: String,
    written: bool,
}

fn run_permissions_init(args: PermissionsInitArgs) -> Result<()> {
    let (path, scope) = if args.local {
        (perms::local_path_current()?, "local")
    } else {
        (perms::global_path()?, "global")
    };
    if path.exists() && !args.force {
        return Err(ZadError::Invalid(format!(
            "permissions file already exists at {}. Pass --force to overwrite.",
            path.display()
        )));
    }
    let template = perms::starter_template();
    let key = zad::permissions::signing::load_or_create_from_keychain()?;
    zad::permissions::signing::write_public_key_cache(&key)?;
    perms::save_file(&path, &template, &key)?;
    if args.json {
        let out = PermissionsInitOutput {
            command: "discord.permissions.init",
            scope,
            path: path.display().to_string(),
            written: true,
        };
        println!("{}", serde_json::to_string_pretty(&out).unwrap());
    } else {
        println!("Wrote starter permissions ({scope}): {}", path.display());
        println!("Signed with key {}.", key.fingerprint());
        println!("Review it; the defaults deny admin-like channels and channels.manage.");
    }
    Ok(())
}

#[derive(Debug, Serialize)]
struct PermissionsPathOutput {
    command: &'static str,
    global: String,
    local: String,
}

fn run_permissions_path(args: PermissionsPathArgs) -> Result<()> {
    let global_p = perms::global_path()?;
    let local_p = perms::local_path_current()?;
    if args.json {
        let out = PermissionsPathOutput {
            command: "discord.permissions.path",
            global: global_p.display().to_string(),
            local: local_p.display().to_string(),
        };
        println!("{}", serde_json::to_string_pretty(&out).unwrap());
    } else {
        println!("{}", global_p.display());
        println!("{}", local_p.display());
    }
    Ok(())
}

#[derive(Debug, Serialize)]
struct PermissionsCheckOutput {
    command: &'static str,
    function: String,
    allowed: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    reason: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    config_path: Option<String>,
}

fn run_permissions_check(args: PermissionsCheckArgs) -> Result<()> {
    let function = parse_function(&args.function)?;
    let permissions = perms::load_effective()?;
    let directory = dir::load().unwrap_or_default();

    let mut outcome: Result<()> = Ok(());
    outcome = outcome.and_then(|()| permissions.check_time(function));

    if outcome.is_ok() {
        outcome = match (function, &args.channel, &args.user, &args.guild) {
            (DiscordFunction::Send, Some(c), None, _) => {
                let id = directory.resolve_channel(c, None).unwrap_or(0);
                permissions.check_send_channel(c, id, &directory)
            }
            (DiscordFunction::Send, None, Some(u), _) => {
                let id = directory.resolve_user(u).unwrap_or(0);
                permissions.check_send_dm(u, id, &directory)
            }
            (DiscordFunction::Read, Some(c), None, _) => {
                let id = directory.resolve_channel(c, None).unwrap_or(0);
                permissions.check_read_channel(c, id, &directory)
            }
            (DiscordFunction::Channels, _, _, Some(g)) => {
                let id = directory.resolve_guild(g).unwrap_or(0);
                permissions.check_channels_guild(g, id, &directory)
            }
            (DiscordFunction::Join, Some(c), None, _) => {
                let id = directory.resolve_channel(c, None).unwrap_or(0);
                permissions.check_join_channel(c, id, &directory)
            }
            (DiscordFunction::Leave, Some(c), None, _) => {
                let id = directory.resolve_channel(c, None).unwrap_or(0);
                permissions.check_leave_channel(c, id, &directory)
            }
            (DiscordFunction::Discover, _, _, Some(g)) => {
                let id = directory.resolve_guild(g).unwrap_or(0);
                permissions.check_discover_guild(g, id, &directory)
            }
            _ => Ok(()),
        };
    }

    if outcome.is_ok()
        && function == DiscordFunction::Send
        && let Some(body) = &args.body
    {
        outcome = permissions.check_send_body(body);
    }

    let (allowed, reason, config_path) = match outcome {
        Ok(()) => (true, None, None),
        Err(ZadError::PermissionDenied {
            reason,
            config_path,
            ..
        }) => (false, Some(reason), Some(config_path.display().to_string())),
        Err(e) => return Err(e),
    };

    if args.json {
        let out = PermissionsCheckOutput {
            command: "discord.permissions.check",
            function: args.function.clone(),
            allowed,
            reason,
            config_path,
        };
        println!("{}", serde_json::to_string_pretty(&out).unwrap());
    } else if allowed {
        println!("allow");
    } else {
        println!(
            "deny — {}",
            reason.as_deref().unwrap_or("unspecified reason")
        );
        if let Some(p) = &config_path {
            println!("  config: {p}");
        }
    }
    if !allowed {
        std::process::exit(1);
    }
    Ok(())
}

fn parse_function(name: &str) -> Result<DiscordFunction> {
    match name {
        "send" => Ok(DiscordFunction::Send),
        "read" => Ok(DiscordFunction::Read),
        "channels" => Ok(DiscordFunction::Channels),
        "join" => Ok(DiscordFunction::Join),
        "leave" => Ok(DiscordFunction::Leave),
        "discover" => Ok(DiscordFunction::Discover),
        "manage" => Ok(DiscordFunction::Manage),
        other => Err(ZadError::Invalid(format!(
            "unknown function `{other}`. Expected one of: send, read, channels, join, leave, discover, manage."
        ))),
    }
}

fn resolve_body(positional: Option<&str>, from_stdin: bool) -> Result<String> {
    resolve_body_inner(positional, from_stdin, false)
}

/// Same as [`resolve_body`] but tolerates an empty result when the
/// caller has attachments to send alongside (Discord accepts an empty
/// `content` as long as at least one file is attached).
fn resolve_body_or_empty(positional: Option<&str>, from_stdin: bool) -> Result<String> {
    resolve_body_inner(positional, from_stdin, true)
}

fn resolve_body_inner(
    positional: Option<&str>,
    from_stdin: bool,
    allow_empty: bool,
) -> Result<String> {
    if from_stdin {
        use std::io::Read;
        let mut buf = String::new();
        std::io::stdin().read_to_string(&mut buf).map_err(|e| {
            ZadError::Invalid(format!("failed to read message body from stdin: {e}"))
        })?;
        let trimmed = buf.trim_end_matches(['\n', '\r']).to_string();
        if trimmed.is_empty() && !allow_empty {
            return Err(ZadError::Invalid("message body is empty (stdin)".into()));
        }
        return Ok(trimmed);
    }
    match positional {
        Some(b) if !b.is_empty() => Ok(b.to_string()),
        Some(_) if allow_empty => Ok(String::new()),
        None if allow_empty => Ok(String::new()),
        _ => Err(ZadError::Invalid(
            "missing message body: pass it as a positional arg, --stdin, or attach at least one --file".into(),
        )),
    }
}

// ---------------------------------------------------------------------------
// self — manage the `@me` resolution target
// ---------------------------------------------------------------------------

#[derive(Debug, Args)]
pub struct SelfArgs {
    #[command(subcommand)]
    pub action: Option<SelfAction>,

    /// When no subcommand is given, behave like `show`.
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Subcommand)]
pub enum SelfAction {
    /// Print the stored self-user ID (or note that it's not set).
    Show(SelfShowArgs),
    /// Validate the supplied snowflake against Discord and store it.
    Set(SelfSetArgs),
    /// Clear the stored self-user ID.
    Clear(SelfClearArgs),
}

#[derive(Debug, Args)]
pub struct SelfShowArgs {
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct SelfSetArgs {
    /// Your Discord user ID (numeric snowflake).
    pub user_id: String,
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct SelfClearArgs {
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Serialize)]
struct SelfOutput {
    command: &'static str,
    self_user_id: Option<String>,
}

async fn run_self(args: SelfArgs) -> Result<()> {
    match args.action {
        None => run_self_show(SelfShowArgs { json: args.json }),
        Some(SelfAction::Show(a)) => run_self_show(a),
        Some(SelfAction::Set(a)) => run_self_set(a).await,
        Some(SelfAction::Clear(a)) => run_self_clear(a),
    }
}

fn run_self_show(args: SelfShowArgs) -> Result<()> {
    let (cfg, _scope) = effective_config()?;
    emit_self(args.json, "discord.self.show", cfg.self_user_id)
}

async fn run_self_set(args: SelfSetArgs) -> Result<()> {
    let (mut cfg, scope) = effective_config()?;
    let token = load_token(&scope)?;
    let resolved =
        crate::cli::service_discord::validate_self_user(&token, args.user_id.trim()).await?;
    cfg.self_user_id = Some(resolved);
    save_effective_config(&cfg, &scope)?;
    emit_self(args.json, "discord.self.set", cfg.self_user_id)
}

fn run_self_clear(args: SelfClearArgs) -> Result<()> {
    let (mut cfg, scope) = effective_config()?;
    cfg.self_user_id = None;
    save_effective_config(&cfg, &scope)?;
    emit_self(args.json, "discord.self.clear", None)
}

fn emit_self(json: bool, command: &'static str, self_user_id: Option<String>) -> Result<()> {
    if json {
        println!(
            "{}",
            serde_json::to_string_pretty(&SelfOutput {
                command,
                self_user_id
            })
            .unwrap()
        );
    } else {
        match self_user_id {
            Some(id) => println!("self user id: {id}"),
            None => println!("self user id: not configured"),
        }
    }
    Ok(())
}

fn save_effective_config(cfg: &DiscordServiceCfg, scope: &EffectiveScope) -> Result<()> {
    let path = match scope {
        EffectiveScope::Local(slug) => {
            config::path::project_service_config_path_for(slug, "discord")?
        }
        EffectiveScope::Global => config::path::global_service_config_path("discord")?,
    };
    config::save_flat(&path, cfg)
}