Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement of /listening command to mute users #206

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 10 additions & 25 deletions src/controllers/baseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { taskCommand } from "./taskCommand";
import { notifyCommand } from "./notifyCommand";
import { oooCommand } from "./oooCommand";
import { userCommand } from "./userCommand";

import { muteUser, unmuteUser } from "../utils/userMuteUnmuteActions";
import { getCommandName } from "../utils/getCommandName";
import JSONResponse from "../utils/JsonResponse";
import { lowerCaseMessageCommands } from "../utils/lowerCaseMessageCommand";
Expand Down Expand Up @@ -39,6 +39,7 @@ import {
REMOVED_LISTENING_MESSAGE,
RETRY_COMMAND,
} from "../constants/responses";
import { DISCORD_BASE_URL } from "../constants/urls";

export async function baseHandler(
message: discordMessageRequest,
Expand Down Expand Up @@ -67,47 +68,32 @@ export async function baseHandler(
roleToBeTaggedObj: data[0],
displayMessageObj: data[1] ?? {},
};

return await mentionEachUser(transformedArgument, env);
}

case getCommandName(LISTENING): {
const data = message.data?.options;
const setter = data ? data[0].value : false;
const memberId = message.member.user.id.toString();
const nickname = removeListening(message.member.nick || "");
let discordEphemeral;
let updateNickNameData = "";
try {
if (setter) {
if (
message.member.nick &&
!message.member.nick.includes(NICKNAME_SUFFIX)
) {
if (!message.member.nick?.includes(NICKNAME_SUFFIX)) {
updateNickNameData =
NICKNAME_PREFIX + message.member.nick + NICKNAME_SUFFIX;
NICKNAME_PREFIX + message.member.user.username + NICKNAME_SUFFIX;
discordEphemeral = LISTENING_SUCCESS_MESSAGE;
} else if (!message.member.nick) {
(updateNickNameData = NICKNAME_PREFIX + "" + NICKNAME_SUFFIX),
(discordEphemeral = LISTENING_SUCCESS_MESSAGE);
await muteUser(memberId, message.guild.id, env.DISCORD_TOKEN);
} else {
updateNickNameData = message.member.nick;
discordEphemeral = ALREADY_LISTENING;
}
} else if (
!setter &&
!message.member.nick &&
message.member.user.username.includes(NICKNAME_SUFFIX)
) {
updateNickNameData = message.member.user.username + NICKNAME_SUFFIX;

discordEphemeral = REMOVED_LISTENING_MESSAGE;
} else {
if (message.member.nick?.includes(NICKNAME_SUFFIX)) {
updateNickNameData = nickname;
discordEphemeral = REMOVED_LISTENING_MESSAGE;
} else {
updateNickNameData = nickname;
discordEphemeral = NOTHING_CHANGED;
}
updateNickNameData = nickname;
discordEphemeral = REMOVED_LISTENING_MESSAGE;
await unmuteUser(memberId, message.guild.id, env.DISCORD_TOKEN);
}
await updateNickName(
message.member.user.id.toString(),
Expand Down Expand Up @@ -135,7 +121,6 @@ export async function baseHandler(
const data = message.data?.options as Array<messageRequestDataOptions>;
return await oooCommand(data[0].value);
}

case getCommandName(USER): {
const data = message.data?.options as Array<messageRequestDataOptions>;
return await userCommand(data[0].value, env);
Expand Down
1 change: 1 addition & 0 deletions src/typeDefinitions/discordMessage.types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface discordMessageRequest {
guild: any;
type: number;
data: messageRequestData;
member: messageRequestMember;
Expand Down
63 changes: 63 additions & 0 deletions src/utils/userMuteUnmuteActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { DISCORD_BASE_URL } from "../constants/urls";

export async function muteUser(
userId: string,
guildId: string,
token: string
): Promise<void> {
try {
const response = await fetch(
`${DISCORD_BASE_URL}/guilds/${guildId}/members/${userId}`,
{
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: `Bot ${token}`,
},
body: JSON.stringify({
mute: true,
channel_id: null,
}),
}
);

if (response.ok) {
console.log("User muted successfully");
} else {
console.error(`Error: ${response.status} - ${response.statusText}`);
}
} catch (error) {
console.error("Error occurred:", error);
}
}

export async function unmuteUser(
userId: string,
guildId: string,
token: string
): Promise<void> {
try {
const response = await fetch(
`${DISCORD_BASE_URL}/guilds/${guildId}/members/${userId}`,
{
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: `Bot ${token}`,
},
body: JSON.stringify({
mute: false,
channel_id: null,
}),
}
);

if (response.ok) {
console.log("User unmuted successfully");
} else {
console.error(`Error: ${response.status} - ${response.statusText}`);
}
} catch (error) {
console.error("Error occurred:", error);
}
}
2 changes: 2 additions & 0 deletions tests/fixtures/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const dummyHelloMessage: discordMessageRequest = {
},
},
guild_id: 123456,
guild: undefined,
};

export const dummyVerifyMessage: discordMessageRequest = {
Expand All @@ -39,6 +40,7 @@ export const dummyVerifyMessage: discordMessageRequest = {
},
},
guild_id: 123456,
guild: undefined,
};

export const dummyCreateBody: createNewRole = {
Expand Down
Loading