File size: 3,296 Bytes
fb4d8fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { APIChannel } from "discord-api-types/v10";
import { Routes } from "discord-api-types/v10";
import type {
  DiscordChannelCreate,
  DiscordChannelEdit,
  DiscordChannelMove,
  DiscordChannelPermissionSet,
  DiscordReactOpts,
} from "./send.types.js";
import { resolveDiscordRest } from "./send.shared.js";

export async function createChannelDiscord(
  payload: DiscordChannelCreate,
  opts: DiscordReactOpts = {},
): Promise<APIChannel> {
  const rest = resolveDiscordRest(opts);
  const body: Record<string, unknown> = {
    name: payload.name,
  };
  if (payload.type !== undefined) {
    body.type = payload.type;
  }
  if (payload.parentId) {
    body.parent_id = payload.parentId;
  }
  if (payload.topic) {
    body.topic = payload.topic;
  }
  if (payload.position !== undefined) {
    body.position = payload.position;
  }
  if (payload.nsfw !== undefined) {
    body.nsfw = payload.nsfw;
  }
  return (await rest.post(Routes.guildChannels(payload.guildId), {
    body,
  })) as APIChannel;
}

export async function editChannelDiscord(
  payload: DiscordChannelEdit,
  opts: DiscordReactOpts = {},
): Promise<APIChannel> {
  const rest = resolveDiscordRest(opts);
  const body: Record<string, unknown> = {};
  if (payload.name !== undefined) {
    body.name = payload.name;
  }
  if (payload.topic !== undefined) {
    body.topic = payload.topic;
  }
  if (payload.position !== undefined) {
    body.position = payload.position;
  }
  if (payload.parentId !== undefined) {
    body.parent_id = payload.parentId;
  }
  if (payload.nsfw !== undefined) {
    body.nsfw = payload.nsfw;
  }
  if (payload.rateLimitPerUser !== undefined) {
    body.rate_limit_per_user = payload.rateLimitPerUser;
  }
  return (await rest.patch(Routes.channel(payload.channelId), {
    body,
  })) as APIChannel;
}

export async function deleteChannelDiscord(channelId: string, opts: DiscordReactOpts = {}) {
  const rest = resolveDiscordRest(opts);
  await rest.delete(Routes.channel(channelId));
  return { ok: true, channelId };
}

export async function moveChannelDiscord(payload: DiscordChannelMove, opts: DiscordReactOpts = {}) {
  const rest = resolveDiscordRest(opts);
  const body: Array<Record<string, unknown>> = [
    {
      id: payload.channelId,
      ...(payload.parentId !== undefined && { parent_id: payload.parentId }),
      ...(payload.position !== undefined && { position: payload.position }),
    },
  ];
  await rest.patch(Routes.guildChannels(payload.guildId), { body });
  return { ok: true };
}

export async function setChannelPermissionDiscord(
  payload: DiscordChannelPermissionSet,
  opts: DiscordReactOpts = {},
) {
  const rest = resolveDiscordRest(opts);
  const body: Record<string, unknown> = {
    type: payload.targetType,
  };
  if (payload.allow !== undefined) {
    body.allow = payload.allow;
  }
  if (payload.deny !== undefined) {
    body.deny = payload.deny;
  }
  await rest.put(`/channels/${payload.channelId}/permissions/${payload.targetId}`, { body });
  return { ok: true };
}

export async function removeChannelPermissionDiscord(
  channelId: string,
  targetId: string,
  opts: DiscordReactOpts = {},
) {
  const rest = resolveDiscordRest(opts);
  await rest.delete(`/channels/${channelId}/permissions/${targetId}`);
  return { ok: true };
}