|
| 1 | +import { INTERNAL_SERVER_ERROR, ROLE_ADDED } from "../constants/responses"; |
| 2 | +import { DISCORD_BASE_URL } from "../constants/urls"; |
| 3 | +import { env } from "../typeDefinitions/default.types"; |
| 4 | +import { |
| 5 | + createNewRole, |
| 6 | + guildRoleResponse, |
| 7 | + memberGroupRole, |
| 8 | +} from "../typeDefinitions/discordMessage.types"; |
| 9 | + |
| 10 | +export async function createGuildRole( |
| 11 | + body: createNewRole, |
| 12 | + env: env |
| 13 | +): Promise<guildRoleResponse | string> { |
| 14 | + const createGuildRoleUrl = `${DISCORD_BASE_URL}/guilds/${env.DISCORD_GUILD_ID}/roles`; |
| 15 | + const data = { |
| 16 | + ...body, |
| 17 | + name: body.rolename, |
| 18 | + }; |
| 19 | + try { |
| 20 | + const response = await fetch(createGuildRoleUrl, { |
| 21 | + method: "POST", |
| 22 | + headers: { |
| 23 | + "Content-Type": "application/json", |
| 24 | + Authorization: `Bot ${env.DISCORD_TOKEN}`, |
| 25 | + }, |
| 26 | + body: JSON.stringify(data), |
| 27 | + }); |
| 28 | + if (response.ok) { |
| 29 | + return await response.json(); |
| 30 | + } else { |
| 31 | + return INTERNAL_SERVER_ERROR; |
| 32 | + } |
| 33 | + } catch (err) { |
| 34 | + return INTERNAL_SERVER_ERROR; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +export async function addGroupRole(body: memberGroupRole, env: env) { |
| 39 | + const { userid, roleid } = body; |
| 40 | + const createGuildRoleUrl = `${DISCORD_BASE_URL}/guilds/${env.DISCORD_GUILD_ID}/members/${userid}/roles/${roleid}`; |
| 41 | + try { |
| 42 | + const response = await fetch(createGuildRoleUrl, { |
| 43 | + method: "PUT", |
| 44 | + headers: { |
| 45 | + "Content-Type": "application/json", |
| 46 | + Authorization: `Bot ${env.DISCORD_TOKEN}`, |
| 47 | + }, |
| 48 | + }); |
| 49 | + if (response.ok) { |
| 50 | + return { message: ROLE_ADDED }; |
| 51 | + } else { |
| 52 | + return INTERNAL_SERVER_ERROR; |
| 53 | + } |
| 54 | + } catch (err) { |
| 55 | + return INTERNAL_SERVER_ERROR; |
| 56 | + } |
| 57 | +} |
0 commit comments