0

I'm making a bot for my private discord servers with friends, we love Star Wars so I called it Darth Vader. I've been watching tutorials, and with this bot I've gotten pretty good but I'm stuck on a command. This command is called forcechoke. What it does is it puts a message in chat:

Darth Vader: Forcechokes @fakeplayer for (time in seconds). Attached file (of Darth Vader choking someone) that I have in the folder with all my code.

Basically, it mutes the person for 60 seconds but then shows Darth Vader choking someone. The command: !forcechoke <@person> <time in seconds>. I have the !forcechoke done you'll just have to see.

const commando = require('discord.js-commando');

class ForceChokeCommand extends commando.Command
{
    constructor(client)
    {
        super(client,{
            name: 'forcechoke',
            group: 'sith',
            memberName: 'forcechoke',
            description: 'Darth Vader will choke the person of your choice!',
            args: [
                {
                    key: 'user',
                    prompt: 'Who would you like me to forcechoke?',
                    type: 'user'
                }
            ]
        });
    }

// THIS IS WHERE I NEED HELP

  }
}

module.exports = ForceChokeCommand;

Also, if theres something that I need to npm install, please tell me.

Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
Nicholxs
  • 11
  • 2
  • 3

2 Answers2

0

1. Muting a user:
There's no built-in way to mute a user, you'll have to do it with roles. Create a role (let's say it's called Mute) and revoke the permissions like "Send Messages", "Connect", "Speak" and so on in every channel. Then assign that role with something like this:

run(msg) {
  let mute_role = msg.guild.roles.find("name", "Mute");
  let member = msg.mentions.members.first();
  member.addRole(mute_role); // <- this assign the role
  setTimeout(() => {member.removeRole(mute_role);}, 60 * 1000); // <- sets a timeout to unmute the user.
}

Alternatively, you can override the permissions for that user (or even the role) in every channel with guild.channels.forEach() and GuildChannel.overwritePermissions(), that's up to you.

2. Sending an image:
You can either use the URL to an online image or the path:

msg.say(`Forcechockes ${member} for 60 seconds.`, {file: "path or URL as a string"});

- Recap:
Create a role called "Mute" (or whatever you want, just replace "Mute" in the code).
Find an image, then you can both use it from the web or save it locally. I'll take one from the web, you can replace my URL with another URL or the local path to your file.
Add this code to your command file:

run(msg) {
  let mute_role = msg.guild.roles.find("name", "Mute"); // this is where you can replace the role name
  let member = msg.mentions.members.first();
  member.addRole(mute_role); // <- this assign the role
  setTimeout(() => {member.removeRole(mute_role);}, 60 * 1000); // <- sets a timeout to unmute the user.
  //                                                       V this is where the URL or the local path goes                                    V
  msg.say(`Forcechockes ${member} for 60 seconds.`, {file: "https://lumiere-a.akamaihd.net/v1/images/databank_forcechoke_01_169_93e4b0cf.jpeg"});
}
Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
  • I'm not really good with javascript, so could you possibly help me set it up? – Nicholxs Jul 28 '18 at 01:02
  • Federico, it seems that I'm getting the error: "ReferenceError: guild is not defined". Is there possibly something wrong with my code doing it? Thanks for all the help btw. – Nicholxs Jul 30 '18 at 09:23
  • Sorry, my bad: I use to store the guild in a `guild` variable, I edited my answer. Replace `guild.roles.find("name", "Mute")` from the second line of my code with `msg.guild.roles.find("name", "Mute")` – Federico Grandi Jul 30 '18 at 10:34
0

Muting with a constant timer

My answer is basically what Federico Grandi did but flushed out some more and implemented in discord.js-commando. Add this run() method inside your extended Command class. run() is the main method used in commando to execute the process that the user requested to start.

Here you go:

async run(message, args) {
     // get the user from the required args object
    const userToMute = message.guild.members.find('id', args.user.id);
    
    // find the name of a role called Muted in the guild that the message
    // was sent from
    const muteRole = message.guild.roles.find("name", "Muted");

    // add that role to the user that should be muted
    userToMute.addRole(muteRole);

    // the time it takes for the mute to be removed
    // in miliseconds
    const MUTE_TIME = 60 * 1000;

    // wait MUTE_TIME miliseconds and then remove the role
    setTimeout(() => {
        userToMute.removeRole(muteRole);
    }, MUTE_TIME);

    message.channel.send(`*${message.author.username} forcechockes ${userToMute.user.username} for ${MUTE_TIME / 60} seconds*`, { file: 'url/path' });
    return;
}

If you're wondering about the async keyword in javascript it's a fairly new thing but it simply allows you to run this method without making your bot wait for it to finish it.

setTimeout() is a global javascript function that simply tells your program to wait a certain amount of time before running that process.

() => {} at its basics is shorthand for function helloworld() {}

the `${}` strange string format try reading a bit of this.

Hope this helped, have fun learning some javascript :) !

Community
  • 1
  • 1
  • I get an error: TypeError: userToMute.addRole is not a function – Nicholxs Jul 29 '18 at 02:20
  • Updated the code. Didn't realize that the object you get back from the args doesn't actually have the methods on it just the data! You'll have to change the first line to my updated code – Emil Choparinov Jul 29 '18 at 06:58
  • When trying to use your code, I get a red line (error) under CommandMessage, any and a few others. Hovering over it the error is: [js] 'types' can only be used in a .ts file. Is there anything I need to install with npm? – Nicholxs Jul 30 '18 at 09:26
  • Nope my bad just updated the code to remove the error! – Emil Choparinov Jul 30 '18 at 15:13