Javascript 使用图像创建定时静音命令

Javascript 使用图像创建定时静音命令,javascript,discord.js,commando,Javascript,Discord.js,Commando,我正在和朋友们为我的个人discord服务器制作一个机器人,我们喜欢《星球大战》,所以我把它叫做达斯·维德。我一直在看教程,有了这个机器人,我做得很好,但我被命令卡住了。此命令称为forcechoke。它的作用是在聊天室中放置一条消息: 达斯·维德:forcechoke@fakeplayer持续时间(秒)。 随附文件(达斯·维德掐死某人的照片),我的所有代码都在文件夹中 基本上,它会让人静音60秒,然后显示达斯·维德掐死了人。命令:!强制扼流圈。我有!forcechoke完成,您只需查看即可 c

我正在和朋友们为我的个人discord服务器制作一个机器人,我们喜欢《星球大战》,所以我把它叫做达斯·维德。我一直在看教程,有了这个机器人,我做得很好,但我被命令卡住了。此命令称为
forcechoke
。它的作用是在聊天室中放置一条消息:

达斯·维德:forcechoke@fakeplayer持续时间(秒)。 随附文件(达斯·维德掐死某人的照片),我的所有代码都在文件夹中

基本上,它会让人静音60秒,然后显示达斯·维德掐死了人。命令:
!强制扼流圈
。我有
!forcechoke
完成,您只需查看即可

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;

另外,如果我需要npm安装一些东西,请告诉我

1。静音用户:
没有内置的方法使用户静音,您必须使用角色。创建一个角色(比方说它被称为Mute)并撤销每个频道中的“发送消息”、“连接”、“讲话”等权限。然后用如下方式分配该角色:

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.
}
-重述:
创建一个名为“Mute”的角色(或者您想要的任何角色,只需在代码中替换“Mute”。
找到一个图像,然后您可以从web上使用它,也可以将其保存在本地。我将从web上获取一个,您可以用另一个URL或文件的本地路径替换我的URL。
将此代码添加到命令文件:

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"});
}
run(msg){
让mute_role=msg.guild.roles.find(“name”,“mute”);//在这里可以替换角色名
让member=msg.indications.members.first();
member.addRole(mute_role);//{member.removole(mute_role);},60*1000);//使用常量计时器静音
我的答案基本上是Federico Grandi所做的,但又刷新了一些,并在discord.js-commando中实现。在扩展的
命令
类中添加此
run()
方法。
run()
是commando中用于执行用户请求启动的进程的主要方法

给你:

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;
}
如果您想知道javascript中的
async
关键字,这是一个相当新的东西,但它只允许您运行此方法,而无需让bot等待它完成

setTimeout()
是一个全局javascript函数,它只告诉程序在运行该进程之前等待一定的时间

()=>{}
基本上是
函数helloworld(){}

`${}`
奇怪的字符串格式请尝试读取一点


希望这能有所帮助,学习一些javascript会很开心:)!

我对javascript不是很在行,所以你能帮我设置一下吗?Federico,似乎我遇到了错误:“ReferenceError:帮会未定义”。我的代码可能有问题吗?谢谢你的帮助。对不起,我的错:我用
guild
变量存储公会,我编辑了我的答案。将
guild.roles.find(“name”,“Mute”)
替换为
msg.guild.roles.find(“name”,“Mute”)
我收到一个错误:TypeError:userToMute.addRole不是更新代码的函数。没有意识到从args返回的对象实际上没有方法,只有数据!您必须将第一行更改为我的更新代码当尝试使用代码时,我收到一条红线(错误)在CommandMessage下,any和其他一些。将鼠标悬停在上面,错误是:[js]“types”只能在.ts文件中使用。是否需要使用npm安装任何内容?不,我的坏消息只是更新了代码以删除错误!
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;
}