Rafafrias2012 / bonziworld-6.9.15-reconstruction

https://bworg-6915.onrender.com/
1 stars 0 forks source link

adding a silent mute #2

Open Rafafrias2012 opened 4 months ago

Rafafrias2012 commented 4 months ago

Hey @autopilot add a silent mute context menu and command?

codeautopilot[bot] commented 4 months ago

Potential solution

To add a silent mute context menu and command, we need to make changes both on the frontend and backend. The frontend will handle the user interface and emit the command, while the backend will process the command and mute the user silently.

How to implement

Frontend Changes

File: frontend/lib/jquery.contextMenu.min.js

Since this file is minified, we should locate the unminified version or the part of the codebase where the context menu is initialized. Assuming we have access to the unminified version, we will add the 'silent mute' option to the context menu.

File: frontend/script.js

We will add the 'silent mute' option to the context menu and emit a socket event to notify the backend when this option is selected.

$.contextMenu({
    selector: this.selCanvas,
    build: function (a, b) {
        let menu = {
            items: {
                cancel: {
                    name: "Cancel",
                    callback: function () {
                        d.cancel();
                    },
                },
                // Other existing items...
            }
        };

        // Add mod options to menu if king
        if (authlevel >= 1) {
            menu.items.mod = {
                name: "Gamer Mod CMDs",
                items: {
                    // Other mod options...
                    silentmute: {
                        name: "Silent Mute",
                        disabled: authlevel < 1.1,
                        callback: function () {
                            socket.emit("command", { list: ["silentmute", d.id] });
                        }
                    },
                    // Other mod options...
                }
            }
        }

        return menu;
    },
    animation: { duration: 175, show: "fadeIn", hide: "fadeOut" },
});

Backend Changes

File: index.js

We will add the 'silentmute' command to the commands list and implement the logic to mute a user silently.

var commands = {
  // Other commands...

  silentmute: (victim, param) => {
    if (victim.level < 1.1 || !victim.room.usersPublic[param]) return;
    if (users[param].muted == 0) {
      users[param].muted = 1; // Set the muted state to 1 (muted)
      // Do not update the typing status or notify others in the room
    } else if (users[param].muted == 1) {
      users[param].muted = 0; // Unmute the user
      // Do not update the typing status or notify others in the room
    }
  },

  // Other commands...
};

// User object, with handlers and user data
class user {
  constructor(socket) {
    // Existing code...

    // COMMAND HANDLER
    this.socket.on("command", cmd => {
      // parse and check
      if (cmd.list[0] == undefined || this.muted != 0) return;
      var comd = cmd.list[0];
      var param = ""
      if (cmd.list[1] == undefined) param = [""]
      else {
        param = cmd.list;
        param.splice(0, 1);
      }
      param = param.join(" ");
      // filter
      if (typeof param !== 'string') return;
      if (this.sanitize) param = param.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\[\[/g, "&#91;&#91;");
      if (filtertext(param) && this.sanitize) return;
      // carry it out
      if (!this.slowed) {
        if (commands[comd] !== undefined) commands[comd](this, param);
        // Slowmode
        this.slowed = true;
        setTimeout(() => {
          this.slowed = false;
        }, config.slowmode)
      }
    });

    // Existing code...
  }
}

// Existing code...

Summary

  1. Frontend:

    • Locate the context menu initialization code.
    • Add the 'silent mute' option to the context menu.
    • Emit a socket event to notify the backend when the 'silent mute' option is selected.
  2. Backend:

    • Add the 'silentmute' command to the commands list.
    • Implement the logic to mute a user silently without notifying others.

This ensures that the 'silent mute' functionality is integrated seamlessly into the application.

Click here to create a Pull Request with the proposed solution

Files used for this task:

Changes on frontend/lib/jquery.contextMenu.min.js To add a new 'silent mute' option to the context menu in the `frontend/lib/jquery.contextMenu.min.js` file, follow these steps: 1. **Locate the Context Menu Initialization**: Identify where the context menu items are being defined. This is typically done in a configuration object passed to the `contextMenu` function. 2. **Add the 'silent mute' Option**: Insert a new item in the context menu configuration for 'silent mute'. Since the provided file is minified, it's challenging to directly edit it. Instead, you should look for the unminified version of this file or the part of your codebase where the context menu is initialized. However, for demonstration purposes, I'll show you how to add the 'silent mute' option assuming you have access to the unminified version or the initialization code. ### Example of Adding 'Silent Mute' Option Assuming you have an unminified version or the initialization code looks something like this: ```javascript $.contextMenu({ selector: '.your-selector', items: { "edit": {name: "Edit", icon: "edit"}, "cut": {name: "Cut", icon: "cut"}, // Add other existing items here } }); ``` You would add the 'silent mute' option like this: ```javascript $.contextMenu({ selector: '.your-selector', items: { "edit": {name: "Edit", icon: "edit"}, "cut": {name: "Cut", icon: "cut"}, // Add other existing items here "silentMute": { name: "Silent Mute", icon: "mute", callback: function(key, options) { // Logic to handle silent mute console.log("Silent Mute selected"); // You can call a function here to handle the silent mute logic handleSilentMute(options.$trigger); } } } }); ``` ### Steps to Implement 1. **Find the Initialization Code**: Locate where the context menu is being initialized in your codebase. This is usually in a JavaScript file where the context menu is configured. 2. **Add the New Option**: Insert the new 'silent mute' option into the `items` object of the context menu configuration. 3. **Define the Callback**: Implement the callback function that will be executed when the 'silent mute' option is selected. This function should handle the logic for muting the user silently. ### Example Callback Function You might define the `handleSilentMute` function in your `frontend/script.js` file: ```javascript function handleSilentMute(triggerElement) { // Logic to mute the user silently const userId = triggerElement.data('user-id'); // Make an AJAX call to the backend to mute the user silently $.post('/muteUserSilently', { userId: userId }, function(response) { if (response.success) { console.log('User muted silently'); } else { console.log('Failed to mute user'); } }); } ``` ### Summary 1. **Locate the context menu initialization code**. 2. **Add the 'silent mute' option** to the `items` object. 3. **Implement the callback function** to handle the silent mute logic. If the context menu initialization is not in this file, you should locate the appropriate file where the context menu is configured and apply the changes there. If this file is purely a library file and not meant to be edited directly, ensure you make changes in the correct configuration file in your project.
Changes on index.js To implement the backend logic for handling the 'silent mute' command, follow these steps: 1. **Add the 'silent mute' command to the commands list**: - Define a new command `silentmute` that will mute a user without notifying them or others in the room. 2. **Implement the logic for the 'silent mute' command**: - Ensure that the command can only be executed by users with the appropriate authority level. - Modify the user's state to indicate they are muted silently. Here's the updated code with the new `silentmute` command: ```javascript // Add the 'silentmute' command to the commands list var commands = { // ... other commands silentmute: (victim, param) => { if (victim.level < 1.1 || !victim.room.usersPublic[param]) return; if (users[param].muted == 0) { users[param].muted = 1; // Set the muted state to 1 (muted) // Do not update the typing status or notify others in the room } else if (users[param].muted == 1) { users[param].muted = 0; // Unmute the user // Do not update the typing status or notify others in the room } }, // ... other commands }; // User object, with handlers and user data class user { constructor(socket) { // ... existing code // COMMAND HANDLER this.socket.on("command", cmd => { // parse and check if (cmd.list[0] == undefined || this.muted != 0) return; var comd = cmd.list[0]; var param = "" if (cmd.list[1] == undefined) param = [""] else { param = cmd.list; param.splice(0, 1); } param = param.join(" "); // filter if (typeof param !== 'string') return; if (this.sanitize) param = param.replace(//g, ">").replace(/\[\[/g, "[["); if (filtertext(param) && this.sanitize) return; // carry it out if (!this.slowed) { if (commands[comd] !== undefined) commands[comd](this, param); // Slowmode this.slowed = true; setTimeout(() => { this.slowed = false; }, config.slowmode) } }); // ... existing code } } // ... existing code ``` ### Explanation: 1. **Command Definition**: - The `silentmute` command is added to the `commands` object. - It checks if the user has the required authority level (`victim.level < 1.1`). - It toggles the `muted` state of the target user (`users[param].muted`). 2. **Command Execution**: - The command is executed when received from the client. - The `muted` state is set to `1` to mute the user or `0` to unmute them. - No updates are sent to the room, ensuring the mute is silent. This implementation ensures that the 'silent mute' command works as intended, muting a user without notifying them or others in the room.
Changes on frontend/script.js To implement the frontend logic for handling the 'silent mute' command when the context menu option is selected, follow these steps: 1. **Add the 'silent mute' option to the context menu**: - Locate the context menu configuration within the `Bonzi` class constructor. - Add a new menu item for 'silent mute' under the appropriate section (e.g., mod options). 2. **Emit the 'silent mute' command to the backend**: - When the 'silent mute' option is selected, emit a socket event to notify the backend. Here is the detailed implementation: ### Step 1: Add 'silent mute' Option to the Context Menu Locate the context menu configuration in the `Bonzi` class constructor and add the 'silent mute' option: ```javascript $.contextMenu({ selector: this.selCanvas, build: function (a, b) { let menu = { items: { cancel: { name: "Cancel", callback: function () { d.cancel(); }, }, hail: { name: "Heil", callback: function () { socket.emit("command", { list: ["hail", d.userPublic.name] }); }, }, dm: { name: "Private Message", callback: function () { $("#dmto").html("Message " + d.userPublic.name); $("#dmguid").val(d.id); $("#dm").show(); }, }, quote: { name: "Quote/Reply", callback: function () { $("#replyto").html("Reply to " + d.userPublic.name); $("#guid").val(d.id); $("#quote").show(); }, }, heyname: { name: "Hey, NAME!", callback: function () { socket.emit("talk", { text: "Hey, " + d.userPublic.name + "!" }) } }, insult: { name: "Insult", items: { asshole: { name: "Call an Asshole", callback: function () { socket.emit("command", { list: ["asshole", d.userPublic.name] }); }, }, bass: { name: "Call an Bass", callback: function () { socket.emit("command", { list: ["bass", d.userPublic.name] }); }, }, owo: { name: "Notice Bulge", callback: function () { socket.emit("command", { list: ["owo", d.userPublic.name] }); }, }, pastule: { name: "Pastule", callback: function () { socket.emit("talk", { text: d.userPublic.name + " stop being a pastule" }); }, }, nigger: { name: "Niggerify", callback: function () { socket.emit("talk", { text: d.userPublic.name + " WANNA HEAR SOMETHING?" }) setTimeout(() => { socket.emit("command", { list: ["nigger", ""] }) }, 2000) } }, kys: { name: "Ask to KYS", callback: function () { socket.emit("talk", { text: "Hey, " + d.userPublic.name + " kill yourself!" }) } }, } } } }; // Add mod options to menu if king if (authlevel >= 1) { menu.items.mod = { name: "Gamer Mod CMDs", items: { jew: { name: "Jewify", callback: function () { socket.emit("command", { list: ["jewify", d.id] }); } }, bless: { name: "Bless", callback: function () { socket.emit("command", { list: ["bless", d.id] }); } }, rabbi: { name: "Rabbify", callback: function () { socket.emit("command", { list: ["rabbify", d.id] }); } }, Hypnotize: { name: "Hypnotize", callback: function () { socket.emit("command", { list: ["hypnotize", d.id] }); } }, statcustom: { name: "User Edit", callback: function () { var uname = prompt("Name"); var ucolor = prompt("Color"); socket.emit("useredit", { id: d.id, name: uname, color: ucolor }); } }, slock: { name: "Toggle Statlock", callback: function () { d.statlock = !d.statlock; socket.emit("command", { list: ["statlock", d.id] }); } }, fullmute: { name: "Server Mute/Unmute", disabled: authlevel < 1.1, callback: function () { socket.emit("command", { list: ["smute", d.id] }); } }, silentmute: { // Add silent mute option here name: "Silent Mute", disabled: authlevel < 1.1, callback: function () { socket.emit("command", { list: ["silentmute", d.id] }); } }, niggle: { name: "Nuke", disabled: authlevel < 1.1, callback: function () { socket.emit("command", { list: ["floyd", d.id] }); } }, kick: { name: "Kick", disabled: authlevel < 1.1, callback: function () { socket.emit("command", { list: ["kick", d.id] }); } }, stats: { name: "Check User Stats", disabled: authlevel < 1.1, callback: function () { alert('name: ' + d.userPublic.name + 'color: ' + d.userPublic.color + 'GUID: ' + d.id); } }, } } } // Additional options for higher auth levels... return menu; }, animation: { duration: 175, show: "fadeIn", hide: "fadeOut" }, }); ``` ### Step 2: Emit the 'silent mute' Command to the Backend When the 'silent mute' option is selected, emit a socket event to notify the backend: ```javascript silentmute: { name: "Silent Mute", disabled: authlevel < 1.1, callback: function () { socket.emit("command", { list: ["silentmute", d.id] }); } } ``` This will send a command to the backend to handle the 'silent mute' functionality. Ensure that the backend logic in `index.js` is implemented to handle this command appropriately. ### Summary - Added a new 'silent mute' option to the context menu. - Emitted a socket event to notify the backend when the 'silent mute' option is selected. This completes the frontend implementation for handling the 'silent mute' command.