generated from nhcarrigan/template
refactor: hardcode Elysian role ID instead of env var
This commit is contained in:
@@ -10,5 +10,4 @@ DISCORD_MILESTONE_WEBHOOK="op://Environment Variables - Naomi/Elysium/discord mi
|
|||||||
DISCORD_BOT_TOKEN="op://Environment Variables - Naomi/Elysium/discord bot token"
|
DISCORD_BOT_TOKEN="op://Environment Variables - Naomi/Elysium/discord bot token"
|
||||||
DISCORD_GUILD_ID="op://Environment Variables - Naomi/Elysium/discord guild id"
|
DISCORD_GUILD_ID="op://Environment Variables - Naomi/Elysium/discord guild id"
|
||||||
DISCORD_APOTHEOSIS_ROLE_ID="op://Environment Variables - Naomi/Elysium/discord apotheosis role id"
|
DISCORD_APOTHEOSIS_ROLE_ID="op://Environment Variables - Naomi/Elysium/discord apotheosis role id"
|
||||||
DISCORD_ELYSIAN_ROLE_ID="op://Environment Variables - Naomi/Elysium/discord elysian role id"
|
|
||||||
LOG_TOKEN="op://Environment Variables - Naomi/Alert Server/api_auth"
|
LOG_TOKEN="op://Environment Variables - Naomi/Alert Server/api_auth"
|
||||||
@@ -15,6 +15,11 @@ const discordApi = "https://discord.com/api/v10";
|
|||||||
*/
|
*/
|
||||||
const suppressNotifications = 4096;
|
const suppressNotifications = 4096;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Discord role ID for the Elysian role granted to all Elysium players.
|
||||||
|
*/
|
||||||
|
const elysianRoleId = "1486144823684628490";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Grants the Elysian Discord role to the given player and returns whether they are in the guild.
|
* Grants the Elysian Discord role to the given player and returns whether they are in the guild.
|
||||||
* Fails silently so role grant errors do not affect the auth flow.
|
* Fails silently so role grant errors do not affect the auth flow.
|
||||||
@@ -24,19 +29,17 @@ const suppressNotifications = 4096;
|
|||||||
const grantElysianRole = async(discordId: string): Promise<boolean> => {
|
const grantElysianRole = async(discordId: string): Promise<boolean> => {
|
||||||
const botToken = process.env.DISCORD_BOT_TOKEN;
|
const botToken = process.env.DISCORD_BOT_TOKEN;
|
||||||
const guildId = process.env.DISCORD_GUILD_ID;
|
const guildId = process.env.DISCORD_GUILD_ID;
|
||||||
const roleId = process.env.DISCORD_ELYSIAN_ROLE_ID;
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
botToken === undefined || botToken === ""
|
botToken === undefined || botToken === ""
|
||||||
|| guildId === undefined || guildId === ""
|
|| guildId === undefined || guildId === ""
|
||||||
|| roleId === undefined || roleId === ""
|
|
||||||
) {
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${discordApi}/guilds/${guildId}/members/${discordId}/roles/${roleId}`,
|
`${discordApi}/guilds/${guildId}/members/${discordId}/roles/${elysianRoleId}`,
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
"Authorization": `Bot ${botToken}`,
|
"Authorization": `Bot ${botToken}`,
|
||||||
|
|||||||
@@ -101,25 +101,14 @@ describe("webhook service", () => {
|
|||||||
expect(result).toBe(false);
|
expect(result).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does nothing when role id is missing", async () => {
|
|
||||||
process.env["DISCORD_BOT_TOKEN"] = "token";
|
|
||||||
process.env["DISCORD_GUILD_ID"] = "guild123";
|
|
||||||
delete process.env["DISCORD_ELYSIAN_ROLE_ID"];
|
|
||||||
const { grantElysianRole } = await import("../../src/services/webhook.js");
|
|
||||||
const result = await grantElysianRole("user123");
|
|
||||||
expect(mockFetch).not.toHaveBeenCalled();
|
|
||||||
expect(result).toBe(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns true when Discord API responds with ok", async () => {
|
it("returns true when Discord API responds with ok", async () => {
|
||||||
process.env["DISCORD_BOT_TOKEN"] = "bot_token";
|
process.env["DISCORD_BOT_TOKEN"] = "bot_token";
|
||||||
process.env["DISCORD_GUILD_ID"] = "guild123";
|
process.env["DISCORD_GUILD_ID"] = "guild123";
|
||||||
process.env["DISCORD_ELYSIAN_ROLE_ID"] = "role456";
|
|
||||||
mockFetch.mockResolvedValueOnce({ ok: true, status: 200 });
|
mockFetch.mockResolvedValueOnce({ ok: true, status: 200 });
|
||||||
const { grantElysianRole } = await import("../../src/services/webhook.js");
|
const { grantElysianRole } = await import("../../src/services/webhook.js");
|
||||||
const result = await grantElysianRole("user789");
|
const result = await grantElysianRole("user789");
|
||||||
expect(mockFetch).toHaveBeenCalledWith(
|
expect(mockFetch).toHaveBeenCalledWith(
|
||||||
"https://discord.com/api/v10/guilds/guild123/members/user789/roles/role456",
|
"https://discord.com/api/v10/guilds/guild123/members/user789/roles/1486144823684628490",
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
headers: expect.objectContaining({ Authorization: "Bot bot_token" }),
|
headers: expect.objectContaining({ Authorization: "Bot bot_token" }),
|
||||||
@@ -131,7 +120,6 @@ describe("webhook service", () => {
|
|||||||
it("returns true when Discord API responds with 204", async () => {
|
it("returns true when Discord API responds with 204", async () => {
|
||||||
process.env["DISCORD_BOT_TOKEN"] = "tok";
|
process.env["DISCORD_BOT_TOKEN"] = "tok";
|
||||||
process.env["DISCORD_GUILD_ID"] = "g";
|
process.env["DISCORD_GUILD_ID"] = "g";
|
||||||
process.env["DISCORD_ELYSIAN_ROLE_ID"] = "r";
|
|
||||||
mockFetch.mockResolvedValueOnce({ ok: false, status: 204 });
|
mockFetch.mockResolvedValueOnce({ ok: false, status: 204 });
|
||||||
const { grantElysianRole } = await import("../../src/services/webhook.js");
|
const { grantElysianRole } = await import("../../src/services/webhook.js");
|
||||||
const result = await grantElysianRole("user");
|
const result = await grantElysianRole("user");
|
||||||
@@ -141,7 +129,6 @@ describe("webhook service", () => {
|
|||||||
it("returns false when Discord API responds with an error status", async () => {
|
it("returns false when Discord API responds with an error status", async () => {
|
||||||
process.env["DISCORD_BOT_TOKEN"] = "tok";
|
process.env["DISCORD_BOT_TOKEN"] = "tok";
|
||||||
process.env["DISCORD_GUILD_ID"] = "g";
|
process.env["DISCORD_GUILD_ID"] = "g";
|
||||||
process.env["DISCORD_ELYSIAN_ROLE_ID"] = "r";
|
|
||||||
mockFetch.mockResolvedValueOnce({ ok: false, status: 403 });
|
mockFetch.mockResolvedValueOnce({ ok: false, status: 403 });
|
||||||
const { grantElysianRole } = await import("../../src/services/webhook.js");
|
const { grantElysianRole } = await import("../../src/services/webhook.js");
|
||||||
const result = await grantElysianRole("user");
|
const result = await grantElysianRole("user");
|
||||||
@@ -151,7 +138,6 @@ describe("webhook service", () => {
|
|||||||
it("returns false and swallows fetch errors gracefully", async () => {
|
it("returns false and swallows fetch errors gracefully", async () => {
|
||||||
process.env["DISCORD_BOT_TOKEN"] = "tok";
|
process.env["DISCORD_BOT_TOKEN"] = "tok";
|
||||||
process.env["DISCORD_GUILD_ID"] = "g";
|
process.env["DISCORD_GUILD_ID"] = "g";
|
||||||
process.env["DISCORD_ELYSIAN_ROLE_ID"] = "r";
|
|
||||||
mockFetch.mockRejectedValueOnce(new Error("Network error"));
|
mockFetch.mockRejectedValueOnce(new Error("Network error"));
|
||||||
const { grantElysianRole } = await import("../../src/services/webhook.js");
|
const { grantElysianRole } = await import("../../src/services/webhook.js");
|
||||||
const result = await grantElysianRole("user");
|
const result = await grantElysianRole("user");
|
||||||
@@ -161,7 +147,6 @@ describe("webhook service", () => {
|
|||||||
it("returns false and swallows non-Error fetch rejections", async () => {
|
it("returns false and swallows non-Error fetch rejections", async () => {
|
||||||
process.env["DISCORD_BOT_TOKEN"] = "tok";
|
process.env["DISCORD_BOT_TOKEN"] = "tok";
|
||||||
process.env["DISCORD_GUILD_ID"] = "g";
|
process.env["DISCORD_GUILD_ID"] = "g";
|
||||||
process.env["DISCORD_ELYSIAN_ROLE_ID"] = "r";
|
|
||||||
mockFetch.mockRejectedValueOnce("raw string error");
|
mockFetch.mockRejectedValueOnce("raw string error");
|
||||||
const { grantElysianRole } = await import("../../src/services/webhook.js");
|
const { grantElysianRole } = await import("../../src/services/webhook.js");
|
||||||
const result = await grantElysianRole("user");
|
const result = await grantElysianRole("user");
|
||||||
|
|||||||
Reference in New Issue
Block a user